fastify / fastify-plugin

Plugin helper for Fastify
MIT License
197 stars 42 forks source link

custom error handler not breaking encapsulation using fastify-plugin #240

Closed ritikrao1 closed 2 months ago

ritikrao1 commented 2 months ago

Prerequisites

Fastify version

4.26.2

Plugin version

4.5.1

Node.js version

14.18.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

14.3

Description

i have set 2 error handlers, i want that error handler that is sending response 'internal error' should be invoked when handler throws error, but in this external custom error handler is getting invoked and reply sent is 'external error'

const fastify = require("fastify");
const app = fastify({logger: true, caseSensitive: false});
const fp = require('fastify-plugin')
app.register(function (app,options, done){
    app.route({
        method: "post",
        url : '/testingApi', 
        handler: async (req, res) => {
            throw new Error("in api handler");
            req.result = { msg: "sent successfully"};
            res.status(200).send(req.result);
        }
    });
    app.register(fp(addErrorHandler), {});
    done();
})

const addErrorHandler = (app, options, done) => {
    app.setErrorHandler(async (err, req, res) => {
        res.status(500).send('internal error');
        return res;
    })
    done();
}

app.setErrorHandler(async (err, req, res) => {
    res.status(500).send('external error');
    return res;
})

app.addHook('onReady', (done) => {
    console.log("Service running on port: 3000");
    done();
});
app.listen({host: 'localhost', port: 3000});

Link to code that reproduces the bug

No response

Expected Behavior

should get reply 'internal error' on hitting api /testingApi

mcollina commented 2 months ago

You need to await your internal plugin:

const fastify = require("fastify");
const app = fastify({logger: true, caseSensitive: false});
const fp = require('fastify-plugin')
app.register(async function (app,options){
    await app.register(fp(addErrorHandler), {});
    app.route({
        method: "POST",
        url : '/testingApi', 
        handler: async (req, res) => {
            throw new Error("in api handler");
            req.result = { msg: "sent successfully"};
            res.status(200).send(req.result);
        }
    });
})

const addErrorHandler = (app, options, done) => {
    app.setErrorHandler(async (err, req, res) => {
        res.status(500).send('internal error');
        return res;
    })
    done();
}

app.setErrorHandler(async (err, req, res) => {
    res.status(500).send('external error');
    return res;
})

app.addHook('onReady', (done) => {
    console.log("Service running on port: 3000");
    done();
});
app.listen({host: 'localhost', port: 3000});
ritikrao1 commented 2 months ago

why does registering plugin after registering route won't work here, it worked when i am registering it before registering route

const fastify = require("fastify");
const app = fastify({logger: true, caseSensitive: false});
const fp = require('fastify-plugin')
app.register(async function (app,options){
    app.route({
        method: "POST",
        url : '/testingApi', 
        handler: async (req, res) => {
            throw new Error("in api handler");
            req.result = { msg: "sent successfully"};
            res.status(200).send(req.result);
        }
    });
     await app.register(fp(addErrorHandler), {});
})

const addErrorHandler = (app, options, done) => {
    app.setErrorHandler(async (err, req, res) => {
        res.status(500).send('internal error');
        return res;
    })
    done();
}

app.setErrorHandler(async (err, req, res) => {
    res.status(500).send('external error');
    return res;
})

app.addHook('onReady', (done) => {
    console.log("Service running on port: 3000");
    done();
});
app.listen({host: 'localhost', port: 3000});