@teardown/errors 0.1.48 → 2.0.27
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/package.json +16 -7
- package/src/elysia.ts +44 -0
- package/src/index.ts +2 -36
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@teardown/errors",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "2.0.27",
|
|
4
4
|
"private": false,
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -18,6 +18,11 @@
|
|
|
18
18
|
"import": "./src/index.ts",
|
|
19
19
|
"default": "./src/index.ts"
|
|
20
20
|
},
|
|
21
|
+
"./elysia": {
|
|
22
|
+
"types": "./src/elysia.ts",
|
|
23
|
+
"import": "./src/elysia.ts",
|
|
24
|
+
"default": "./src/elysia.ts"
|
|
25
|
+
},
|
|
21
26
|
"./package.json": "./package.json"
|
|
22
27
|
},
|
|
23
28
|
"scripts": {
|
|
@@ -27,18 +32,22 @@
|
|
|
27
32
|
"fmt": "bun x biome format --write ./src",
|
|
28
33
|
"lint": "bun x biome lint --write ./src",
|
|
29
34
|
"check": "bun x biome check ./src",
|
|
35
|
+
"test": "bun test",
|
|
30
36
|
"prepublishOnly": "bun x turbo run build"
|
|
31
37
|
},
|
|
32
38
|
"devDependencies": {
|
|
33
|
-
"@biomejs/biome": "2.3.
|
|
34
|
-
"@teardown/tsconfig": "0.
|
|
35
|
-
"@types/bun": "1.3.
|
|
39
|
+
"@biomejs/biome": "2.3.10",
|
|
40
|
+
"@teardown/tsconfig": "2.0.27",
|
|
41
|
+
"@types/bun": "1.3.5"
|
|
36
42
|
},
|
|
37
43
|
"peerDependencies": {
|
|
44
|
+
"elysia": "1.4.16",
|
|
38
45
|
"typescript": "5.9.3",
|
|
39
|
-
"zod": "4.1
|
|
46
|
+
"zod": "4.2.1"
|
|
40
47
|
},
|
|
41
|
-
"
|
|
42
|
-
"elysia":
|
|
48
|
+
"peerDependenciesMeta": {
|
|
49
|
+
"elysia": {
|
|
50
|
+
"optional": true
|
|
51
|
+
}
|
|
43
52
|
}
|
|
44
53
|
}
|
package/src/elysia.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Elysia, ValidationError } from "elysia";
|
|
2
|
+
import {
|
|
3
|
+
BadRequestError,
|
|
4
|
+
ForbiddenError,
|
|
5
|
+
handleError,
|
|
6
|
+
InternalServerError,
|
|
7
|
+
NotFoundError,
|
|
8
|
+
RateLimitError,
|
|
9
|
+
UnauthorizedError,
|
|
10
|
+
UnprocessableContentError,
|
|
11
|
+
} from "./index";
|
|
12
|
+
|
|
13
|
+
export const ElysiaErrors = new Elysia()
|
|
14
|
+
.error({
|
|
15
|
+
FORBIDDEN: ForbiddenError,
|
|
16
|
+
NOT_FOUND: NotFoundError,
|
|
17
|
+
INTERNAL_SERVER_ERROR: InternalServerError,
|
|
18
|
+
BAD_REQUEST: BadRequestError,
|
|
19
|
+
UNAUTHORIZED: UnauthorizedError,
|
|
20
|
+
UNPROCESSABLE_ENTITY: UnprocessableContentError,
|
|
21
|
+
TOO_MANY_REQUESTS: RateLimitError,
|
|
22
|
+
SERVICE_UNAVAILABLE: InternalServerError,
|
|
23
|
+
})
|
|
24
|
+
.onError(({ error, code, set }) => {
|
|
25
|
+
// Skip logging validation errors - they're expected and properly handled by Elysia
|
|
26
|
+
if (error instanceof ValidationError || code === "VALIDATION") {
|
|
27
|
+
const result = handleError(error, code);
|
|
28
|
+
set.status = result.status;
|
|
29
|
+
return result.body;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
console.error("[ErrorHandler] Unhandled error:", {
|
|
33
|
+
code,
|
|
34
|
+
error,
|
|
35
|
+
errorType: typeof error,
|
|
36
|
+
errorConstructor: error?.constructor?.name,
|
|
37
|
+
errorMessage: error instanceof Error ? error.message : String(error),
|
|
38
|
+
errorStack: error instanceof Error ? error.stack : undefined,
|
|
39
|
+
});
|
|
40
|
+
const result = handleError(error, code);
|
|
41
|
+
set.status = result.status;
|
|
42
|
+
return result.body;
|
|
43
|
+
})
|
|
44
|
+
.as("global");
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Elysia, ValidationError } from "elysia";
|
|
2
1
|
import { ZodError } from "zod";
|
|
3
2
|
|
|
4
3
|
export enum ErrorCodes {
|
|
@@ -162,7 +161,7 @@ export interface ErrorHandlerResult {
|
|
|
162
161
|
body: ErrorResponse;
|
|
163
162
|
}
|
|
164
163
|
|
|
165
|
-
const handleValidationError = (error:
|
|
164
|
+
const handleValidationError = (error: unknown): ErrorHandlerResult => {
|
|
166
165
|
const message = error instanceof Error ? error.message : "Validation failed";
|
|
167
166
|
return {
|
|
168
167
|
status: 422,
|
|
@@ -201,7 +200,7 @@ const handleZodError = (error: ZodError): ErrorHandlerResult => {
|
|
|
201
200
|
export function handleError(error: unknown, code: number | string = 500): ErrorHandlerResult {
|
|
202
201
|
// Handle Elysia ValidationError (duck-typed check)
|
|
203
202
|
if (code === "VALIDATION" || (error && typeof error === "object" && "type" in error && error.type === "validation")) {
|
|
204
|
-
return handleValidationError(error
|
|
203
|
+
return handleValidationError(error);
|
|
205
204
|
}
|
|
206
205
|
|
|
207
206
|
// Handle ZodError (duck-typed check for issues array)
|
|
@@ -266,36 +265,3 @@ export function handleError(error: unknown, code: number | string = 500): ErrorH
|
|
|
266
265
|
},
|
|
267
266
|
};
|
|
268
267
|
}
|
|
269
|
-
|
|
270
|
-
export const ElysiaErrors = new Elysia()
|
|
271
|
-
.error({
|
|
272
|
-
FORBIDDEN: ForbiddenError,
|
|
273
|
-
NOT_FOUND: NotFoundError,
|
|
274
|
-
INTERNAL_SERVER_ERROR: InternalServerError,
|
|
275
|
-
BAD_REQUEST: BadRequestError,
|
|
276
|
-
UNAUTHORIZED: UnauthorizedError,
|
|
277
|
-
UNPROCESSABLE_ENTITY: UnprocessableContentError,
|
|
278
|
-
TOO_MANY_REQUESTS: RateLimitError,
|
|
279
|
-
SERVICE_UNAVAILABLE: InternalServerError,
|
|
280
|
-
})
|
|
281
|
-
.onError(({ error, code, set }) => {
|
|
282
|
-
// Skip logging validation errors - they're expected and properly handled by Elysia
|
|
283
|
-
if (error instanceof ValidationError || code === "VALIDATION") {
|
|
284
|
-
const result = handleError(error, code);
|
|
285
|
-
set.status = result.status;
|
|
286
|
-
return result.body;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
console.error("[ErrorHandler] Unhandled error:", {
|
|
290
|
-
code,
|
|
291
|
-
error,
|
|
292
|
-
errorType: typeof error,
|
|
293
|
-
errorConstructor: error?.constructor?.name,
|
|
294
|
-
errorMessage: error instanceof Error ? error.message : String(error),
|
|
295
|
-
errorStack: error instanceof Error ? error.stack : undefined,
|
|
296
|
-
});
|
|
297
|
-
const result = handleError(error, code);
|
|
298
|
-
set.status = result.status;
|
|
299
|
-
return result.body;
|
|
300
|
-
})
|
|
301
|
-
.as("global");
|