@ogcio/fastify-error-handler 5.0.2 → 5.1.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/__tests__/helpers/build-fastify.ts +16 -15
- package/__tests__/helpers/fastify-test-helpers.ts +3 -3
- package/__tests__/initialize-error-handler.test.ts +77 -79
- package/__tests__/life-events-error-handler.test.ts +64 -57
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/initialize-error-handler.d.ts +2 -2
- package/dist/initialize-error-handler.d.ts.map +1 -1
- package/dist/initialize-error-handler.js +1 -1
- package/dist/initialize-error-handler.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +2 -2
- package/src/initialize-error-handler.ts +12 -12
- package/vitest.config.mts +20 -0
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
import { FastifyError, createError } from "@fastify/error";
|
|
2
|
-
import { pino, DestinationStream } from "pino";
|
|
3
|
-
import fastify, { FastifyInstance } from "fastify";
|
|
4
|
-
import { initializeErrorHandler } from "../../src/index.js";
|
|
1
|
+
import { type FastifyError, createError } from "@fastify/error";
|
|
5
2
|
import fastifySensible from "@fastify/sensible";
|
|
3
|
+
import fastify, { type FastifyInstance } from "fastify";
|
|
6
4
|
import httpErrors from "http-errors";
|
|
5
|
+
import { type DestinationStream, pino } from "pino";
|
|
6
|
+
import { initializeErrorHandler } from "../../src/index.js";
|
|
7
7
|
export const buildFastify = async (
|
|
8
|
-
loggerDestination?: DestinationStream
|
|
8
|
+
loggerDestination?: DestinationStream,
|
|
9
9
|
): Promise<FastifyInstance> => {
|
|
10
10
|
const server = fastify({ loggerInstance: pino({}, loggerDestination) });
|
|
11
11
|
initializeErrorHandler(server as unknown as FastifyInstance);
|
|
12
12
|
await server.register(fastifySensible);
|
|
13
13
|
server.get("/error", async (request, _reply) => {
|
|
14
14
|
const parsed = request.query as { [x: string]: unknown };
|
|
15
|
-
const requestedStatusCode = Number(parsed
|
|
16
|
-
const requestedMessage = String(parsed
|
|
15
|
+
const requestedStatusCode = Number(parsed.status_code ?? "500");
|
|
16
|
+
const requestedMessage = String(parsed.error_message ?? "WHOOOPS");
|
|
17
17
|
|
|
18
|
-
if (!parsed
|
|
18
|
+
if (!parsed.status_code) {
|
|
19
19
|
throw new Error(requestedMessage);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
throw createError(
|
|
23
23
|
"CUSTOM_CODE",
|
|
24
24
|
requestedMessage as string,
|
|
25
|
-
requestedStatusCode as number
|
|
25
|
+
requestedStatusCode as number,
|
|
26
26
|
)();
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
server.get("/validation", async (request, _reply) => {
|
|
30
30
|
const parsed = request.query as { [x: string]: unknown };
|
|
31
|
-
const requestedMessage = String(parsed
|
|
31
|
+
const requestedMessage = String(parsed.error_message ?? "WHOOOPS");
|
|
32
32
|
|
|
33
33
|
const error = createError(
|
|
34
34
|
"CUSTOM_CODE",
|
|
@@ -56,11 +56,11 @@ export const buildFastify = async (
|
|
|
56
56
|
|
|
57
57
|
server.get("/life-events/custom", async (request, _reply) => {
|
|
58
58
|
const parsed = request.query as { [x: string]: unknown };
|
|
59
|
-
const requestedStatusCode = Number(parsed
|
|
59
|
+
const requestedStatusCode = Number(parsed.status_code ?? "500");
|
|
60
60
|
|
|
61
61
|
throw server.httpErrors.createError(
|
|
62
62
|
requestedStatusCode as number,
|
|
63
|
-
"message"
|
|
63
|
+
"message",
|
|
64
64
|
);
|
|
65
65
|
});
|
|
66
66
|
|
|
@@ -73,12 +73,13 @@ export const buildFastify = async (
|
|
|
73
73
|
});
|
|
74
74
|
|
|
75
75
|
server.get("/life-events/:errorCode", async (request, _reply) => {
|
|
76
|
-
const errorCode =
|
|
77
|
-
.errorCode)
|
|
76
|
+
const errorCode = request.params
|
|
77
|
+
? Number((request.params as { errorCode: string }).errorCode)
|
|
78
|
+
: "DO NOT EXIST";
|
|
78
79
|
if (!httpErrors[errorCode]) {
|
|
79
80
|
throw new Error("Wrong parameter");
|
|
80
81
|
}
|
|
81
|
-
|
|
82
|
+
|
|
82
83
|
const errorObj = httpErrors[errorCode];
|
|
83
84
|
|
|
84
85
|
throw new errorObj("Failed Correctly!");
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FastifyInstance } from "fastify";
|
|
1
|
+
import type { FastifyInstance } from "fastify";
|
|
2
2
|
import { buildFastify } from "./build-fastify.js";
|
|
3
3
|
export const DEFAULT_HOSTNAME = "localhost:80";
|
|
4
4
|
export const DEFAULT_USER_AGENT = "lightMyRequest";
|
|
@@ -34,11 +34,11 @@ export const initializeServer = async (): Promise<{
|
|
|
34
34
|
loggingDestination: TestingLoggerDestination;
|
|
35
35
|
}> => {
|
|
36
36
|
const loggingDestination = getTestingDestinationLogger();
|
|
37
|
-
const server =
|
|
37
|
+
const server = await buildFastify(loggingDestination.loggerDestination);
|
|
38
38
|
|
|
39
39
|
return { server, loggingDestination };
|
|
40
40
|
};
|
|
41
41
|
|
|
42
|
-
//
|
|
42
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
43
43
|
export const parseLogEntry = (logEntry: string): { [x: string]: any } =>
|
|
44
44
|
JSON.parse(logEntry);
|
|
@@ -1,99 +1,97 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { HttpErrorClasses } from "@ogcio/shared-errors";
|
|
2
|
+
import httpErrors from "http-errors";
|
|
3
|
+
import { assert, afterEach, describe, it } from "vitest";
|
|
3
4
|
import {
|
|
4
5
|
DEFAULT_METHOD,
|
|
5
6
|
DEFAULT_PATH,
|
|
6
7
|
initializeServer,
|
|
7
8
|
} from "./helpers/fastify-test-helpers.js";
|
|
8
|
-
import httpErrors from "http-errors";
|
|
9
|
-
import { HttpErrorClasses } from "@ogcio/shared-errors";
|
|
10
9
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const response = await server.inject({
|
|
16
|
-
method: DEFAULT_METHOD,
|
|
17
|
-
url: DEFAULT_PATH,
|
|
18
|
-
query: { status_code: "500", error_message: "error message" },
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
assert.ok(typeof response !== "undefined");
|
|
22
|
-
assert.equal(response?.statusCode, 500);
|
|
23
|
-
assert.deepStrictEqual(response.json(), {
|
|
24
|
-
code: HttpErrorClasses.ServerError,
|
|
25
|
-
detail: "error message",
|
|
26
|
-
requestId: "req-1",
|
|
27
|
-
name: "FastifyError",
|
|
10
|
+
describe("Error management", () => {
|
|
11
|
+
afterEach(async () => {
|
|
12
|
+
const { server } = await initializeServer();
|
|
13
|
+
await server.close();
|
|
28
14
|
});
|
|
29
|
-
});
|
|
30
15
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
16
|
+
it("manages common errors as expected", async () => {
|
|
17
|
+
const { server } = await initializeServer();
|
|
18
|
+
const response = await server.inject({
|
|
19
|
+
method: DEFAULT_METHOD,
|
|
20
|
+
url: DEFAULT_PATH,
|
|
21
|
+
query: { status_code: "500", error_message: "error message" },
|
|
22
|
+
});
|
|
34
23
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
24
|
+
assert.ok(typeof response !== "undefined");
|
|
25
|
+
assert.equal(response?.statusCode, 500);
|
|
26
|
+
assert.deepEqual(response.json(), {
|
|
27
|
+
code: HttpErrorClasses.ServerError,
|
|
28
|
+
detail: "error message",
|
|
29
|
+
requestId: "req-1",
|
|
30
|
+
name: "FastifyError",
|
|
31
|
+
});
|
|
39
32
|
});
|
|
40
33
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
34
|
+
it("manages validation errors as expected", async () => {
|
|
35
|
+
const { server } = await initializeServer();
|
|
36
|
+
const response = await server.inject({
|
|
37
|
+
method: DEFAULT_METHOD,
|
|
38
|
+
url: "/validation",
|
|
39
|
+
query: { error_message: "error message" },
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
assert.ok(typeof response !== "undefined");
|
|
43
|
+
assert.equal(response?.statusCode, 422);
|
|
44
|
+
assert.deepEqual(response.json(), {
|
|
45
|
+
code: HttpErrorClasses.ValidationError,
|
|
46
|
+
detail: "error message",
|
|
47
|
+
requestId: "req-1",
|
|
48
|
+
name: "UnprocessableEntityError",
|
|
49
|
+
validation: [
|
|
50
|
+
{
|
|
51
|
+
fieldName: "the.instance.path",
|
|
52
|
+
message: "error message",
|
|
53
|
+
validationRule: "field",
|
|
54
|
+
additionalInfo: {
|
|
55
|
+
field: "one",
|
|
56
|
+
property: "two",
|
|
57
|
+
},
|
|
56
58
|
},
|
|
57
|
-
|
|
58
|
-
|
|
59
|
+
],
|
|
60
|
+
});
|
|
59
61
|
});
|
|
60
|
-
});
|
|
61
62
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
63
|
+
it("manages unknown errors as 500", async () => {
|
|
64
|
+
const { server } = await initializeServer();
|
|
65
|
+
const response = await server.inject({
|
|
66
|
+
method: DEFAULT_METHOD,
|
|
67
|
+
url: DEFAULT_PATH,
|
|
68
|
+
query: { status_code: "200", error_message: "error message" },
|
|
69
|
+
});
|
|
65
70
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
71
|
+
assert.ok(typeof response !== "undefined");
|
|
72
|
+
assert.equal(response?.statusCode, 500);
|
|
73
|
+
assert.deepEqual(response.json(), {
|
|
74
|
+
code: HttpErrorClasses.UnknownError,
|
|
75
|
+
detail: "error message",
|
|
76
|
+
requestId: "req-1",
|
|
77
|
+
name: "FastifyError",
|
|
78
|
+
});
|
|
70
79
|
});
|
|
71
80
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
name: "FastifyError",
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
test("404 error is managed as expected", async (t) => {
|
|
83
|
-
const { server } = await initializeServer();
|
|
84
|
-
t.after(() => server.close());
|
|
85
|
-
|
|
86
|
-
const response = await server.inject({
|
|
87
|
-
method: DEFAULT_METHOD,
|
|
88
|
-
url: "/this-path-does-not-exist",
|
|
89
|
-
});
|
|
81
|
+
it("manages 404 errors as expected", async () => {
|
|
82
|
+
const { server } = await initializeServer();
|
|
83
|
+
const response = await server.inject({
|
|
84
|
+
method: DEFAULT_METHOD,
|
|
85
|
+
url: "/this-path-does-not-exist",
|
|
86
|
+
});
|
|
90
87
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
88
|
+
assert.ok(typeof response !== "undefined");
|
|
89
|
+
assert.equal(response?.statusCode, 404);
|
|
90
|
+
assert.deepEqual(response.json(), {
|
|
91
|
+
code: HttpErrorClasses.NotFoundError,
|
|
92
|
+
detail: "Route not found: /this-path-does-not-exist",
|
|
93
|
+
requestId: "req-1",
|
|
94
|
+
name: new httpErrors[404]("TEMP").name,
|
|
95
|
+
});
|
|
98
96
|
});
|
|
99
97
|
});
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import * as sharedErrors from "@ogcio/shared-errors";
|
|
2
|
+
import httpErrors from "http-errors";
|
|
3
|
+
import { assert, afterEach, describe, it } from "vitest";
|
|
3
4
|
import {
|
|
4
5
|
DEFAULT_METHOD,
|
|
5
6
|
initializeServer,
|
|
6
7
|
} from "./helpers/fastify-test-helpers.js";
|
|
7
|
-
import httpErrors from "http-errors";
|
|
8
|
-
import * as sharedErrors from "@ogcio/shared-errors";
|
|
9
8
|
|
|
10
9
|
const errorsProvider = [
|
|
11
10
|
{ errorType: httpErrors[401], expectedStatusCode: 401 },
|
|
@@ -16,67 +15,75 @@ const errorsProvider = [
|
|
|
16
15
|
{ errorType: httpErrors[502], expectedStatusCode: 502 },
|
|
17
16
|
];
|
|
18
17
|
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
describe("Error management", () => {
|
|
19
|
+
afterEach(async () => {
|
|
21
20
|
const { server } = await initializeServer();
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const errorInstance = new errorProv.errorType("message");
|
|
21
|
+
await server.close();
|
|
22
|
+
});
|
|
25
23
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
for (const errorProv of errorsProvider) {
|
|
25
|
+
it(`Error is managed in the correct way - ${errorProv.errorType.name}`, async (_t) => {
|
|
26
|
+
const { server } = await initializeServer();
|
|
27
|
+
await server.close();
|
|
29
28
|
});
|
|
30
29
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
detail: "Failed Correctly!",
|
|
36
|
-
requestId: "req-1",
|
|
37
|
-
name: errorInstance.name,
|
|
38
|
-
});
|
|
39
|
-
})
|
|
40
|
-
);
|
|
30
|
+
for (const errorProv of errorsProvider) {
|
|
31
|
+
it(`manages ${errorProv.errorType.name} correctly`, async () => {
|
|
32
|
+
const { server } = await initializeServer();
|
|
33
|
+
const errorInstance = new errorProv.errorType("message");
|
|
41
34
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
35
|
+
const response = await server.inject({
|
|
36
|
+
method: DEFAULT_METHOD,
|
|
37
|
+
url: `/life-events/${errorProv.expectedStatusCode}`,
|
|
38
|
+
});
|
|
45
39
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
40
|
+
assert.ok(typeof response !== "undefined");
|
|
41
|
+
assert.equal(response?.statusCode, errorProv.expectedStatusCode);
|
|
42
|
+
assert.deepEqual(response.json(), {
|
|
43
|
+
code: sharedErrors.parseHttpErrorClass(errorProv.expectedStatusCode),
|
|
44
|
+
detail: "Failed Correctly!",
|
|
45
|
+
requestId: "req-1",
|
|
46
|
+
name: errorInstance.name,
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
}
|
|
51
50
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
});
|
|
60
|
-
});
|
|
51
|
+
it("manages custom errors based on parameters", async () => {
|
|
52
|
+
const { server } = await initializeServer();
|
|
53
|
+
const response = await server.inject({
|
|
54
|
+
method: DEFAULT_METHOD,
|
|
55
|
+
url: "/life-events/custom",
|
|
56
|
+
query: { status_code: "503" },
|
|
57
|
+
});
|
|
61
58
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
59
|
+
assert.ok(typeof response !== "undefined");
|
|
60
|
+
assert.equal(response?.statusCode, 503);
|
|
61
|
+
assert.deepEqual(response.json(), {
|
|
62
|
+
code: sharedErrors.parseHttpErrorClass(503),
|
|
63
|
+
detail: "message",
|
|
64
|
+
requestId: "req-1",
|
|
65
|
+
name: new httpErrors[503]("MOCK").name,
|
|
66
|
+
});
|
|
67
|
+
});
|
|
65
68
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
69
|
+
it("manages validation errors as expected", async () => {
|
|
70
|
+
const { server } = await initializeServer();
|
|
71
|
+
const response = await server.inject({
|
|
72
|
+
method: DEFAULT_METHOD,
|
|
73
|
+
url: "/life-events/validation",
|
|
74
|
+
});
|
|
70
75
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
76
|
+
assert.ok(typeof response !== "undefined");
|
|
77
|
+
assert.equal(response?.statusCode, 422);
|
|
78
|
+
assert.deepEqual(response.json(), {
|
|
79
|
+
code: sharedErrors.parseHttpErrorClass(422),
|
|
80
|
+
detail: "message",
|
|
81
|
+
requestId: "req-1",
|
|
82
|
+
name: new httpErrors[422]("MOCK").name,
|
|
83
|
+
validation: [
|
|
84
|
+
{ fieldName: "field", message: "error", validationRule: "equal" },
|
|
85
|
+
],
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
}
|
|
82
89
|
});
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAM/C,eAAO,MAAM,sBAAsB,WAAY,eAAe,KAAG,IAGhE,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { initializeNotFoundHandler, setupErrorHandler } from "./initialize-error-handler.js";
|
|
1
|
+
import { initializeNotFoundHandler, setupErrorHandler, } from "./initialize-error-handler.js";
|
|
2
2
|
export const initializeErrorHandler = (server) => {
|
|
3
3
|
setupErrorHandler(server);
|
|
4
4
|
initializeNotFoundHandler(server);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,yBAAyB,EACzB,iBAAiB,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,yBAAyB,EACzB,iBAAiB,GAClB,MAAM,+BAA+B,CAAC;AAEvC,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,MAAuB,EAAQ,EAAE;IACtE,iBAAiB,CAAC,MAAM,CAAC,CAAC;IAC1B,yBAAyB,CAAC,MAAM,CAAC,CAAC;AACpC,CAAC,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { FastifyInstance } from "fastify";
|
|
2
|
-
import { HttpErrorClasses, ValidationErrorData } from "@ogcio/shared-errors";
|
|
1
|
+
import type { FastifyInstance } from "fastify";
|
|
2
|
+
import { type HttpErrorClasses, type ValidationErrorData } from "@ogcio/shared-errors";
|
|
3
3
|
export interface OutputHttpError {
|
|
4
4
|
code: HttpErrorClasses;
|
|
5
5
|
detail: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initialize-error-handler.d.ts","sourceRoot":"","sources":["../src/initialize-error-handler.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"initialize-error-handler.d.ts","sourceRoot":"","sources":["../src/initialize-error-handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,eAAe,EAEhB,MAAM,SAAS,CAAC;AAOjB,OAAO,EACL,KAAK,gBAAgB,EACrB,KAAK,mBAAmB,EAEzB,MAAM,sBAAsB,CAAC;AAI9B,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,gBAAgB,CAAC;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,CAAC,EAAE,mBAAmB,EAAE,CAAC;IACnC,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAOD,eAAO,MAAM,iBAAiB,WAAY,eAAe,KAAG,IAyC3D,CAAC;AAIF,eAAO,MAAM,yBAAyB,WAAY,eAAe,KAAG,IAUnE,CAAC"}
|
|
@@ -27,7 +27,7 @@ export const setupErrorHandler = (server) => {
|
|
|
27
27
|
res.statusCode = statusCode;
|
|
28
28
|
reply.statusCode = res.statusCode;
|
|
29
29
|
};
|
|
30
|
-
server.setErrorHandler(
|
|
30
|
+
server.setErrorHandler((error, request, reply) => {
|
|
31
31
|
if (isHttpError(error)) {
|
|
32
32
|
manageHttpError(error, request, reply);
|
|
33
33
|
return;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initialize-error-handler.js","sourceRoot":"","sources":["../src/initialize-error-handler.ts"],"names":[],"mappings":"AAOA,OAAO,EACL,iBAAiB,EACjB,sBAAsB,EACtB,WAAW,GACZ,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAGL,mBAAmB,GACpB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,
|
|
1
|
+
{"version":3,"file":"initialize-error-handler.js","sourceRoot":"","sources":["../src/initialize-error-handler.ts"],"names":[],"mappings":"AAOA,OAAO,EACL,iBAAiB,EACjB,sBAAsB,EACtB,WAAW,GACZ,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAGL,mBAAmB,GACpB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAkB,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAW/D,sEAAsE;AACtE,oCAAoC;AACpC,wEAAwE;AACxE,mDAAmD;AACnD,iDAAiD;AACjD,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,MAAuB,EAAQ,EAAE;IACjE,MAAM,eAAe,GAAG,CACtB,KAIC,EACD,KAAmB,EACnB,EAAE;QACF,MAAM,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC;QACtB,IAAI,UAAU,GAAG,GAAG,CAAC,UAAU,CAAC;QAChC,UAAU,GAAG,UAAU,IAAI,GAAG,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,GAAG,CAAC;QAClD,mCAAmC;QACnC,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;YAClB,IAAI,KAAK,CAAC,OAAO,KAAK,SAAS,EAAE,CAAC;gBAChC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YAC/B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AAC,KAAK,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;AACrD,CAAC,CAAC;AAEF,MAAM,2BAA2B,GAAG,CAAC,KAAmB,EAAa,EAAE;IACrE,IAAI,CAAC,KAAK,CAAC,UAAU,EAAE,CAAC;QACtB,MAAM,UAAU,CAAC,WAAW,CAAC,GAAG,EAAE,gCAAgC,CAAC,CAAC;IACtE,CAAC;IAED,OAAO,UAAU,CAAC,WAAW,CAC3B,GAAG,EACH,KAAK,CAAC,OAAO,EACb,6BAA6B,CAAC,KAAK,CAAC,UAAU,CAAC,CAChD,CAAC;AACJ,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ogcio/fastify-error-handler",
|
|
3
|
-
"version": "5.0
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "rm -rf dist tsconfig.prod.tsbuildinfo tsconfig.tsbuildinfo && tsc -p tsconfig.prod.json",
|
|
9
|
-
"test": "
|
|
9
|
+
"test": "vitest run --coverage --outputFile=results.xml"
|
|
10
10
|
},
|
|
11
11
|
"keywords": [],
|
|
12
12
|
"author": {
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { FastifyInstance } from "fastify";
|
|
1
|
+
import type { FastifyInstance } from "fastify";
|
|
2
2
|
import {
|
|
3
3
|
initializeNotFoundHandler,
|
|
4
|
-
setupErrorHandler
|
|
4
|
+
setupErrorHandler,
|
|
5
5
|
} from "./initialize-error-handler.js";
|
|
6
6
|
|
|
7
7
|
export const initializeErrorHandler = (server: FastifyInstance): void => {
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type {
|
|
2
2
|
FastifyError,
|
|
3
3
|
FastifyRequest,
|
|
4
4
|
FastifyInstance,
|
|
5
5
|
FastifyReply,
|
|
6
6
|
} from "fastify";
|
|
7
|
-
import { FastifySchemaValidationError } from "fastify/types/schema.js";
|
|
7
|
+
import type { FastifySchemaValidationError } from "fastify/types/schema.js";
|
|
8
8
|
import {
|
|
9
9
|
setLoggingContext,
|
|
10
10
|
getLoggingContextError,
|
|
11
11
|
LogMessages,
|
|
12
12
|
} from "@ogcio/fastify-logging-wrapper";
|
|
13
13
|
import {
|
|
14
|
-
HttpErrorClasses,
|
|
15
|
-
ValidationErrorData,
|
|
14
|
+
type HttpErrorClasses,
|
|
15
|
+
type ValidationErrorData,
|
|
16
16
|
parseHttpErrorClass,
|
|
17
17
|
} from "@ogcio/shared-errors";
|
|
18
18
|
import { isHttpError } from "http-errors";
|
|
19
|
-
import { HttpError, httpErrors } from "@fastify/sensible";
|
|
19
|
+
import { type HttpError, httpErrors } from "@fastify/sensible";
|
|
20
20
|
|
|
21
21
|
export interface OutputHttpError {
|
|
22
22
|
code: HttpErrorClasses;
|
|
@@ -39,7 +39,7 @@ export const setupErrorHandler = (server: FastifyInstance): void => {
|
|
|
39
39
|
status?: number;
|
|
40
40
|
statusCode?: number;
|
|
41
41
|
},
|
|
42
|
-
reply: FastifyReply
|
|
42
|
+
reply: FastifyReply,
|
|
43
43
|
) => {
|
|
44
44
|
const res = reply.raw;
|
|
45
45
|
let statusCode = res.statusCode;
|
|
@@ -59,7 +59,7 @@ export const setupErrorHandler = (server: FastifyInstance): void => {
|
|
|
59
59
|
reply.statusCode = res.statusCode;
|
|
60
60
|
};
|
|
61
61
|
|
|
62
|
-
server.setErrorHandler(
|
|
62
|
+
server.setErrorHandler((error, request, reply) => {
|
|
63
63
|
if (isHttpError(error)) {
|
|
64
64
|
manageHttpError(error, request, reply);
|
|
65
65
|
return;
|
|
@@ -90,7 +90,7 @@ export const initializeNotFoundHandler = (server: FastifyInstance): void => {
|
|
|
90
90
|
};
|
|
91
91
|
|
|
92
92
|
const getValidationFromFastifyError = (
|
|
93
|
-
validationInput: FastifySchemaValidationError[]
|
|
93
|
+
validationInput: FastifySchemaValidationError[],
|
|
94
94
|
): { validation: ValidationErrorData[] } => {
|
|
95
95
|
const output: ValidationErrorData[] = [];
|
|
96
96
|
|
|
@@ -121,7 +121,7 @@ const getValidationFromFastifyError = (
|
|
|
121
121
|
|
|
122
122
|
const getResponseFromFastifyError = (
|
|
123
123
|
error: FastifyError,
|
|
124
|
-
request: FastifyRequest
|
|
124
|
+
request: FastifyRequest,
|
|
125
125
|
): OutputHttpError => {
|
|
126
126
|
const output: OutputHttpError = {
|
|
127
127
|
code: parseHttpErrorClass(error.statusCode),
|
|
@@ -131,7 +131,7 @@ const getResponseFromFastifyError = (
|
|
|
131
131
|
};
|
|
132
132
|
if (error.validation && error.validation.length > 0) {
|
|
133
133
|
output.validation = getValidationFromFastifyError(
|
|
134
|
-
error.validation
|
|
134
|
+
error.validation,
|
|
135
135
|
).validation;
|
|
136
136
|
}
|
|
137
137
|
|
|
@@ -141,7 +141,7 @@ const getResponseFromFastifyError = (
|
|
|
141
141
|
const manageHttpError = (
|
|
142
142
|
error: HttpError,
|
|
143
143
|
request: FastifyRequest,
|
|
144
|
-
reply: FastifyReply
|
|
144
|
+
reply: FastifyReply,
|
|
145
145
|
): void => {
|
|
146
146
|
reply.raw.statusCode = error.statusCode;
|
|
147
147
|
reply.statusCode = error.statusCode;
|
|
@@ -175,6 +175,6 @@ const toOutputHttpValidationError = (error: FastifyError): HttpError => {
|
|
|
175
175
|
return httpErrors.createError(
|
|
176
176
|
422,
|
|
177
177
|
error.message,
|
|
178
|
-
getValidationFromFastifyError(error.validation)
|
|
178
|
+
getValidationFromFastifyError(error.validation),
|
|
179
179
|
);
|
|
180
180
|
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { defineConfig } from "vitest/config";
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
test: {
|
|
5
|
+
reporters: "junit",
|
|
6
|
+
coverage: {
|
|
7
|
+
reporter: ["text"],
|
|
8
|
+
provider: "istanbul",
|
|
9
|
+
},
|
|
10
|
+
|
|
11
|
+
include: [
|
|
12
|
+
"**/@(test?(s)|__test?(s)__)/**/*.test.@(js|cjs|mjs|tap|cts|jsx|mts|ts|tsx)",
|
|
13
|
+
"**/*.@(test?(s)|spec).@(js|cjs|mjs|tap|cts|jsx|mts|ts|tsx)",
|
|
14
|
+
"**/test?(s).@(js|cjs|mjs|tap|cts|jsx|mts|ts|tsx)",
|
|
15
|
+
],
|
|
16
|
+
exclude: ["**/@(fixture*(s)|dist|node_modules)/**"],
|
|
17
|
+
maxConcurrency: 1,
|
|
18
|
+
testTimeout: 30000, // Timeout in milliseconds (30 seconds)
|
|
19
|
+
},
|
|
20
|
+
});
|