@lssm/lib.error 1.2.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.
@@ -0,0 +1,17 @@
1
+ import { ErrorCode } from "./codes.js";
2
+
3
+ //#region src/appError.d.ts
4
+
5
+ /**
6
+ * Generic application error with code and optional details.
7
+ */
8
+ declare class AppError extends Error {
9
+ code: ErrorCode;
10
+ details?: Record<string, unknown> | undefined;
11
+ constructor(code: ErrorCode, message: string, details?: Record<string, unknown> | undefined);
12
+ }
13
+ /** Type guard to detect AppError */
14
+ declare function isAppError(err: unknown): err is AppError;
15
+ //#endregion
16
+ export { AppError, isAppError };
17
+ //# sourceMappingURL=appError.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"appError.d.ts","names":[],"sources":["../src/appError.ts"],"sourcesContent":[],"mappings":";;;;;;AAKA;AAEiB,cAFJ,QAAA,SAAiB,KAAA,CAEb;EAEI,IAAA,EAFJ,SAEI;EAFJ,OAAA,CAAA,EAEI,MAFJ,CAAA,MAAA,EAAA,OAAA,CAAA,GAAA,SAAA;EAEI,WAAA,CAAA,IAAA,EAFJ,SAEI,EAAA,OAAA,EAAA,MAAA,EAAA,OAAA,CAAA,EAAA,MAAA,CAAA,MAAA,EAAA,OAAA,CAAA,GAAA,SAAA;;;AASL,iBAAA,UAAA,CAAiC,GAAQ,EAAA,OAAA,CAAA,EAAA,GAAA,IAAR,QAAQ"}
@@ -0,0 +1,2 @@
1
+ var e=class extends Error{constructor(e,t,n){super(t),this.code=e,this.details=n,Object.setPrototypeOf(this,new.target.prototype),this.name=`AppError`}};function t(t){return t instanceof e}export{e as AppError,t as isAppError};
2
+ //# sourceMappingURL=appError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"appError.js","names":["code: ErrorCode","details?: Record<string, unknown>"],"sources":["../src/appError.ts"],"sourcesContent":["import { ErrorCode } from './codes';\n\n/**\n * Generic application error with code and optional details.\n */\nexport class AppError extends Error {\n constructor(\n public code: ErrorCode,\n message: string,\n public details?: Record<string, unknown>\n ) {\n super(message);\n Object.setPrototypeOf(this, new.target.prototype);\n this.name = 'AppError';\n }\n}\n\n/** Type guard to detect AppError */\nexport function isAppError(err: unknown): err is AppError {\n return err instanceof AppError;\n}\n"],"mappings":"AAKA,IAAa,EAAb,cAA8B,KAAM,CAClC,YACE,EACA,EACA,EACA,CACA,MAAM,EAAQ,CAJP,KAAA,KAAA,EAEA,KAAA,QAAA,EAGP,OAAO,eAAe,KAAM,IAAI,OAAO,UAAU,CACjD,KAAK,KAAO,aAKhB,SAAgB,EAAW,EAA+B,CACxD,OAAO,aAAe"}
@@ -0,0 +1,19 @@
1
+ //#region src/codes.d.ts
2
+ /**
3
+ * Centralised error codes used across services.
4
+ * Extend this enum to add more domain-specific codes.
5
+ */
6
+ declare enum ErrorCode {
7
+ UNAUTHENTICATED = "UNAUTHENTICATED",
8
+ FORBIDDEN = "FORBIDDEN",
9
+ NOT_FOUND = "NOT_FOUND",
10
+ INVALID_INPUT = "INVALID_INPUT",
11
+ CONFLICT = "CONFLICT",
12
+ RATE_LIMITED = "RATE_LIMITED",
13
+ SERVER_ERROR = "SERVER_ERROR",
14
+ POLICY_DENIED = "POLICY_DENIED",
15
+ }
16
+ declare function isKnownErrorCode(code: string): code is ErrorCode;
17
+ //#endregion
18
+ export { ErrorCode, isKnownErrorCode };
19
+ //# sourceMappingURL=codes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codes.d.ts","names":[],"sources":["../src/codes.ts"],"sourcesContent":[],"mappings":";;AAIA;AAYA;;aAZY,SAAA;;;;;;;;;;iBAYI,gBAAA,wBAAwC"}
package/dist/codes.js ADDED
@@ -0,0 +1,2 @@
1
+ let e=function(e){return e.UNAUTHENTICATED=`UNAUTHENTICATED`,e.FORBIDDEN=`FORBIDDEN`,e.NOT_FOUND=`NOT_FOUND`,e.INVALID_INPUT=`INVALID_INPUT`,e.CONFLICT=`CONFLICT`,e.RATE_LIMITED=`RATE_LIMITED`,e.SERVER_ERROR=`SERVER_ERROR`,e.POLICY_DENIED=`POLICY_DENIED`,e}({});function t(t){return Object.values(e).includes(t)}export{e as ErrorCode,t as isKnownErrorCode};
2
+ //# sourceMappingURL=codes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codes.js","names":[],"sources":["../src/codes.ts"],"sourcesContent":["/**\n * Centralised error codes used across services.\n * Extend this enum to add more domain-specific codes.\n */\nexport enum ErrorCode {\n UNAUTHENTICATED = 'UNAUTHENTICATED',\n FORBIDDEN = 'FORBIDDEN',\n NOT_FOUND = 'NOT_FOUND',\n INVALID_INPUT = 'INVALID_INPUT',\n CONFLICT = 'CONFLICT',\n RATE_LIMITED = 'RATE_LIMITED',\n SERVER_ERROR = 'SERVER_ERROR',\n POLICY_DENIED = 'POLICY_DENIED',\n // add more codes here\n}\n\nexport function isKnownErrorCode(code: string): code is ErrorCode {\n return Object.values(ErrorCode).includes(code as ErrorCode);\n}\n"],"mappings":"AAIA,IAAY,EAAA,SAAA,EAAL,OACL,GAAA,gBAAA,kBACA,EAAA,UAAA,YACA,EAAA,UAAA,YACA,EAAA,cAAA,gBACA,EAAA,SAAA,WACA,EAAA,aAAA,eACA,EAAA,aAAA,eACA,EAAA,cAAA,uBAIF,SAAgB,EAAiB,EAAiC,CAChE,OAAO,OAAO,OAAO,EAAU,CAAC,SAAS,EAAkB"}
package/dist/http.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import { ErrorCode } from "./codes.js";
2
+
3
+ //#region src/http.d.ts
4
+
5
+ /**
6
+ * Map known error codes to HTTP status codes.
7
+ */
8
+ declare function httpStatus(code: ErrorCode): number;
9
+ //#endregion
10
+ export { httpStatus };
11
+ //# sourceMappingURL=http.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.d.ts","names":[],"sources":["../src/http.ts"],"sourcesContent":[],"mappings":";;;;;;AAMA;iBAAgB,UAAA,OAAiB"}
package/dist/http.js ADDED
@@ -0,0 +1,2 @@
1
+ import{ErrorCode as e}from"./codes.js";function t(t){switch(t){case e.UNAUTHENTICATED:return 401;case e.FORBIDDEN:case e.POLICY_DENIED:return 403;case e.NOT_FOUND:return 404;case e.INVALID_INPUT:return 400;case e.CONFLICT:return 409;case e.RATE_LIMITED:return 429;default:return 500}}export{t as httpStatus};
2
+ //# sourceMappingURL=http.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.js","names":[],"sources":["../src/http.ts"],"sourcesContent":["import { isAppError } from './appError';\nimport { ErrorCode } from './codes';\n\n/**\n * Map known error codes to HTTP status codes.\n */\nexport function httpStatus(code: ErrorCode): number {\n switch (code) {\n case ErrorCode.UNAUTHENTICATED:\n return 401;\n case ErrorCode.FORBIDDEN:\n case ErrorCode.POLICY_DENIED:\n return 403;\n case ErrorCode.NOT_FOUND:\n return 404;\n case ErrorCode.INVALID_INPUT:\n return 400;\n case ErrorCode.CONFLICT:\n return 409;\n case ErrorCode.RATE_LIMITED:\n return 429;\n default:\n return 500;\n }\n}\n\n/**\n * Convert AppError or unknown error into a Problem JSON response body and status code.\n */\n// export function toHttpResponse(err: unknown): { status: number; body: any } {\n// if (isAppError(err)) {\n// const status = httpStatus(err.code);\n// return {\n// status,\n// body: {\n// type: `https://api.chaman.dev/errors/${err.code.toLowerCase()}`,\n// title: err.message,\n// detail: err.details ?? null,\n// status,\n// },\n// };\n// }\n// // Unknown error: hide internal details\n// return {\n// status: 500,\n// body: {\n// type: 'https://api.chaman.dev/errors/server_error',\n// title: 'Unexpected Error',\n// detail: null,\n// status: 500,\n// },\n// };\n// }\n"],"mappings":"uCAMA,SAAgB,EAAW,EAAyB,CAClD,OAAQ,EAAR,CACE,KAAK,EAAU,gBACb,MAAO,KACT,KAAK,EAAU,UACf,KAAK,EAAU,cACb,MAAO,KACT,KAAK,EAAU,UACb,MAAO,KACT,KAAK,EAAU,cACb,MAAO,KACT,KAAK,EAAU,SACb,MAAO,KACT,KAAK,EAAU,aACb,MAAO,KACT,QACE,MAAO"}
@@ -0,0 +1,4 @@
1
+ import { ErrorCode, isKnownErrorCode } from "./codes.js";
2
+ import { AppError, isAppError } from "./appError.js";
3
+ import { httpStatus } from "./http.js";
4
+ export { AppError, ErrorCode, httpStatus, isAppError, isKnownErrorCode };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ import{ErrorCode as e,isKnownErrorCode as t}from"./codes.js";import{AppError as n,isAppError as r}from"./appError.js";import{httpStatus as i}from"./http.js";export{n as AppError,e as ErrorCode,i as httpStatus,r as isAppError,t as isKnownErrorCode};
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@lssm/lib.error",
3
+ "version": "1.2.0",
4
+ "scripts": {
5
+ "build": "bun build:bundle && bun build:types",
6
+ "build:bundle": "tsdown",
7
+ "build:types": "tsc --noEmit",
8
+ "dev": "bun build:bundle --watch",
9
+ "clean": "rimraf dist .turbo",
10
+ "lint": "bun lint:fix",
11
+ "lint:fix": "eslint src --fix",
12
+ "lint:check": "eslint src"
13
+ },
14
+ "devDependencies": {
15
+ "@lssm/tool.typescript": "workspace:*",
16
+ "tsdown": "^0.15.9",
17
+ "typescript": "^5.9.3"
18
+ },
19
+ "dependencies": {},
20
+ "peerDependencies": {
21
+ "elysia": "^1.4.11",
22
+ "express": "^5.1.0",
23
+ "next": "15.5.6"
24
+ },
25
+ "exports": {
26
+ ".": "./src/index.ts",
27
+ "./appError": "./src/appError.ts",
28
+ "./codes": "./src/codes.ts",
29
+ "./http": "./src/http.ts",
30
+ "./*": "./*"
31
+ },
32
+ "type": "module",
33
+ "main": "./dist/index.js",
34
+ "module": "./dist/index.js",
35
+ "types": "./dist/index.d.ts",
36
+ "publishConfig": {
37
+ "exports": {
38
+ ".": "./dist/index.js",
39
+ "./appError": "./dist/appError.js",
40
+ "./codes": "./dist/codes.js",
41
+ "./http": "./dist/http.js",
42
+ "./*": "./*"
43
+ },
44
+ "access": "public"
45
+ },
46
+ "files": [
47
+ "dist",
48
+ "README.md"
49
+ ]
50
+ }