1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| export enum HttpStatusCode { OK = 200, CREATED = 201, ACCEPTED = 202, NO_CONTENT = 204, PARTIAL_CONTENT = 206,
MULTIPLE_CHOICES = 300, MOVED_PERMANENTLY = 301, FOUND = 302, SEE_OTHER = 303, NOT_MODIFIED = 304, USE_PROXY = 305, TEMPORARY_REDIRECT = 307, PERMANENT_REDIRECT = 308,
BAD_REQUEST = 400, UNAUTHORIZED = 401, PAYMENT_REQUIRED = 402, FORBIDDEN = 403, NOT_FOUND = 404, METHOD_NOT_ALLOWED = 405, NOT_ACCEPTABLE = 406, PROXY_AUTHENTICATION_REQUIRED = 407, REQUEST_TIMEOUT = 408, CONFLICT = 409,
GONE = 410, LENGTH_REQUIRED = 411, PRECONDITION_FAILED = 412, PAYLOAD_TOO_LARGE = 413, URI_TOO_LONG = 414, UNSUPPORTED_MEDIA_TYPE = 415, RANGE_NOT_SATISFIABLE = 416, EXPECTATION_FAILED = 417,
UNPROCESSABLE_ENTITY = 422, TOO_MANY_REQUESTS = 429,
INTERNAL_SERVER_ERROR = 500, NOT_IMPLEMENTED = 501, BAD_GATEWAY = 502, SERVICE_UNAVAILABLE = 503, GATEWAY_TIMEOUT = 504, HTTP_VERSION_NOT_SUPPORTED = 505,
UNKNOWN = 999, }
import { HttpException } from "@nestjs/common"; import { HttpStatusCode } from "../enum/http.enum";
export class ApiException extends HttpException { private errCode: number; constructor( msg: string | Record<string, any>, errCode: HttpStatusCode = HttpStatusCode.OK ) { switch (errCode) { case HttpStatusCode.UNAUTHORIZED: super(msg, HttpStatusCode.OK); this.errCode = HttpStatusCode.UNAUTHORIZED; break; case HttpStatusCode.FORBIDDEN: super(msg, HttpStatusCode.OK); this.errCode = HttpStatusCode.FORBIDDEN; break; case HttpStatusCode.NOT_FOUND: super(msg, HttpStatusCode.OK); this.errCode = HttpStatusCode.NOT_FOUND; break; default: super(msg, errCode ?? HttpStatusCode.OK); this.errCode = HttpStatusCode.INTERNAL_SERVER_ERROR; } } getErrCode(): number { return this.errCode; } }
|