fix(api): handle errors on APIs

This commit is contained in:
Nicolas Meienberger 2024-03-21 08:49:01 +01:00
parent 7d7e67dd02
commit 8d10d0e96a
10 changed files with 51 additions and 16 deletions

View File

@ -8,7 +8,7 @@
* - Please do NOT serve this file on production.
*/
const PACKAGE_VERSION = '2.2.6'
const PACKAGE_VERSION = '2.2.9'
const INTEGRITY_CHECKSUM = '26357c79639bfa20d64c0efca2a87423'
const IS_MOCKED_RESPONSE = Symbol('isMockedResponse')
const activeClientIds = new Set()

View File

@ -14,7 +14,7 @@ const input = z.object({ id: z.string() });
*/
export const startAppAction = action(input, async ({ id }) => {
try {
ensureUser();
await ensureUser();
await appService.startApp(id);

View File

@ -5,7 +5,7 @@ export const ensureUser = async () => {
const user = await getUserFromCookie();
if (!user) {
throw new TranslatedError('SYSTEM_ERROR_YOU_MUST_BE_LOGGED_IN');
throw new TranslatedError('SYSTEM_ERROR_YOU_MUST_BE_LOGGED_IN', {}, 401);
}
return user;

View File

@ -0,0 +1,26 @@
import * as Sentry from '@sentry/nextjs';
import { MessageKey, TranslatedError } from '@/server/utils/errors';
import { getTranslatorFromCookie } from '@/lib/get-translator';
import { TipiConfig } from '@/server/core/TipiConfig';
import { Logger } from '../../../server/core/Logger';
/**
* Given an error, returns a 500 response with the translated error message.
*/
export const handleApiError = async (e: unknown) => {
const originalMessage = e instanceof Error ? e.message : e;
const status = e instanceof TranslatedError ? e.status : 500;
const errorVariables = e instanceof TranslatedError ? e.variableValues : {};
const translator = await getTranslatorFromCookie();
const messageTranslated =
e instanceof TranslatedError ? translator(originalMessage as MessageKey, errorVariables) : translator('INTERNAL_SERVER_ERROR');
// Non TranslatedErrors are unexpected and should be reported to Sentry.
if (!(e instanceof TranslatedError) && TipiConfig.getConfig().allowErrorMonitoring) {
Logger.error(e);
Sentry.captureException(e);
}
return new Response(messageTranslated as string, { status });
};

View File

@ -1,9 +1,9 @@
import * as Sentry from '@sentry/nextjs';
import { TipiConfig } from '@/server/core/TipiConfig/TipiConfig';
import { pathExists } from '@runtipi/shared/node';
import fs from 'fs-extra';
import path from 'path';
import { sanitizePath } from '@runtipi/shared';
import { handleApiError } from '@/actions/utils/handle-api-error';
import { APP_DIR, DATA_DIR } from '../../../config/constants';
export async function GET(request: Request) {
@ -30,7 +30,6 @@ export async function GET(request: Request) {
return new Response(file, { headers: { 'content-type': 'image/jpeg' } });
} catch (error) {
Sentry.captureException(error);
return new Response('Error', { status: 500 });
return handleApiError(error);
}
}

View File

@ -1,3 +1,5 @@
import { ensureUser } from '@/actions/utils/ensure-user';
import { handleApiError } from '@/actions/utils/handle-api-error';
import { appService } from '@/server/services/apps/apps.service';
const getApps = async (searchParams: URLSearchParams) => {
@ -10,11 +12,17 @@ const getApps = async (searchParams: URLSearchParams) => {
};
export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
try {
await ensureUser();
const apps = await getApps(searchParams);
const { searchParams } = new URL(request.url);
return new Response(JSON.stringify(apps), { headers: { 'content-type': 'application/json' } });
const apps = await getApps(searchParams);
return new Response(JSON.stringify(apps), { headers: { 'content-type': 'application/json' } });
} catch (error) {
return handleApiError(error);
}
}
export type AppStoreApiResponse = Awaited<ReturnType<typeof getApps>>;

View File

@ -1,6 +1,6 @@
import * as Sentry from '@sentry/nextjs';
import { getUserFromCookie } from '@/server/common/session.helpers';
import fs from 'fs-extra';
import { handleApiError } from '@/actions/utils/handle-api-error';
import { DATA_DIR } from '../../../config/constants';
export async function GET() {
@ -26,7 +26,6 @@ export async function GET() {
return new Response('Forbidden', { status: 403 });
} catch (error) {
Sentry.captureException(error);
return new Response('Error', { status: 500 });
return handleApiError(error);
}
}

View File

@ -1,5 +1,5 @@
import * as Sentry from '@sentry/nextjs';
import { ensureUser } from '@/actions/utils/ensure-user';
import { handleApiError } from '@/actions/utils/handle-api-error';
import { fetchSystemStatus } from './fetch-system-status';
export async function GET() {
@ -9,7 +9,6 @@ export async function GET() {
const data = await fetchSystemStatus();
return new Response(JSON.stringify(data), { status: 200 });
} catch (error) {
Sentry.captureException(error);
return new Response('Error', { status: 500 });
return handleApiError(error);
}
}

View File

@ -173,6 +173,7 @@
"HEADER_SOURCE_CODE": "Source code",
"HEADER_SPONSOR": "Sponsor",
"HEADER_UPDATE_AVAILABLE": "Update available",
"INTERNAL_SERVER_ERROR": "Internal server error",
"LINKS_ADD_SUBMIT": "Submit",
"LINKS_ADD_SUBTITLE": "Add external link to the dashboard",
"LINKS_ADD_SUCCESS": "Link added succesfully",

View File

@ -8,12 +8,15 @@ export type MessageKey = Parameters<typeof t>[0];
export class TranslatedError extends Error {
public readonly variableValues: TranslationValues;
constructor(message: MessageKey, variableValues: TranslationValues = {}) {
public readonly status: number;
constructor(message: MessageKey, variableValues: TranslationValues = {}, status?: number) {
super(message);
Logger.error(`server error: ${t(message, variableValues)}`);
this.name = 'TranslatedError';
this.variableValues = variableValues;
this.status = status || 500;
}
}