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.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)); }