I'm running a simple Fastify server and when I make a request and it fails, I wanna handle that exception in the setErrorHandler
.
How can I achieve that?
This doesn't seem to work:
import fastify from 'fastify';
import fetch from 'node-fetch';
const app = fastify();
app.setErrorHandler(async (error, request, response) => {
// I want this to be called whenever there is an error
console.log('setErrorHandler');
return 'error';
});
app.get('/', (request, response) => {
// I do not want to use async / await or .catch here
fetch('https://...').then(() => { response.send(); });
});
I get the error UnhandledPromiseRejectionWarning
and the server never sends a response.
I do NOT want to use async
, await
or .catch
on the promise. The reason is I'm simulating a developer error. So if someone forgets to add a .catch
and is not using async
/ await
, I still wanna "catch" that error and return status 500 to the client.
I'm willing to change / wrap the request library adding a .catch
in it, I just don't wanna change that endpoint handler code in general, since it's sort of out of my control (another developer might code it any way they want).