mirror of
https://github.com/PretendoNetwork/website.git
synced 2026-09-08 00:47:10 -05:00
feat: add parsable error handling to api methods
This commit is contained in:
@@ -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: {
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 });
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user