Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(handler): custom error handling (#2943)
- Loading branch information
1 parent
505f90b
commit de01bfc
Showing
12 changed files
with
193 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { createHandler } from "~/fastify"; | ||
import { createRoute } from "~/plugins/RoutePlugin"; | ||
import WebinyError from "@webiny/error"; | ||
import { createModifyFastifyPlugin } from "~/plugins/ModifyFastifyPlugin"; | ||
|
||
describe("custom error handler", () => { | ||
const data = { | ||
stringValue: "123", | ||
numberValue: 123, | ||
booleanValue: false, | ||
arrayValue: ["123", 123, false], | ||
objectValue: { | ||
testing: true, | ||
errorMessage: "not ok" | ||
} | ||
}; | ||
|
||
it("should properly output error via built-in error handler", async () => { | ||
const app = createHandler({ | ||
plugins: [ | ||
createRoute(({ onAll }) => { | ||
onAll("/webiny-test", async () => { | ||
throw new WebinyError( | ||
"Testing custom error handler output", | ||
"CUSTOM_ERROR_HANDLER_CODE", | ||
data | ||
); | ||
}); | ||
}) | ||
] | ||
}); | ||
|
||
const result = await app.inject({ | ||
path: "/webiny-test", | ||
headers: { | ||
"content-type": "application/json" | ||
}, | ||
method: "POST", | ||
payload: "{}" | ||
}); | ||
|
||
const expected = JSON.stringify({ | ||
message: "Testing custom error handler output", | ||
code: "CUSTOM_ERROR_HANDLER_CODE", | ||
data: { | ||
stringValue: "123", | ||
numberValue: 123, | ||
booleanValue: false, | ||
arrayValue: ["123", 123, false], | ||
objectValue: { | ||
testing: true, | ||
errorMessage: "not ok" | ||
} | ||
} | ||
}); | ||
|
||
expect(result).toMatchObject({ | ||
payload: expected, | ||
body: expected, | ||
statusCode: 500 | ||
}); | ||
}); | ||
|
||
it("should properly output error via user defined error handler", async () => { | ||
const app = createHandler({ | ||
plugins: [ | ||
createModifyFastifyPlugin(instance => { | ||
instance.setErrorHandler((error, request, reply) => { | ||
return reply | ||
.send({ | ||
justSimpleOutput: true | ||
}) | ||
.code(404); | ||
}); | ||
}), | ||
createRoute(({ onAll }) => { | ||
onAll("/webiny-test", async () => { | ||
throw new WebinyError( | ||
"Testing custom error handler output", | ||
"CUSTOM_ERROR_HANDLER_CODE", | ||
data | ||
); | ||
}); | ||
}) | ||
] | ||
}); | ||
|
||
const result = await app.inject({ | ||
path: "/webiny-test", | ||
headers: { | ||
"content-type": "application/json" | ||
}, | ||
method: "GET", | ||
payload: "{}" | ||
}); | ||
|
||
const expected = JSON.stringify({ | ||
justSimpleOutput: true | ||
}); | ||
|
||
expect(result).toMatchObject({ | ||
payload: expected, | ||
body: expected, | ||
statusCode: 404 | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { createHandler } from "~/fastify"; | ||
import { createModifyFastifyPlugin } from "~/plugins/ModifyFastifyPlugin"; | ||
|
||
describe("modify fastify with plugin", () => { | ||
it("should modify existing Fastify instance via the plugin", async () => { | ||
const app = createHandler({ | ||
plugins: [ | ||
createModifyFastifyPlugin(instance => { | ||
(instance as any).modifiedValue = true; | ||
}) | ||
] | ||
}); | ||
|
||
expect((app as any).modifiedValue).toEqual(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
export * from "~/fastify"; | ||
export * from "@webiny/api/plugins/ContextPlugin"; | ||
export * from "~/fastify"; | ||
export * from "~/Context"; | ||
export * from "~/plugins/EventPlugin"; | ||
export * from "~/plugins/RoutePlugin"; | ||
export * from "~/plugins/BeforeHandlerPlugin"; | ||
export * from "~/plugins/HandlerErrorPlugin"; | ||
export * from "~/plugins/HandlerResultPlugin"; | ||
export * from "~/plugins/ModifyFastifyPlugin"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Plugin } from "@webiny/plugins/Plugin"; | ||
import { FastifyInstance } from "fastify"; | ||
|
||
interface ModifyFastifyPluginCallable { | ||
(app: FastifyInstance): void; | ||
} | ||
|
||
export class ModifyFastifyPlugin extends Plugin { | ||
public static override type = "handler.fastify.modify"; | ||
|
||
private readonly cb: ModifyFastifyPluginCallable; | ||
|
||
public constructor(cb: ModifyFastifyPluginCallable) { | ||
super(); | ||
this.cb = cb; | ||
} | ||
|
||
public modify(app: FastifyInstance): void { | ||
this.cb(app); | ||
} | ||
} | ||
|
||
export const createModifyFastifyPlugin = (cb: ModifyFastifyPluginCallable) => { | ||
return new ModifyFastifyPlugin(cb); | ||
}; |