dario-tcr-problem-details 1.0.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.
@@ -0,0 +1,26 @@
1
+ export declare const ERROR_CATALOG: {
2
+ readonly USER_NOT_FOUND: {
3
+ readonly title: "Not Found";
4
+ readonly status: 404;
5
+ readonly type: "/errors/not-found";
6
+ };
7
+ readonly FORBIDDEN: {
8
+ readonly title: "Forbidden";
9
+ readonly status: 403;
10
+ readonly type: "/errors/forbidden";
11
+ };
12
+ readonly INTERNAL_ERROR: {
13
+ readonly title: "Internal Server Error";
14
+ readonly status: 500;
15
+ readonly type: "/errors/internal";
16
+ };
17
+ };
18
+ export declare class AppError extends Error {
19
+ readonly status: number;
20
+ readonly title: string;
21
+ readonly type: string;
22
+ readonly detail: string;
23
+ readonly traceId: string;
24
+ constructor(status: number, title: string, type: string, detail: string, traceId?: string);
25
+ static fromCatalog(key: keyof typeof ERROR_CATALOG, detail: string): AppError;
26
+ }
package/dist/errors.js ADDED
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AppError = exports.ERROR_CATALOG = void 0;
4
+ exports.ERROR_CATALOG = {
5
+ USER_NOT_FOUND: { title: "Not Found", status: 404, type: "/errors/not-found" },
6
+ FORBIDDEN: { title: "Forbidden", status: 403, type: "/errors/forbidden" },
7
+ INTERNAL_ERROR: { title: "Internal Server Error", status: 500, type: "/errors/internal" }
8
+ };
9
+ class AppError extends Error {
10
+ constructor(status, title, type, detail, traceId = crypto.randomUUID()) {
11
+ super(detail);
12
+ this.status = status;
13
+ this.title = title;
14
+ this.type = type;
15
+ this.detail = detail;
16
+ this.traceId = traceId;
17
+ Object.setPrototypeOf(this, AppError.prototype);
18
+ }
19
+ static fromCatalog(key, detail) {
20
+ const err = exports.ERROR_CATALOG[key];
21
+ return new AppError(err.status, err.title, err.type, detail);
22
+ }
23
+ }
24
+ exports.AppError = AppError;
@@ -0,0 +1,8 @@
1
+ export declare function formatProblemDetails(error: any, instance: string): {
2
+ type: any;
3
+ title: any;
4
+ status: any;
5
+ detail: any;
6
+ instance: string;
7
+ traceId: any;
8
+ };
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatProblemDetails = formatProblemDetails;
4
+ function formatProblemDetails(error, instance) {
5
+ return {
6
+ type: error.type || "/errors/internal",
7
+ title: error.title || "Internal Server Error",
8
+ status: error.status || 500,
9
+ detail: error.message || "An unexpected error occurred",
10
+ instance,
11
+ traceId: error.traceId || "N/A"
12
+ };
13
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./errors";
2
+ export * from "./formatter";
package/dist/index.js ADDED
@@ -0,0 +1,18 @@
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("./errors"), exports);
18
+ __exportStar(require("./formatter"), exports);
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "dario-tcr-problem-details",
3
+ "version": "1.0.1",
4
+ "description": "RFC 7807 Error Handling Library",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc"
9
+ },
10
+ "devDependencies": {
11
+ "@types/node": "^20.0.0",
12
+ "typescript": "^5.9.3"
13
+ }
14
+ }
package/src/errors.ts ADDED
@@ -0,0 +1,23 @@
1
+ export const ERROR_CATALOG = {
2
+ USER_NOT_FOUND: { title: "Not Found", status: 404, type: "/errors/not-found" },
3
+ FORBIDDEN: { title: "Forbidden", status: 403, type: "/errors/forbidden" },
4
+ INTERNAL_ERROR: { title: "Internal Server Error", status: 500, type: "/errors/internal" }
5
+ } as const;
6
+
7
+ export class AppError extends Error {
8
+ constructor(
9
+ public readonly status: number,
10
+ public readonly title: string,
11
+ public readonly type: string,
12
+ public readonly detail: string,
13
+ public readonly traceId: string = crypto.randomUUID()
14
+ ) {
15
+ super(detail);
16
+ Object.setPrototypeOf(this, AppError.prototype);
17
+ }
18
+
19
+ static fromCatalog(key: keyof typeof ERROR_CATALOG, detail: string) {
20
+ const err = ERROR_CATALOG[key];
21
+ return new AppError(err.status, err.title, err.type, detail);
22
+ }
23
+ }
@@ -0,0 +1,10 @@
1
+ export function formatProblemDetails(error: any, instance: string) {
2
+ return {
3
+ type: error.type || "/errors/internal",
4
+ title: error.title || "Internal Server Error",
5
+ status: error.status || 500,
6
+ detail: error.message || "An unexpected error occurred",
7
+ instance,
8
+ traceId: error.traceId || "N/A"
9
+ };
10
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./errors";
2
+ export * from "./formatter";
package/tsconfig.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "CommonJS",
5
+ "declaration": true,
6
+ "outDir": "./dist",
7
+ "strict": true,
8
+ "esModuleInterop": true
9
+ },
10
+ "include": [
11
+ "src"
12
+ ]
13
+ }