forked from Yara724/api
39 lines
1.2 KiB
TypeScript
39 lines
1.2 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]:
|
|
"کپچا الزامی است. ابتدا تصویر کپچا را دریافت کنید.",
|
|
[CaptchaAuthErrorCode.CAPTCHA_NOT_FOUND]:
|
|
"شناسه کپچا یافت نشد. تصویر جدیدی درخواست دهید.",
|
|
[CaptchaAuthErrorCode.CAPTCHA_EXPIRED]:
|
|
"کپچا منقضی شده است. تصویر جدیدی درخواست دهید.",
|
|
[CaptchaAuthErrorCode.CAPTCHA_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));
|
|
}
|