fastify / fastify/help

Why doesn't this code throw an error?

Open
#1,004 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
68
Forks
8
Avg merge
11h 2m
Merged PRs (30d)
2

Description

I've spent some time debugging my code to find an issue, and realized its the result of fastify swallowing any exceptions thrown after the reply is sent. My use case is I check all responses my routes send, against a strict schema. Is there a reason it's like this, or is it a bug in fastify? I'm aware fastify does _log_ an error, but that doesn't help me handle/see the error.

Fastify Version: 4.26.2

**Minimal reproduction**
```js
const myAbstractedRoutes = [{
name: "route",
path: "/test",
run: (req, res) => res.send("Hello World")
}];

const fastify = require('fastify')({
logger: true
})

fastify.setErrorHandler((error, _request, reply) => {
console.log("This really should log!");
reply.send("Error");
});

for (const route of myAbstractedRoutes) {
fastify.get(route.path, async (req, res) => {
// Uncomment to test this error is logged
// throw new Error("Pre Run Test Error");
await route.run(req, res);
// This error is swallowed.
throw new Error("Post Run Test Error");
});
}

fastify.listen({ port: 3000 });
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.