Skip to content

Commit 60cf5c1

Browse files
feat(sensible): add http error examples with sensible
1 parent c9ebcbf commit 60cf5c1

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/plugins/sensible.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import fp from "fastify-plugin";
22
import sensible from "@fastify/sensible";
3+
import { Type } from "@sinclair/typebox";
4+
5+
export const HttpError = Type.Ref("HttpError");
36

47
/**
58
* This plugin adds some utilities to handle http errors.
69
*
710
* @see https://github.com/fastify/fastify-sensible
811
*/
912
export default fp(async (fastify) => {
10-
fastify.register(sensible);
13+
fastify.register(sensible, {
14+
sharedSchemaId: "HttpError",
15+
});
1116
});

src/routes/example/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { FastifyPluginAsync } from "fastify";
22
import { FastifyTypebox } from "../../app.js";
33
import { Type } from "@sinclair/typebox";
4+
import { HttpError } from "../../plugins/sensible.js";
45

56
const example: FastifyPluginAsync = async (
67
fastify: FastifyTypebox,
@@ -21,6 +22,21 @@ const example: FastifyPluginAsync = async (
2122
return "this is an example";
2223
},
2324
);
25+
fastify.get(
26+
"/error",
27+
{
28+
schema: {
29+
summary: "Get Example with Some Errors",
30+
tags: ["Example"],
31+
response: {
32+
200: HttpError,
33+
},
34+
},
35+
},
36+
async function (request, reply) {
37+
return reply.badRequest("this is an error example");
38+
},
39+
);
2440
};
2541

2642
export default example;

0 commit comments

Comments
 (0)