feat: add parsable error handling to api methods

This commit is contained in:
mrjvs
2026-08-11 20:31:42 +02:00
parent f98a244606
commit 95cefec5c2
12 changed files with 128 additions and 37 deletions

View File

@@ -7,10 +7,7 @@ export default defineEventHandler(async (event): Promise<void> => {
const captchaResult = await hcaptchaVerify(event, body.captchaResponse);
if (!captchaResult) {
throw createError({
status: 400,
message: 'Invalid captcha'
});
throw createApiError('INVALID_CAPTCHA');
}
await grpc.forgotPassword({

View File

@@ -1,4 +1,4 @@
import { ServerError } from 'nice-grpc';
import { ClientError } from 'nice-grpc';
import { LoginSchema } from '#shared/api-types';
import type { ApiAuthLogin } from '#shared/api-types';
@@ -18,18 +18,12 @@ export default defineEventHandler(async (event): Promise<ApiAuthLogin> => {
refreshToken: res.refreshToken
};
} catch (error: unknown) {
if (error instanceof ServerError) {
if (error instanceof ClientError) {
if (error.details === 'INVALID_ARGUMENT: User not found') {
throw createError({
status: 400,
message: 'User not found'
});
throw createApiError('INVALID_USERNAME');
}
if (error.details === 'INVALID_ARGUMENT: Password is incorrect') {
throw createError({
status: 400,
message: 'Password was incorrect'
});
throw createApiError('INVALID_PASSWORD');
}
}
throw error;