mirror of
https://github.com/PretendoNetwork/website.git
synced 2026-09-22 15:57:08 -05:00
100 lines
3.1 KiB
TypeScript
100 lines
3.1 KiB
TypeScript
const apiErrorCodes = {
|
|
UNPARSABLE_ERROR: 'Fatal exception!',
|
|
RATELIMITED: 'Too many requests!',
|
|
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',
|
|
PASSWORD_INVALID_LENGTH: 'Password must be between 6 and 16 characters long',
|
|
PASSWORD_NOT_USERNAME: 'Password cannot be the same as your username',
|
|
PASSWORD_NEEDS_CHARS: 'Password must have combination of letters, numbers, and/or punctuation characters',
|
|
PASSWORDS_DO_NOT_MATCH: 'Passwords do not match',
|
|
PASSWORD_REPEATED_CHARS: 'Password may not have 3 repeating characters',
|
|
ACCOUNT_DELETED: 'Account has been deleted',
|
|
INVALID_ACCESS_LEVEL: 'Invalid access level',
|
|
BANNED: 'Account is banned',
|
|
INSUFFICIENT_ACCESS_LEVEL: 'Do not have permission to enter this environment',
|
|
INVALID_DATE: 'Invalid date',
|
|
INVALID_GENDER: 'Invalid gender',
|
|
INVALID_REGION: 'Invalid region',
|
|
INVALID_TIMEZONE: 'Invalid timezone',
|
|
INVALID_MII_DATA: 'Invalid mii data',
|
|
EMAIL_UNCHANGED: 'New email address must differ from current',
|
|
INVALID_EMAIL_TOKEN: 'Invalid email token',
|
|
MISSING_EMAIL_TOKEN: 'Missing email token'
|
|
} as const;
|
|
|
|
export type ApiErrorCodes = keyof typeof apiErrorCodes;
|
|
|
|
export const apiErrorCodeStatus: Record<ApiErrorCodes, number> = {
|
|
UNPARSABLE_ERROR: 500,
|
|
RATELIMITED: 429,
|
|
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,
|
|
PASSWORD_INVALID_LENGTH: 400,
|
|
PASSWORD_NOT_USERNAME: 400,
|
|
PASSWORD_NEEDS_CHARS: 400,
|
|
PASSWORDS_DO_NOT_MATCH: 400,
|
|
PASSWORD_REPEATED_CHARS: 400,
|
|
MIINAME_TOO_LONG: 400,
|
|
USERNAME_IN_USE: 400,
|
|
USERNAME_INVALID_CHARS: 400,
|
|
USERNAME_TOO_LONG: 400,
|
|
USERNAME_TOO_SHORT: 400,
|
|
INVALID_ACCESS_LEVEL: 400,
|
|
BANNED: 403,
|
|
INSUFFICIENT_ACCESS_LEVEL: 403,
|
|
INVALID_DATE: 400,
|
|
INVALID_GENDER: 400,
|
|
INVALID_REGION: 400,
|
|
INVALID_TIMEZONE: 400,
|
|
INVALID_MII_DATA: 400,
|
|
EMAIL_UNCHANGED: 400,
|
|
INVALID_EMAIL_TOKEN: 400,
|
|
MISSING_EMAIL_TOKEN: 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')
|
|
};
|
|
}
|