Comments (3)
The current behavior is actually well documented.
fastify/docs/Reference/Errors.md
Lines 158 to 172 in c90ac14
If you wish to skip preSerialization
, you should send serialized content (e.g. string
, Buffer
, stream
).
import Fastify from 'fastify';
const app = Fastify();
// 4. fallback to outer scoped and send
app.setErrorHandler(async (err, req, res) => {
// You should send serialized content as last fallback.
res.status(500).send(JSON.stringify({ msg: "error response" }));
return res;
})
app.register((app, options, done)=>{
// 1. enter route
app.post('/', {handler: async (req, res) => {
req.result = { msg: "hello"};
res.status(200).send(req.result);
return res;
}})
// 2. pre-serialize and throw
app.addHook('preSerialization', async (req, res, payload) => {
await asyncfunction();
throw new Error('preSerialization error');
});
// 3. handle error and throw
app.setErrorHandler(async (err, req, res) => {
throw new Error('preSerialization error');
});
done();
});
const response = await app.inject({ method: 'POST', path: '/'})
console.log(response.body)
from fastify.
in my outer scoped error handler when i am sending error with this
res.header('content-type', 'application/json; charset=utf-8');
res.status(500).send(JSON.stringify({ msg: "error response" }));
this seems to be sending me json objects in response, with skipping preserialization hooks too
@climba03003 can you confirm if it will break in any case ?
from fastify.
can you confirm if it will break in any case ?
At least I don't see it will break in a case currently.
For my own project, I do send serialized content as last resort.
Working fine since I use fastify
.
from fastify.
Related Issues (20)
- querystring with property of type number fails HOT 7
- onError hook is called before errorHandler when error is thrown in onRequest hook HOT 7
- Approval Request for Future State Proposal HOT 20
- Expose .writeEarlyHints()
- Logger methods are not bound to this HOT 4
- Whole API breaks when passing non-async function to fastify.register HOT 3
- Documentation and example of webhook signature check (through rawBody) HOT 1
- req.headers or req.query null in a 404 handler HOT 6
- Update reply.then and fastify.then types to use branded types to please typescript-eslint HOT 5
- hasRoute not working properly HOT 7
- Serverless Firebase Functions (v2) Issue: "... failed to start and listen on the port defined provided by the PORT=8080 ..." HOT 2
- import/require fastify module is incredibly slow > 300 ms HOT 3
- Custom format for ajv gets overwritten by ajv-formats HOT 4
- Plugin: Generate endpoints based on file names on a directory. HOT 12
- custom error handler not breaking encapsulation using fastify-plugin HOT 1
- Broken type inferrence with auxilliary hook handlers HOT 2
- listen() registers a callback even when listen() fails, calling the callback twice - once when it fails and again if it succeeds in a later call HOT 5
- Only automatically run workflows when a member approved them or if the PR is created by a member HOT 2
- app.close only triggers when a new request comes, resulting in 503 error HOT 7
- Missing cookie field in headers of upgrade websocket request HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fastify.