Files
website/shared/errors.ts

70 lines
2.0 KiB
TypeScript

const apiErrorCodes = {
UNPARSABLE_ERROR: 'Fatal exception!',
UNHANDLED_ERROR: 'Something went wrong',
INVALID_INPUT: 'Invalid input',
INTEGRATION_DISABLED: 'Integration with this service is disabled',
STAFF_NO_DONATE: 'Staff members do not need to purchase tiers',
INVALID_CAPTCHA: 'Invalid captcha, try again',
INVALID_USERNAME: 'Could not find user',
INVALID_PASSWORD: 'Incorrect password',
UNAUTHENTICATED: 'This request needs authentication',
UNDER_THIRTEEN: 'Must be 13 or older to use these services',
INVALID_EMAIL: 'Invalid email address',
USERNAME_TOO_SHORT: 'Username is too short',
USERNAME_TOO_LONG: 'Username is too long',
USERNAME_INVALID_CHARS: 'Username contains invalid characters',
USERNAME_IN_USE: 'PNID already in use',
MIINAME_TOO_LONG: 'Mii name too long',
INVALID_PASSWORD_INPUT: 'Password must be between 6 and 16 characters long',
INVALID_PASSWORD_NO_MATCH: 'Passwords do not match',
ACCOUNT_DELETED: 'Account has been deleted'
} as const;
export type ApiErrorCodes = keyof typeof apiErrorCodes;
export const apiErrorCodeStatus: Record<ApiErrorCodes, number> = {
UNPARSABLE_ERROR: 500,
UNHANDLED_ERROR: 500,
INVALID_INPUT: 400,
INTEGRATION_DISABLED: 500,
STAFF_NO_DONATE: 400,
INVALID_CAPTCHA: 400,
INVALID_USERNAME: 400,
INVALID_PASSWORD: 400,
UNAUTHENTICATED: 401,
UNDER_THIRTEEN: 400,
ACCOUNT_DELETED: 400,
INVALID_EMAIL: 400,
INVALID_PASSWORD_INPUT: 400,
INVALID_PASSWORD_NO_MATCH: 400,
MIINAME_TOO_LONG: 400,
USERNAME_IN_USE: 400,
USERNAME_INVALID_CHARS: 400,
USERNAME_TOO_LONG: 400,
USERNAME_TOO_SHORT: 400
};
export function getTextForApiErrorCode(code: ApiErrorCodes): string {
return apiErrorCodes[code];
}
export type ApiError = {
code: ApiErrorCodes;
message: string;
};
export function getApiError(error: any): ApiError {
if (error?.code) {
return error as ApiError;
}
if (error?.data?.code) {
return error.data as ApiError;
}
return {
code: 'UNPARSABLE_ERROR',
message: getTextForApiErrorCode('UNPARSABLE_ERROR')
};
}