@naman_deep_singh/errors-utils 1.0.1 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +157 -82
- package/dist/cjs/constants/errorCodes.d.ts +32 -0
- package/dist/cjs/constants/errorCodes.js +34 -0
- package/dist/cjs/constants/errorMessages.d.ts +2 -0
- package/dist/cjs/constants/errorMessages.js +34 -0
- package/dist/cjs/constants/index.d.ts +2 -0
- package/dist/cjs/constants/index.js +18 -0
- package/dist/cjs/error/AppError.d.ts +11 -2
- package/dist/cjs/error/AppError.js +15 -2
- package/dist/cjs/error/BadRequestError.d.ts +2 -2
- package/dist/cjs/error/BadRequestError.js +4 -2
- package/dist/cjs/error/ConflictError.d.ts +2 -2
- package/dist/cjs/error/ConflictError.js +4 -2
- package/dist/cjs/error/CryptoIntegrityError.d.ts +4 -0
- package/dist/cjs/error/CryptoIntegrityError.js +11 -0
- package/dist/cjs/error/ForbiddenError.d.ts +2 -2
- package/dist/cjs/error/ForbiddenError.js +4 -2
- package/dist/cjs/error/HTTPError.d.ts +4 -3
- package/dist/cjs/error/HTTPError.js +3 -3
- package/dist/cjs/error/InternalServerError.d.ts +3 -2
- package/dist/cjs/error/InternalServerError.js +4 -2
- package/dist/cjs/error/NotFoundError.d.ts +2 -2
- package/dist/cjs/error/NotFoundError.js +4 -2
- package/dist/cjs/error/RateLimitError.d.ts +4 -0
- package/dist/cjs/error/RateLimitError.js +11 -0
- package/dist/cjs/error/TokenExpiredError.d.ts +4 -0
- package/dist/cjs/error/TokenExpiredError.js +12 -0
- package/dist/cjs/error/TokenMalformedError.d.ts +4 -0
- package/dist/cjs/error/TokenMalformedError.js +12 -0
- package/dist/cjs/error/TooManyRequestsError.d.ts +5 -0
- package/dist/cjs/error/TooManyRequestsError.js +12 -0
- package/dist/cjs/error/UnauthorizedError.d.ts +3 -2
- package/dist/cjs/error/UnauthorizedError.js +4 -2
- package/dist/cjs/error/ValidationError.d.ts +2 -2
- package/dist/cjs/error/ValidationError.js +4 -2
- package/dist/cjs/index.d.ts +10 -10
- package/dist/cjs/middleware/express/errorConverter.d.ts +2 -2
- package/dist/cjs/middleware/express/errorConverter.js +30 -2
- package/dist/cjs/middleware/express/errorHandler.d.ts +2 -2
- package/dist/cjs/middleware/express/errorHandler.js +9 -8
- package/dist/cjs/utils/mapAppErrorToResponder.d.ts +3 -3
- package/dist/cjs/utils/mapAppErrorToResponder.js +3 -1
- package/dist/esm/constants/errorCodes.d.ts +32 -0
- package/dist/esm/constants/errorCodes.js +31 -0
- package/dist/esm/constants/errorMessages.d.ts +2 -0
- package/dist/esm/constants/errorMessages.js +31 -0
- package/dist/esm/constants/index.d.ts +2 -0
- package/dist/esm/constants/index.js +2 -0
- package/dist/esm/error/AppError.d.ts +11 -2
- package/dist/esm/error/AppError.js +15 -2
- package/dist/esm/error/BadRequestError.d.ts +2 -2
- package/dist/esm/error/BadRequestError.js +5 -3
- package/dist/esm/error/ConflictError.d.ts +2 -2
- package/dist/esm/error/ConflictError.js +5 -3
- package/dist/esm/error/CryptoIntegrityError.d.ts +4 -0
- package/dist/esm/error/CryptoIntegrityError.js +7 -0
- package/dist/esm/error/ForbiddenError.d.ts +2 -2
- package/dist/esm/error/ForbiddenError.js +5 -3
- package/dist/esm/error/HTTPError.d.ts +4 -3
- package/dist/esm/error/HTTPError.js +4 -4
- package/dist/esm/error/InternalServerError.d.ts +3 -2
- package/dist/esm/error/InternalServerError.js +5 -3
- package/dist/esm/error/NotFoundError.d.ts +2 -2
- package/dist/esm/error/NotFoundError.js +5 -3
- package/dist/esm/error/RateLimitError.d.ts +4 -0
- package/dist/esm/error/RateLimitError.js +7 -0
- package/dist/esm/error/TokenExpiredError.d.ts +4 -0
- package/dist/esm/error/TokenExpiredError.js +8 -0
- package/dist/esm/error/TokenMalformedError.d.ts +4 -0
- package/dist/esm/error/TokenMalformedError.js +8 -0
- package/dist/esm/error/TooManyRequestsError.d.ts +5 -0
- package/dist/esm/error/TooManyRequestsError.js +8 -0
- package/dist/esm/error/UnauthorizedError.d.ts +3 -2
- package/dist/esm/error/UnauthorizedError.js +5 -3
- package/dist/esm/error/ValidationError.d.ts +2 -2
- package/dist/esm/error/ValidationError.js +5 -3
- package/dist/esm/index.d.ts +10 -10
- package/dist/esm/index.js +10 -10
- package/dist/esm/middleware/express/errorConverter.d.ts +2 -2
- package/dist/esm/middleware/express/errorConverter.js +31 -3
- package/dist/esm/middleware/express/errorHandler.d.ts +2 -2
- package/dist/esm/middleware/express/errorHandler.js +11 -10
- package/dist/esm/utils/mapAppErrorToResponder.d.ts +3 -3
- package/dist/esm/utils/mapAppErrorToResponder.js +4 -2
- package/dist/types/constants/errorCodes.d.ts +32 -0
- package/dist/types/constants/errorMessages.d.ts +2 -0
- package/dist/types/constants/index.d.ts +2 -0
- package/dist/types/error/AppError.d.ts +11 -2
- package/dist/types/error/BadRequestError.d.ts +2 -2
- package/dist/types/error/ConflictError.d.ts +2 -2
- package/dist/types/error/CryptoIntegrityError.d.ts +4 -0
- package/dist/types/error/ForbiddenError.d.ts +2 -2
- package/dist/types/error/HTTPError.d.ts +4 -3
- package/dist/types/error/InternalServerError.d.ts +3 -2
- package/dist/types/error/NotFoundError.d.ts +2 -2
- package/dist/types/error/RateLimitError.d.ts +4 -0
- package/dist/types/error/TokenExpiredError.d.ts +4 -0
- package/dist/types/error/TokenMalformedError.d.ts +4 -0
- package/dist/types/error/TooManyRequestsError.d.ts +5 -0
- package/dist/types/error/UnauthorizedError.d.ts +3 -2
- package/dist/types/error/ValidationError.d.ts +2 -2
- package/dist/types/index.d.ts +10 -10
- package/dist/types/middleware/express/errorConverter.d.ts +2 -2
- package/dist/types/middleware/express/errorHandler.d.ts +2 -2
- package/dist/types/utils/mapAppErrorToResponder.d.ts +3 -3
- package/package.json +1 -1
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TokenExpiredError = void 0;
|
|
4
|
+
const response_utils_1 = require("@naman_deep_singh/response-utils");
|
|
5
|
+
const constants_1 = require("src/constants");
|
|
6
|
+
const UnauthorizedError_1 = require("./UnauthorizedError");
|
|
7
|
+
class TokenExpiredError extends UnauthorizedError_1.UnauthorizedError {
|
|
8
|
+
constructor(details, cause) {
|
|
9
|
+
super(constants_1.ERROR_CODES.TOKEN_EXPIRED, response_utils_1.HTTP_STATUS.CLIENT_ERROR.UNAUTHORIZED, details, cause);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.TokenExpiredError = TokenExpiredError;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TokenMalformedError = void 0;
|
|
4
|
+
const response_utils_1 = require("@naman_deep_singh/response-utils");
|
|
5
|
+
const constants_1 = require("src/constants");
|
|
6
|
+
const UnauthorizedError_1 = require("./UnauthorizedError");
|
|
7
|
+
class TokenMalformedError extends UnauthorizedError_1.UnauthorizedError {
|
|
8
|
+
constructor(details, cause) {
|
|
9
|
+
super(constants_1.ERROR_CODES.TOKEN_INVALID, response_utils_1.HTTP_STATUS.CLIENT_ERROR.UNAUTHORIZED, details, cause);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.TokenMalformedError = TokenMalformedError;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TooManyRequestsError = void 0;
|
|
4
|
+
const response_utils_1 = require("@naman_deep_singh/response-utils");
|
|
5
|
+
const constants_1 = require("src/constants");
|
|
6
|
+
const HTTPError_1 = require("./HTTPError");
|
|
7
|
+
class TooManyRequestsError extends HTTPError_1.HTTPError {
|
|
8
|
+
constructor(errorCode = constants_1.ERROR_CODES.TOO_MANY_REQUESTS, details, cause) {
|
|
9
|
+
super(errorCode, response_utils_1.HTTP_STATUS.CLIENT_ERROR.TOO_MANY_REQUESTS, details, cause);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.TooManyRequestsError = TooManyRequestsError;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type ErrorCode } from 'src/constants';
|
|
2
|
+
import { HTTPError } from './HTTPError';
|
|
2
3
|
export declare class UnauthorizedError extends HTTPError {
|
|
3
|
-
constructor(
|
|
4
|
+
constructor(errorCodes?: ErrorCode, status?: number, details?: unknown, cause?: Error);
|
|
4
5
|
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.UnauthorizedError = void 0;
|
|
4
|
+
const response_utils_1 = require("@naman_deep_singh/response-utils");
|
|
5
|
+
const constants_1 = require("src/constants");
|
|
4
6
|
const HTTPError_1 = require("./HTTPError");
|
|
5
7
|
class UnauthorizedError extends HTTPError_1.HTTPError {
|
|
6
|
-
constructor(
|
|
7
|
-
super(
|
|
8
|
+
constructor(errorCodes = constants_1.ERROR_CODES.UNAUTHORIZED, status = response_utils_1.HTTP_STATUS.CLIENT_ERROR.UNAUTHORIZED, details, cause) {
|
|
9
|
+
super(errorCodes, status, details, cause);
|
|
8
10
|
}
|
|
9
11
|
}
|
|
10
12
|
exports.UnauthorizedError = UnauthorizedError;
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ValidationError = void 0;
|
|
4
|
+
const response_utils_1 = require("@naman_deep_singh/response-utils");
|
|
5
|
+
const constants_1 = require("src/constants");
|
|
4
6
|
const HTTPError_1 = require("./HTTPError");
|
|
5
7
|
class ValidationError extends HTTPError_1.HTTPError {
|
|
6
|
-
constructor(
|
|
7
|
-
super(
|
|
8
|
+
constructor(details, cause) {
|
|
9
|
+
super(constants_1.ERROR_CODES.VALIDATION_FAILED, response_utils_1.HTTP_STATUS.CLIENT_ERROR.UNPROCESSABLE_ENTITY, details, cause);
|
|
8
10
|
}
|
|
9
11
|
}
|
|
10
12
|
exports.ValidationError = ValidationError;
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
7
|
-
export * from
|
|
8
|
-
export * from
|
|
9
|
-
export * from
|
|
10
|
-
export * from
|
|
1
|
+
export * from './error/AppError';
|
|
2
|
+
export * from './error/HTTPError';
|
|
3
|
+
export * from './error/BadRequestError';
|
|
4
|
+
export * from './error/UnauthorizedError';
|
|
5
|
+
export * from './error/ForbiddenError';
|
|
6
|
+
export * from './error/NotFoundError';
|
|
7
|
+
export * from './error/ConflictError';
|
|
8
|
+
export * from './error/ValidationError';
|
|
9
|
+
export * from './error/InternalServerError';
|
|
10
|
+
export * from './middleware/express';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Request, Response
|
|
2
|
-
export declare function errorConverter(err:
|
|
1
|
+
import type { NextFunction, Request, Response } from 'express';
|
|
2
|
+
export declare function errorConverter(err: unknown, _req: Request, _res: Response, next: NextFunction): void;
|
|
@@ -1,11 +1,39 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.errorConverter = errorConverter;
|
|
4
|
+
const response_utils_1 = require("@naman_deep_singh/response-utils");
|
|
5
|
+
const constants_1 = require("../../constants");
|
|
4
6
|
const AppError_1 = require("../../error/AppError");
|
|
5
|
-
function errorConverter(err,
|
|
7
|
+
function errorConverter(err, _req, _res, next) {
|
|
8
|
+
// 1. Already a known AppError → pass through
|
|
6
9
|
if (err instanceof AppError_1.AppError) {
|
|
7
10
|
return next(err);
|
|
8
11
|
}
|
|
9
|
-
|
|
12
|
+
let code = constants_1.ERROR_CODES.INTERNAL_SERVER_ERROR;
|
|
13
|
+
let statusCode = response_utils_1.HTTP_STATUS.SERVER_ERROR.INTERNAL_SERVER_ERROR;
|
|
14
|
+
let details = undefined;
|
|
15
|
+
let cause = undefined;
|
|
16
|
+
// 2. Handle unknown objects safely
|
|
17
|
+
if (typeof err === 'object' && err !== null) {
|
|
18
|
+
const e = err;
|
|
19
|
+
if (typeof e.code === 'string' && e.code in constants_1.ERROR_CODES) {
|
|
20
|
+
code = e.code;
|
|
21
|
+
}
|
|
22
|
+
if (typeof e.statusCode === 'number') {
|
|
23
|
+
statusCode = e.statusCode;
|
|
24
|
+
}
|
|
25
|
+
if ('details' in e) {
|
|
26
|
+
details = e.details;
|
|
27
|
+
}
|
|
28
|
+
if (err instanceof Error) {
|
|
29
|
+
cause = err;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
// 3. Handle string errors
|
|
33
|
+
if (typeof err === 'string') {
|
|
34
|
+
details = err;
|
|
35
|
+
}
|
|
36
|
+
// 4. Convert to AppError
|
|
37
|
+
const convertedError = new AppError_1.AppError(code, statusCode, details, cause);
|
|
10
38
|
next(convertedError);
|
|
11
39
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { Request, Response
|
|
2
|
-
export declare function expressErrorHandler(err: unknown,
|
|
1
|
+
import type { NextFunction, Request, Response } from 'express';
|
|
2
|
+
export declare function expressErrorHandler(err: unknown, _req: Request, res: Response, _next: NextFunction): any;
|
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.expressErrorHandler = expressErrorHandler;
|
|
4
|
-
const AppError_1 = require("../../error/AppError");
|
|
5
4
|
const response_utils_1 = require("@naman_deep_singh/response-utils");
|
|
5
|
+
const constants_1 = require("../../constants");
|
|
6
|
+
const AppError_1 = require("../../error/AppError");
|
|
6
7
|
const mapAppErrorToResponder_1 = require("../../utils/mapAppErrorToResponder");
|
|
7
|
-
function expressErrorHandler(err,
|
|
8
|
+
function expressErrorHandler(err, _req, res, _next) {
|
|
8
9
|
const responder = new response_utils_1.ExpressResponder({}, res);
|
|
9
|
-
//
|
|
10
|
+
// 1. Known operational error
|
|
10
11
|
if (err instanceof AppError_1.AppError) {
|
|
11
12
|
return (0, mapAppErrorToResponder_1.mapAppErrorToResponder)(responder, err);
|
|
12
13
|
}
|
|
13
|
-
//
|
|
14
|
-
console.error(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
// 2. Log unexpected errors (never expose internals in prod)
|
|
15
|
+
console.error('UNEXPECTED ERROR:', err);
|
|
16
|
+
// 3. Normalize unknown error → AppError
|
|
17
|
+
const internalError = new AppError_1.AppError(constants_1.ERROR_CODES.INTERNAL_SERVER_ERROR, 500, process.env.NODE_ENV === 'production' ? undefined : err, err instanceof Error ? err : undefined);
|
|
18
|
+
return (0, mapAppErrorToResponder_1.mapAppErrorToResponder)(responder, internalError);
|
|
18
19
|
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { ExpressResponder } from
|
|
2
|
-
import { AppError } from
|
|
3
|
-
export declare function mapAppErrorToResponder(responder: ExpressResponder<
|
|
1
|
+
import type { ExpressResponder } from '@naman_deep_singh/response-utils';
|
|
2
|
+
import type { AppError } from 'src/error/AppError';
|
|
3
|
+
export declare function mapAppErrorToResponder(responder: ExpressResponder<unknown>, err: AppError): any;
|
|
@@ -15,7 +15,9 @@ function mapAppErrorToResponder(responder, err) {
|
|
|
15
15
|
case response_utils_1.HTTP_STATUS.CLIENT_ERROR.CONFLICT:
|
|
16
16
|
return responder.conflict(err.message);
|
|
17
17
|
case response_utils_1.HTTP_STATUS.CLIENT_ERROR.UNPROCESSABLE_ENTITY:
|
|
18
|
-
return responder.unprocessableEntity(err.message, {
|
|
18
|
+
return responder.unprocessableEntity(err.message, {
|
|
19
|
+
details: err.details,
|
|
20
|
+
});
|
|
19
21
|
case response_utils_1.HTTP_STATUS.CLIENT_ERROR.TOO_MANY_REQUESTS:
|
|
20
22
|
return responder.tooManyRequests(err.message);
|
|
21
23
|
default:
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export declare const ERROR_CODES: {
|
|
2
|
+
readonly BAD_REQUEST: "BAD_REQUEST";
|
|
3
|
+
readonly UNAUTHORIZED: "UNAUTHORIZED";
|
|
4
|
+
readonly FORBIDDEN: "FORBIDDEN";
|
|
5
|
+
readonly NOT_FOUND: "NOT_FOUND";
|
|
6
|
+
readonly CONFLICT: "CONFLICT";
|
|
7
|
+
readonly VALIDATION_FAILED: "VALIDATION_FAILED";
|
|
8
|
+
readonly RATE_LIMIT_EXCEEDED: "RATE_LIMIT_EXCEEDED";
|
|
9
|
+
readonly INTERNAL_SERVER_ERROR: "INTERNAL_SERVER_ERROR";
|
|
10
|
+
readonly CRYPTO_INTEGRITY_ERROR: "CRYPTO_INTEGRITY_ERROR";
|
|
11
|
+
readonly TOO_MANY_REQUESTS: "TOO_MANY_REQUESTS";
|
|
12
|
+
readonly TOKEN_EXPIRED: "TOKEN_EXPIRED";
|
|
13
|
+
readonly TOKEN_INVALID: "TOKEN_INVALID";
|
|
14
|
+
readonly TOKEN_NOT_PROVIDED: "TOKEN_NOT_PROVIDED";
|
|
15
|
+
readonly TOKEN_NOT_FOUND: "TOKEN_NOT_FOUND";
|
|
16
|
+
readonly TOKEN_NOT_CREATED: "TOKEN_NOT_CREATED";
|
|
17
|
+
readonly TOKEN_NOT_UPDATED: "TOKEN_NOT_UPDATED";
|
|
18
|
+
readonly TOKEN_NOT_DELETED: "TOKEN_NOT_DELETED";
|
|
19
|
+
readonly TOKEN_NOT_VALIDATED: "TOKEN_NOT_VALIDATED";
|
|
20
|
+
readonly TOKEN_NOT_REFRESHED: "TOKEN_NOT_REFRESHED";
|
|
21
|
+
readonly TOKEN_NOT_REVOKED: "TOKEN_NOT_REVOKED";
|
|
22
|
+
readonly TOKEN_NOT_BLACKLISTED: "TOKEN_NOT_BLACKLISTED";
|
|
23
|
+
readonly TOKEN_NOT_WHITELISTED: "TOKEN_NOT_WHITELISTED";
|
|
24
|
+
readonly TOKEN_NOT_DECODED: "TOKEN_NOT_DECODED";
|
|
25
|
+
readonly TOKEN_NOT_ENCODED: "TOKEN_NOT_ENCODED";
|
|
26
|
+
readonly TOKEN_NOT_SIGNED: "TOKEN_NOT_SIGNED";
|
|
27
|
+
readonly TOKEN_NOT_VERIFIED: "TOKEN_NOT_VERIFIED";
|
|
28
|
+
readonly TOKEN_NOT_DECRYPTED: "TOKEN_NOT_DECRYPTED";
|
|
29
|
+
readonly TOKEN_NOT_ENCRYPTED: "TOKEN_NOT_ENCRYPTED";
|
|
30
|
+
readonly TOKEN_NOT_GENERATED: "TOKEN_NOT_GENERATED";
|
|
31
|
+
};
|
|
32
|
+
export type ErrorCode = keyof typeof ERROR_CODES;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export const ERROR_CODES = {
|
|
2
|
+
BAD_REQUEST: 'BAD_REQUEST',
|
|
3
|
+
UNAUTHORIZED: 'UNAUTHORIZED',
|
|
4
|
+
FORBIDDEN: 'FORBIDDEN',
|
|
5
|
+
NOT_FOUND: 'NOT_FOUND',
|
|
6
|
+
CONFLICT: 'CONFLICT',
|
|
7
|
+
VALIDATION_FAILED: 'VALIDATION_FAILED',
|
|
8
|
+
RATE_LIMIT_EXCEEDED: 'RATE_LIMIT_EXCEEDED',
|
|
9
|
+
INTERNAL_SERVER_ERROR: 'INTERNAL_SERVER_ERROR',
|
|
10
|
+
CRYPTO_INTEGRITY_ERROR: 'CRYPTO_INTEGRITY_ERROR',
|
|
11
|
+
TOO_MANY_REQUESTS: 'TOO_MANY_REQUESTS',
|
|
12
|
+
TOKEN_EXPIRED: 'TOKEN_EXPIRED',
|
|
13
|
+
TOKEN_INVALID: 'TOKEN_INVALID',
|
|
14
|
+
TOKEN_NOT_PROVIDED: 'TOKEN_NOT_PROVIDED',
|
|
15
|
+
TOKEN_NOT_FOUND: 'TOKEN_NOT_FOUND',
|
|
16
|
+
TOKEN_NOT_CREATED: 'TOKEN_NOT_CREATED',
|
|
17
|
+
TOKEN_NOT_UPDATED: 'TOKEN_NOT_UPDATED',
|
|
18
|
+
TOKEN_NOT_DELETED: 'TOKEN_NOT_DELETED',
|
|
19
|
+
TOKEN_NOT_VALIDATED: 'TOKEN_NOT_VALIDATED',
|
|
20
|
+
TOKEN_NOT_REFRESHED: 'TOKEN_NOT_REFRESHED',
|
|
21
|
+
TOKEN_NOT_REVOKED: 'TOKEN_NOT_REVOKED',
|
|
22
|
+
TOKEN_NOT_BLACKLISTED: 'TOKEN_NOT_BLACKLISTED',
|
|
23
|
+
TOKEN_NOT_WHITELISTED: 'TOKEN_NOT_WHITELISTED',
|
|
24
|
+
TOKEN_NOT_DECODED: 'TOKEN_NOT_DECODED',
|
|
25
|
+
TOKEN_NOT_ENCODED: 'TOKEN_NOT_ENCODED',
|
|
26
|
+
TOKEN_NOT_SIGNED: 'TOKEN_NOT_SIGNED',
|
|
27
|
+
TOKEN_NOT_VERIFIED: 'TOKEN_NOT_VERIFIED',
|
|
28
|
+
TOKEN_NOT_DECRYPTED: 'TOKEN_NOT_DECRYPTED',
|
|
29
|
+
TOKEN_NOT_ENCRYPTED: 'TOKEN_NOT_ENCRYPTED',
|
|
30
|
+
TOKEN_NOT_GENERATED: 'TOKEN_NOT_GENERATED',
|
|
31
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export const ERROR_MESSAGES = {
|
|
2
|
+
BAD_REQUEST: 'Bad request',
|
|
3
|
+
UNAUTHORIZED: 'Unauthorized',
|
|
4
|
+
FORBIDDEN: 'Forbidden',
|
|
5
|
+
NOT_FOUND: 'Resource not found',
|
|
6
|
+
CONFLICT: 'Conflict occurred',
|
|
7
|
+
VALIDATION_FAILED: 'Validation failed',
|
|
8
|
+
RATE_LIMIT_EXCEEDED: 'Too many requests',
|
|
9
|
+
INTERNAL_SERVER_ERROR: 'Internal server error',
|
|
10
|
+
CRYPTO_INTEGRITY_ERROR: 'Crypto integrity error',
|
|
11
|
+
TOO_MANY_REQUESTS: 'Too many requests',
|
|
12
|
+
TOKEN_EXPIRED: 'Token expired',
|
|
13
|
+
TOKEN_INVALID: 'Invalid token',
|
|
14
|
+
TOKEN_NOT_PROVIDED: 'Token not provided',
|
|
15
|
+
TOKEN_NOT_FOUND: 'Token not found',
|
|
16
|
+
TOKEN_NOT_CREATED: 'Token not created',
|
|
17
|
+
TOKEN_NOT_UPDATED: 'Token not updated',
|
|
18
|
+
TOKEN_NOT_DELETED: 'Token not deleted',
|
|
19
|
+
TOKEN_NOT_VALIDATED: 'Token not validated',
|
|
20
|
+
TOKEN_NOT_REFRESHED: 'Token not refreshed',
|
|
21
|
+
TOKEN_NOT_REVOKED: 'Token not revoked',
|
|
22
|
+
TOKEN_NOT_BLACKLISTED: 'Token not blacklisted',
|
|
23
|
+
TOKEN_NOT_WHITELISTED: 'Token not whitelisted',
|
|
24
|
+
TOKEN_NOT_DECODED: 'Token not decoded',
|
|
25
|
+
TOKEN_NOT_ENCODED: 'Token not encoded',
|
|
26
|
+
TOKEN_NOT_SIGNED: 'Token not signed',
|
|
27
|
+
TOKEN_NOT_VERIFIED: 'Token not verified',
|
|
28
|
+
TOKEN_NOT_DECRYPTED: 'Token not decrypted',
|
|
29
|
+
TOKEN_NOT_ENCRYPTED: 'Token not encrypted',
|
|
30
|
+
TOKEN_NOT_GENERATED: 'Token not generated',
|
|
31
|
+
};
|
|
@@ -1,6 +1,15 @@
|
|
|
1
|
+
import { type ErrorCode } from '../constants';
|
|
1
2
|
export declare class AppError extends Error {
|
|
2
3
|
statusCode: number;
|
|
3
4
|
isOperational: boolean;
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
code: ErrorCode;
|
|
6
|
+
details?: unknown;
|
|
7
|
+
cause?: Error;
|
|
8
|
+
constructor(code: ErrorCode, statusCode?: number, details?: unknown, cause?: Error);
|
|
9
|
+
toJSON(): {
|
|
10
|
+
success: boolean;
|
|
11
|
+
code: "BAD_REQUEST" | "UNAUTHORIZED" | "FORBIDDEN" | "NOT_FOUND" | "CONFLICT" | "VALIDATION_FAILED" | "RATE_LIMIT_EXCEEDED" | "INTERNAL_SERVER_ERROR" | "CRYPTO_INTEGRITY_ERROR" | "TOO_MANY_REQUESTS" | "TOKEN_EXPIRED" | "TOKEN_INVALID" | "TOKEN_NOT_PROVIDED" | "TOKEN_NOT_FOUND" | "TOKEN_NOT_CREATED" | "TOKEN_NOT_UPDATED" | "TOKEN_NOT_DELETED" | "TOKEN_NOT_VALIDATED" | "TOKEN_NOT_REFRESHED" | "TOKEN_NOT_REVOKED" | "TOKEN_NOT_BLACKLISTED" | "TOKEN_NOT_WHITELISTED" | "TOKEN_NOT_DECODED" | "TOKEN_NOT_ENCODED" | "TOKEN_NOT_SIGNED" | "TOKEN_NOT_VERIFIED" | "TOKEN_NOT_DECRYPTED" | "TOKEN_NOT_ENCRYPTED" | "TOKEN_NOT_GENERATED";
|
|
12
|
+
message: string;
|
|
13
|
+
details: {} | null;
|
|
14
|
+
};
|
|
6
15
|
}
|
|
@@ -1,9 +1,22 @@
|
|
|
1
|
+
import { HTTP_STATUS } from '@naman_deep_singh/response-utils';
|
|
2
|
+
import { ERROR_MESSAGES } from '../constants';
|
|
1
3
|
export class AppError extends Error {
|
|
2
|
-
constructor(
|
|
3
|
-
super(
|
|
4
|
+
constructor(code, statusCode = HTTP_STATUS.SERVER_ERROR.INTERNAL_SERVER_ERROR, details, cause) {
|
|
5
|
+
super(ERROR_MESSAGES[code]); // message comes from mapping
|
|
6
|
+
this.code = code;
|
|
4
7
|
this.statusCode = statusCode;
|
|
5
8
|
this.isOperational = true;
|
|
6
9
|
this.details = details;
|
|
10
|
+
if (cause)
|
|
11
|
+
this.cause = cause;
|
|
7
12
|
Error.captureStackTrace(this, this.constructor);
|
|
8
13
|
}
|
|
14
|
+
toJSON() {
|
|
15
|
+
return {
|
|
16
|
+
success: false,
|
|
17
|
+
code: this.code,
|
|
18
|
+
message: this.message,
|
|
19
|
+
details: this.details ?? null,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
9
22
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { HTTP_STATUS } from '@naman_deep_singh/response-utils';
|
|
2
|
+
import { ERROR_CODES } from '../constants';
|
|
3
|
+
import { HTTPError } from './HTTPError';
|
|
2
4
|
export class BadRequestError extends HTTPError {
|
|
3
|
-
constructor(
|
|
4
|
-
super(
|
|
5
|
+
constructor(details, cause) {
|
|
6
|
+
super(ERROR_CODES.BAD_REQUEST, HTTP_STATUS.CLIENT_ERROR.BAD_REQUEST, details, cause);
|
|
5
7
|
}
|
|
6
8
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { HTTP_STATUS } from '@naman_deep_singh/response-utils';
|
|
2
|
+
import { ERROR_CODES } from 'src/constants';
|
|
3
|
+
import { HTTPError } from './HTTPError';
|
|
2
4
|
export class ConflictError extends HTTPError {
|
|
3
|
-
constructor(
|
|
4
|
-
super(
|
|
5
|
+
constructor(details, cause) {
|
|
6
|
+
super(ERROR_CODES.CONFLICT, HTTP_STATUS.CLIENT_ERROR.CONFLICT, details, cause);
|
|
5
7
|
}
|
|
6
8
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ERROR_CODES } from 'src/constants';
|
|
2
|
+
import { InternalServerError } from './InternalServerError';
|
|
3
|
+
export class CryptoIntegrityError extends InternalServerError {
|
|
4
|
+
constructor(details, cause) {
|
|
5
|
+
super(ERROR_CODES.CRYPTO_INTEGRITY_ERROR, details, cause);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { HTTP_STATUS } from '@naman_deep_singh/response-utils';
|
|
2
|
+
import { ERROR_CODES } from 'src/constants';
|
|
3
|
+
import { HTTPError } from './HTTPError';
|
|
2
4
|
export class ForbiddenError extends HTTPError {
|
|
3
|
-
constructor(
|
|
4
|
-
super(
|
|
5
|
+
constructor(details, cause) {
|
|
6
|
+
super(ERROR_CODES.FORBIDDEN, HTTP_STATUS.CLIENT_ERROR.FORBIDDEN, details, cause);
|
|
5
7
|
}
|
|
6
8
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { ErrorCode } from '../constants';
|
|
2
|
+
import { AppError } from './AppError';
|
|
2
3
|
export declare class HTTPError extends AppError {
|
|
3
|
-
status:
|
|
4
|
-
constructor(
|
|
4
|
+
status: 'fail' | 'error';
|
|
5
|
+
constructor(errorCode: ErrorCode, statusCode: number, details?: unknown, cause?: Error);
|
|
5
6
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { AppError } from
|
|
1
|
+
import { AppError } from './AppError';
|
|
2
2
|
export class HTTPError extends AppError {
|
|
3
|
-
constructor(
|
|
4
|
-
super(
|
|
5
|
-
this.status =
|
|
3
|
+
constructor(errorCode, statusCode, details, cause) {
|
|
4
|
+
super(errorCode, statusCode, details, cause);
|
|
5
|
+
this.status = statusCode >= 400 && statusCode < 500 ? 'fail' : 'error';
|
|
6
6
|
}
|
|
7
7
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type ErrorCode } from 'src/constants';
|
|
2
|
+
import { HTTPError } from './HTTPError';
|
|
2
3
|
export declare class InternalServerError extends HTTPError {
|
|
3
|
-
constructor(
|
|
4
|
+
constructor(errorCode?: ErrorCode, details?: unknown, cause?: Error);
|
|
4
5
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { HTTP_STATUS } from '@naman_deep_singh/response-utils';
|
|
2
|
+
import { ERROR_CODES } from 'src/constants';
|
|
3
|
+
import { HTTPError } from './HTTPError';
|
|
2
4
|
export class InternalServerError extends HTTPError {
|
|
3
|
-
constructor(
|
|
4
|
-
super(
|
|
5
|
+
constructor(errorCode = ERROR_CODES.INTERNAL_SERVER_ERROR, details, cause) {
|
|
6
|
+
super(errorCode, HTTP_STATUS.SERVER_ERROR.INTERNAL_SERVER_ERROR, details, cause);
|
|
5
7
|
}
|
|
6
8
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { HTTP_STATUS } from '@naman_deep_singh/response-utils';
|
|
2
|
+
import { ERROR_CODES } from 'src/constants';
|
|
3
|
+
import { HTTPError } from './HTTPError';
|
|
2
4
|
export class NotFoundError extends HTTPError {
|
|
3
|
-
constructor(
|
|
4
|
-
super(
|
|
5
|
+
constructor(details, cause) {
|
|
6
|
+
super(ERROR_CODES.NOT_FOUND, HTTP_STATUS.CLIENT_ERROR.NOT_FOUND, details, cause);
|
|
5
7
|
}
|
|
6
8
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ERROR_CODES } from 'src/constants';
|
|
2
|
+
import { TooManyRequestsError } from './TooManyRequestsError';
|
|
3
|
+
export class RateLimitError extends TooManyRequestsError {
|
|
4
|
+
constructor(details, cause) {
|
|
5
|
+
super(ERROR_CODES.RATE_LIMIT_EXCEEDED, details, cause);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { HTTP_STATUS } from '@naman_deep_singh/response-utils';
|
|
2
|
+
import { ERROR_CODES } from 'src/constants';
|
|
3
|
+
import { UnauthorizedError } from './UnauthorizedError';
|
|
4
|
+
export class TokenExpiredError extends UnauthorizedError {
|
|
5
|
+
constructor(details, cause) {
|
|
6
|
+
super(ERROR_CODES.TOKEN_EXPIRED, HTTP_STATUS.CLIENT_ERROR.UNAUTHORIZED, details, cause);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { HTTP_STATUS } from '@naman_deep_singh/response-utils';
|
|
2
|
+
import { ERROR_CODES } from 'src/constants';
|
|
3
|
+
import { UnauthorizedError } from './UnauthorizedError';
|
|
4
|
+
export class TokenMalformedError extends UnauthorizedError {
|
|
5
|
+
constructor(details, cause) {
|
|
6
|
+
super(ERROR_CODES.TOKEN_INVALID, HTTP_STATUS.CLIENT_ERROR.UNAUTHORIZED, details, cause);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { HTTP_STATUS } from '@naman_deep_singh/response-utils';
|
|
2
|
+
import { ERROR_CODES } from 'src/constants';
|
|
3
|
+
import { HTTPError } from './HTTPError';
|
|
4
|
+
export class TooManyRequestsError extends HTTPError {
|
|
5
|
+
constructor(errorCode = ERROR_CODES.TOO_MANY_REQUESTS, details, cause) {
|
|
6
|
+
super(errorCode, HTTP_STATUS.CLIENT_ERROR.TOO_MANY_REQUESTS, details, cause);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type ErrorCode } from 'src/constants';
|
|
2
|
+
import { HTTPError } from './HTTPError';
|
|
2
3
|
export declare class UnauthorizedError extends HTTPError {
|
|
3
|
-
constructor(
|
|
4
|
+
constructor(errorCodes?: ErrorCode, status?: number, details?: unknown, cause?: Error);
|
|
4
5
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { HTTP_STATUS } from '@naman_deep_singh/response-utils';
|
|
2
|
+
import { ERROR_CODES } from 'src/constants';
|
|
3
|
+
import { HTTPError } from './HTTPError';
|
|
2
4
|
export class UnauthorizedError extends HTTPError {
|
|
3
|
-
constructor(
|
|
4
|
-
super(
|
|
5
|
+
constructor(errorCodes = ERROR_CODES.UNAUTHORIZED, status = HTTP_STATUS.CLIENT_ERROR.UNAUTHORIZED, details, cause) {
|
|
6
|
+
super(errorCodes, status, details, cause);
|
|
5
7
|
}
|
|
6
8
|
}
|