🐛 Fix ping issue

This commit is contained in:
Meier Lukas
2023-07-17 21:35:34 +02:00
parent e831ea0acc
commit c1b8af911f

View File

@@ -16,12 +16,17 @@ export const appRouter = createTRPCRouter({
const configName = getCookie('config-name'); const configName = getCookie('config-name');
const config = getConfig(configName?.toString() ?? 'default'); const config = getConfig(configName?.toString() ?? 'default');
const app = config.apps.find((app) => app.id === input); const app = config.apps.find((app) => app.id === input);
const errorResponse = {
state: 'offline',
status: 500,
statusText: 'Check logs for more informations',
};
const url = app?.url; const url = app?.url;
if (url === undefined || !app) { if (url === undefined || !app) {
throw new TRPCError({ Consola.error(`App ${input} not found`);
code: 'NOT_FOUND', return errorResponse;
message: 'App or url not found',
});
} }
const res = await axios const res = await axios
.get(url, { httpsAgent: agent, timeout: 2000 }) .get(url, { httpsAgent: agent, timeout: 2000 })
@@ -31,27 +36,20 @@ export const appRouter = createTRPCRouter({
})) }))
.catch((error: AxiosError) => { .catch((error: AxiosError) => {
if (error.response) { if (error.response) {
if (getIsOk(app as AppType, error.response.status)) { return {
return { state: getIsOk(app as AppType, error.response.status) ? 'online' : 'offline',
state: 'offline', status: error.response.status,
status: error.response.status, statusText: error.response.statusText,
statusText: error.response.statusText, };
};
}
} }
if (error.code === 'ECONNABORTED') { if (error.code === 'ECONNABORTED') {
throw new TRPCError({ Consola.error(`Ping timeout for ${input}`);
code: 'TIMEOUT', return errorResponse;
message: 'Request Timeout',
});
} }
Consola.error(`Unexpected response: ${error.message}`); Consola.error(`Unexpected response: ${error.message}`);
throw new TRPCError({ return errorResponse;
code: 'INTERNAL_SERVER_ERROR',
cause: app.id,
message: error.message,
});
}); });
return res; return res;
}), }),