@naman_deep_singh/errors-utils 1.0.0
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/dist/error/AppError.d.ts +6 -0
- package/dist/error/AppError.js +13 -0
- package/dist/error/BadRequestError.d.ts +4 -0
- package/dist/error/BadRequestError.js +10 -0
- package/dist/error/ConflictError.d.ts +4 -0
- package/dist/error/ConflictError.js +10 -0
- package/dist/error/ForbiddenError.d.ts +4 -0
- package/dist/error/ForbiddenError.js +10 -0
- package/dist/error/HTTPError.d.ts +5 -0
- package/dist/error/HTTPError.js +11 -0
- package/dist/error/InternalServerError.d.ts +4 -0
- package/dist/error/InternalServerError.js +10 -0
- package/dist/error/NotFoundError.d.ts +4 -0
- package/dist/error/NotFoundError.js +10 -0
- package/dist/error/UnauthorizedError.d.ts +4 -0
- package/dist/error/UnauthorizedError.js +10 -0
- package/dist/error/ValidationError.d.ts +4 -0
- package/dist/error/ValidationError.js +10 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +27 -0
- package/dist/middleware/errorConverter.d.ts +2 -0
- package/dist/middleware/errorConverter.js +11 -0
- package/dist/middleware/errorHandler.d.ts +2 -0
- package/dist/middleware/errorHandler.js +18 -0
- package/dist/utils/mapAppErrorToResponder.d.ts +3 -0
- package/dist/utils/mapAppErrorToResponder.js +25 -0
- package/package.json +24 -0
- package/src/error/AppError.ts +16 -0
- package/src/error/BadRequestError.ts +7 -0
- package/src/error/ConflictError.ts +7 -0
- package/src/error/ForbiddenError.ts +7 -0
- package/src/error/HTTPError.ts +11 -0
- package/src/error/InternalServerError.ts +7 -0
- package/src/error/NotFoundError.ts +7 -0
- package/src/error/UnauthorizedError.ts +7 -0
- package/src/error/ValidationError.ts +7 -0
- package/src/index.ts +12 -0
- package/src/middleware/errorConverter.ts +16 -0
- package/src/middleware/errorHandler.ts +25 -0
- package/src/utils/mapAppErrorToResponder.ts +31 -0
- package/tsconfig.json +21 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AppError = void 0;
|
|
4
|
+
class AppError extends Error {
|
|
5
|
+
constructor(message, statusCode = 500, details) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.statusCode = statusCode;
|
|
8
|
+
this.isOperational = true;
|
|
9
|
+
this.details = details;
|
|
10
|
+
Error.captureStackTrace(this, this.constructor);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.AppError = AppError;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BadRequestError = void 0;
|
|
4
|
+
const HTTPError_1 = require("./HTTPError");
|
|
5
|
+
class BadRequestError extends HTTPError_1.HTTPError {
|
|
6
|
+
constructor(message = "Bad Request", details) {
|
|
7
|
+
super(message, 400, details);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.BadRequestError = BadRequestError;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ConflictError = void 0;
|
|
4
|
+
const HTTPError_1 = require("./HTTPError");
|
|
5
|
+
class ConflictError extends HTTPError_1.HTTPError {
|
|
6
|
+
constructor(message = "Conflict", details) {
|
|
7
|
+
super(message, 409, details);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.ConflictError = ConflictError;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ForbiddenError = void 0;
|
|
4
|
+
const HTTPError_1 = require("./HTTPError");
|
|
5
|
+
class ForbiddenError extends HTTPError_1.HTTPError {
|
|
6
|
+
constructor(message = "Forbidden", details) {
|
|
7
|
+
super(message, 403, details);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.ForbiddenError = ForbiddenError;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HTTPError = void 0;
|
|
4
|
+
const AppError_1 = require("./AppError");
|
|
5
|
+
class HTTPError extends AppError_1.AppError {
|
|
6
|
+
constructor(message, statusCode, details) {
|
|
7
|
+
super(message, statusCode, details);
|
|
8
|
+
this.status = `${statusCode}`.startsWith("4") ? "fail" : "error";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.HTTPError = HTTPError;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InternalServerError = void 0;
|
|
4
|
+
const HTTPError_1 = require("./HTTPError");
|
|
5
|
+
class InternalServerError extends HTTPError_1.HTTPError {
|
|
6
|
+
constructor(message = "Internal Server Error", details) {
|
|
7
|
+
super(message, 500, details);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.InternalServerError = InternalServerError;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotFoundError = void 0;
|
|
4
|
+
const HTTPError_1 = require("./HTTPError");
|
|
5
|
+
class NotFoundError extends HTTPError_1.HTTPError {
|
|
6
|
+
constructor(message = "Not Found", details) {
|
|
7
|
+
super(message, 404, details);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.NotFoundError = NotFoundError;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UnauthorizedError = void 0;
|
|
4
|
+
const HTTPError_1 = require("./HTTPError");
|
|
5
|
+
class UnauthorizedError extends HTTPError_1.HTTPError {
|
|
6
|
+
constructor(message = "Unauthorized", details) {
|
|
7
|
+
super(message, 401, details);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.UnauthorizedError = UnauthorizedError;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ValidationError = void 0;
|
|
4
|
+
const HTTPError_1 = require("./HTTPError");
|
|
5
|
+
class ValidationError extends HTTPError_1.HTTPError {
|
|
6
|
+
constructor(message = "Validation Error", details) {
|
|
7
|
+
super(message, 422, details);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.ValidationError = ValidationError;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
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/errorConverter";
|
|
11
|
+
export * from "./middleware/errorHandler";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./error/AppError"), exports);
|
|
18
|
+
__exportStar(require("./error/HTTPError"), exports);
|
|
19
|
+
__exportStar(require("./error/BadRequestError"), exports);
|
|
20
|
+
__exportStar(require("./error/UnauthorizedError"), exports);
|
|
21
|
+
__exportStar(require("./error/ForbiddenError"), exports);
|
|
22
|
+
__exportStar(require("./error/NotFoundError"), exports);
|
|
23
|
+
__exportStar(require("./error/ConflictError"), exports);
|
|
24
|
+
__exportStar(require("./error/ValidationError"), exports);
|
|
25
|
+
__exportStar(require("./error/InternalServerError"), exports);
|
|
26
|
+
__exportStar(require("./middleware/errorConverter"), exports);
|
|
27
|
+
__exportStar(require("./middleware/errorHandler"), exports);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.errorConverter = errorConverter;
|
|
4
|
+
const AppError_1 = require("../error/AppError");
|
|
5
|
+
function errorConverter(err, req, res, next) {
|
|
6
|
+
if (err instanceof AppError_1.AppError) {
|
|
7
|
+
return next(err);
|
|
8
|
+
}
|
|
9
|
+
const convertedError = new AppError_1.AppError(err.message || "Internal Error", err.statusCode || 500, err.details || undefined);
|
|
10
|
+
next(convertedError);
|
|
11
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.errorHandler = errorHandler;
|
|
4
|
+
const AppError_1 = require("../error/AppError");
|
|
5
|
+
const response_utils_1 = require("@naman_deep_singh/response-utils");
|
|
6
|
+
const mapAppErrorToResponder_1 = require("src/utils/mapAppErrorToResponder");
|
|
7
|
+
function errorHandler(err, req, res, next) {
|
|
8
|
+
const responder = new response_utils_1.ExpressResponder({}, res);
|
|
9
|
+
// AppError → known operational error
|
|
10
|
+
if (err instanceof AppError_1.AppError) {
|
|
11
|
+
return (0, mapAppErrorToResponder_1.mapAppErrorToResponder)(responder, err);
|
|
12
|
+
}
|
|
13
|
+
// Unexpected / programming / unknown error
|
|
14
|
+
console.error("UNEXPECTED ERROR:", err);
|
|
15
|
+
return responder.serverError("Internal server error", {
|
|
16
|
+
details: process.env.NODE_ENV === "production" ? undefined : err,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mapAppErrorToResponder = mapAppErrorToResponder;
|
|
4
|
+
const response_utils_1 = require("@naman_deep_singh/response-utils");
|
|
5
|
+
function mapAppErrorToResponder(responder, err) {
|
|
6
|
+
switch (err.statusCode) {
|
|
7
|
+
case response_utils_1.HTTP_STATUS.CLIENT_ERROR.BAD_REQUEST:
|
|
8
|
+
return responder.badRequest(err.message, { details: err.details });
|
|
9
|
+
case response_utils_1.HTTP_STATUS.CLIENT_ERROR.UNAUTHORIZED:
|
|
10
|
+
return responder.unauthorized(err.message);
|
|
11
|
+
case response_utils_1.HTTP_STATUS.CLIENT_ERROR.FORBIDDEN:
|
|
12
|
+
return responder.forbidden(err.message);
|
|
13
|
+
case response_utils_1.HTTP_STATUS.CLIENT_ERROR.NOT_FOUND:
|
|
14
|
+
return responder.notFound(err.message);
|
|
15
|
+
case response_utils_1.HTTP_STATUS.CLIENT_ERROR.CONFLICT:
|
|
16
|
+
return responder.conflict(err.message);
|
|
17
|
+
case response_utils_1.HTTP_STATUS.CLIENT_ERROR.UNPROCESSABLE_ENTITY:
|
|
18
|
+
return responder.unprocessableEntity(err.message, { details: err.details });
|
|
19
|
+
case response_utils_1.HTTP_STATUS.CLIENT_ERROR.TOO_MANY_REQUESTS:
|
|
20
|
+
return responder.tooManyRequests(err.message);
|
|
21
|
+
default:
|
|
22
|
+
// Any other custom status maps to a generic server error
|
|
23
|
+
return responder.serverError(err.message, { details: err.details });
|
|
24
|
+
}
|
|
25
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@naman_deep_singh/errors-utils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Security utilities for password hashing and JWT token management with TypeScript",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"errors"
|
|
9
|
+
],
|
|
10
|
+
"author": "Naman Deep Singh",
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@types/express": "^5.0.5",
|
|
14
|
+
"@types/node": "^25.0.1",
|
|
15
|
+
"express": "^5.1.0",
|
|
16
|
+
"typescript": "^5.9.3"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@naman_deep_singh/response-utils": "^2.0.3"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsc"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export class AppError extends Error {
|
|
2
|
+
public statusCode: number;
|
|
3
|
+
public isOperational: boolean;
|
|
4
|
+
public details?: any;
|
|
5
|
+
|
|
6
|
+
constructor(message: string, statusCode = 500, details?: any) {
|
|
7
|
+
super(message);
|
|
8
|
+
|
|
9
|
+
this.statusCode = statusCode;
|
|
10
|
+
this.isOperational = true;
|
|
11
|
+
this.details = details;
|
|
12
|
+
|
|
13
|
+
Error.captureStackTrace(this, this.constructor);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AppError } from "./AppError";
|
|
2
|
+
|
|
3
|
+
export class HTTPError extends AppError {
|
|
4
|
+
public status: string;
|
|
5
|
+
|
|
6
|
+
constructor(message: string, statusCode: number, details?: any) {
|
|
7
|
+
super(message, statusCode, details);
|
|
8
|
+
|
|
9
|
+
this.status = `${statusCode}`.startsWith("4") ? "fail" : "error";
|
|
10
|
+
}
|
|
11
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
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
|
+
|
|
11
|
+
export * from "./middleware/errorConverter";
|
|
12
|
+
export * from "./middleware/errorHandler";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from "express";
|
|
2
|
+
import { AppError } from "../error/AppError";
|
|
3
|
+
|
|
4
|
+
export function errorConverter(err: any, req: Request, res: Response, next: NextFunction) {
|
|
5
|
+
if (err instanceof AppError) {
|
|
6
|
+
return next(err);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const convertedError = new AppError(
|
|
10
|
+
err.message || "Internal Error",
|
|
11
|
+
err.statusCode || 500,
|
|
12
|
+
err.details || undefined
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
next(convertedError);
|
|
16
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Request, Response, NextFunction } from "express";
|
|
2
|
+
import { AppError } from "../error/AppError";
|
|
3
|
+
import { ExpressResponder } from "@naman_deep_singh/response-utils";
|
|
4
|
+
import { mapAppErrorToResponder } from "src/utils/mapAppErrorToResponder";
|
|
5
|
+
|
|
6
|
+
export function errorHandler(
|
|
7
|
+
err: unknown,
|
|
8
|
+
req: Request,
|
|
9
|
+
res: Response,
|
|
10
|
+
next: NextFunction
|
|
11
|
+
) {
|
|
12
|
+
const responder = new ExpressResponder({}, res);
|
|
13
|
+
|
|
14
|
+
// AppError → known operational error
|
|
15
|
+
if (err instanceof AppError) {
|
|
16
|
+
return mapAppErrorToResponder(responder, err);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Unexpected / programming / unknown error
|
|
20
|
+
console.error("UNEXPECTED ERROR:", err);
|
|
21
|
+
|
|
22
|
+
return responder.serverError("Internal server error", {
|
|
23
|
+
details: process.env.NODE_ENV === "production" ? undefined : err,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ExpressResponder, HTTP_STATUS } from "@naman_deep_singh/response-utils";
|
|
2
|
+
import { AppError } from "src/error/AppError";
|
|
3
|
+
|
|
4
|
+
export function mapAppErrorToResponder(responder: ExpressResponder<any>, err: AppError) {
|
|
5
|
+
switch (err.statusCode) {
|
|
6
|
+
case HTTP_STATUS.CLIENT_ERROR.BAD_REQUEST:
|
|
7
|
+
return responder.badRequest(err.message, { details: err.details });
|
|
8
|
+
|
|
9
|
+
case HTTP_STATUS.CLIENT_ERROR.UNAUTHORIZED:
|
|
10
|
+
return responder.unauthorized(err.message);
|
|
11
|
+
|
|
12
|
+
case HTTP_STATUS.CLIENT_ERROR.FORBIDDEN:
|
|
13
|
+
return responder.forbidden(err.message);
|
|
14
|
+
|
|
15
|
+
case HTTP_STATUS.CLIENT_ERROR.NOT_FOUND:
|
|
16
|
+
return responder.notFound(err.message);
|
|
17
|
+
|
|
18
|
+
case HTTP_STATUS.CLIENT_ERROR.CONFLICT:
|
|
19
|
+
return responder.conflict(err.message);
|
|
20
|
+
|
|
21
|
+
case HTTP_STATUS.CLIENT_ERROR.UNPROCESSABLE_ENTITY:
|
|
22
|
+
return responder.unprocessableEntity(err.message, { details: err.details });
|
|
23
|
+
|
|
24
|
+
case HTTP_STATUS.CLIENT_ERROR.TOO_MANY_REQUESTS:
|
|
25
|
+
return responder.tooManyRequests(err.message);
|
|
26
|
+
|
|
27
|
+
default:
|
|
28
|
+
// Any other custom status maps to a generic server error
|
|
29
|
+
return responder.serverError(err.message, { details: err.details });
|
|
30
|
+
}
|
|
31
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "CommonJS",
|
|
5
|
+
"moduleResolution": "node",
|
|
6
|
+
"rootDir": "./src",
|
|
7
|
+
"outDir": "./dist",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"allowSyntheticDefaultImports": true,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"forceConsistentCasingInFileNames": true,
|
|
13
|
+
"declaration": true,
|
|
14
|
+
"baseUrl": ".",
|
|
15
|
+
"paths": {
|
|
16
|
+
"*": ["*", "*.ts", "*.js"]
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"include": ["src/**/*"],
|
|
20
|
+
"exclude": ["node_modules", "dist"]
|
|
21
|
+
}
|