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

@@ -8,10 +8,7 @@ export default defineEventHandler(async (event): Promise<ApiAccountCheckoutLink>
const stripe = useStripe(event);
const config = useRuntimeConfig(event);
if (!stripe || !papr) {
throw createError({
status: 400,
message: 'Stripe integration not configured'
});
throw createApiError('INTEGRATION_DISABLED');
}
const body = await readZodBody(event, CheckoutSchema);
@@ -30,10 +27,7 @@ export default defineEventHandler(async (event): Promise<ApiAccountCheckoutLink>
// ensure PNID always has latest customer ID
if (auth.accessLevel >= 2) {
throw createError({
status: 400,
message: 'Staff members do not need to purchase tiers'
});
throw createApiError('STAFF_NO_DONATE');
}
await papr.Pnid.updateOne({ pid: auth.pid }, {
$set: {

View File

@@ -5,10 +5,7 @@ export default defineEventHandler(async (event): Promise<ApiAccountDiscordLink>
enforceLoggedIn(event);
const discord = useDiscord(event);
if (!discord) {
throw createError({
status: 400,
message: 'Discord integration not configured'
});
throw createApiError('INTEGRATION_DISABLED');
}
const redirectUrl = discord.makeCallbackUrl();

View File

@@ -4,10 +4,7 @@ export default defineEventHandler(async (event): Promise<void> => {
const auth = enforceLoggedIn(event);
const discord = useDiscord(event);
if (!discord) {
throw createError({
status: 400,
message: 'Discord integration not configured'
});
throw createApiError('INTEGRATION_DISABLED');
}
const grpc = useApiGrpcWithToken(event, auth.token);

View File

@@ -4,10 +4,7 @@ export default defineEventHandler(async (event): Promise<ApiAccountTiers> => {
enforceLoggedIn(event);
const stripe = useStripe(event);
if (!stripe) {
throw createError({
status: 400,
message: 'Stripe integration not configured'
});
throw createApiError('INTEGRATION_DISABLED');
}
const prices = await stripe.prices.list().autoPagingToArray({ limit: 10 });

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;