forked from Yara724/api
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import {
|
|
BadRequestException,
|
|
UnauthorizedException,
|
|
} from "@nestjs/common";
|
|
|
|
export enum CaptchaAuthErrorCode {
|
|
CAPTCHA_REQUIRED = "CAPTCHA_REQUIRED",
|
|
CAPTCHA_NOT_FOUND = "CAPTCHA_NOT_FOUND",
|
|
CAPTCHA_EXPIRED = "CAPTCHA_EXPIRED",
|
|
CAPTCHA_INVALID = "CAPTCHA_INVALID",
|
|
}
|
|
|
|
const messages: Record<CaptchaAuthErrorCode, string> = {
|
|
[CaptchaAuthErrorCode.CAPTCHA_REQUIRED]:
|
|
"Captcha is required. Request a new captcha image first.",
|
|
[CaptchaAuthErrorCode.CAPTCHA_NOT_FOUND]:
|
|
"Captcha id was not found. Request a new captcha image.",
|
|
[CaptchaAuthErrorCode.CAPTCHA_EXPIRED]:
|
|
"Captcha has expired. Request a new captcha image.",
|
|
[CaptchaAuthErrorCode.CAPTCHA_INVALID]: "Captcha is invalid.",
|
|
};
|
|
|
|
export function captchaAuthErrorBody(code: CaptchaAuthErrorCode) {
|
|
return {
|
|
code,
|
|
message: messages[code],
|
|
};
|
|
}
|
|
|
|
export function throwCaptchaAuthError(code: CaptchaAuthErrorCode): never {
|
|
if (
|
|
code === CaptchaAuthErrorCode.CAPTCHA_REQUIRED ||
|
|
code === CaptchaAuthErrorCode.CAPTCHA_NOT_FOUND
|
|
) {
|
|
throw new BadRequestException(captchaAuthErrorBody(code));
|
|
}
|
|
throw new UnauthorizedException(captchaAuthErrorBody(code));
|
|
}
|