BackendStack21 / restana

Restana is a lightweight and fast Node.js framework for building RESTful APIs.
MIT License
467 stars 27 forks source link

Unable to get the errorHandler to trigger #84

Closed benjamin-wss closed 4 years ago

benjamin-wss commented 4 years ago

I am using your framework but it seems like I cannot get to the errorHandler function.

My config script:

const service = require('restana')({
  errorHandler (err, req, res) {
    console.log(`Unexpected error: ${err.message}`)
    res.send(err)
  }
});

const config = require('./config');

const bodyParser = require('body-parser');
service.use(bodyParser.json());
service.use(
  bodyParser.urlencoded({
    extended: true,
  }),
);

const compression = require('compression');
service.use(compression());

const cors = require('cors');
service.use(cors());

service.use(async (req, res, next) => {
  try {
    await next()
  } catch (err) {
    return next(err)
  }
})

// Register controllers
const glob = require('glob');
const controllers = glob.sync(`${config.rootDir}/controllers/*.js`);
controllers.forEach((controller) => {
  require(controller)(service);
});

const primaryDb = require('./db/primary');

primaryDb.sequelizeInstance
  .sync({ alter: true })
  .then((syncResult) => {
    console.log('Database syncronized', syncResult);
    return service.start(config.portNumber);
  })
  .then((server) => {
    console.log('Server Started', server);
  })
  .catch((error) => {
    console.error('Server failed to start', error);
  });

My route:


const router = service.getRouter();

const Services = require('../services');

router.get('/keys', (request, response, next) => {
  new Services.PublicFrontEndService()
    .GetAllKeys()
    .then((result) => {
      response.send(result);
    })
    .catch((error) => {
      next(error);
    });
});

router.get('/keys/:key', async (request, response, next) => {
  try {
    const result = await new Services.PublicFrontEndService().GetByKey(request.params.key);

    response.send(result);
  } catch (error) {
    next(error);
  }
});

module.exports = (app) => app.use('/api/public/fe/v1', router);```
jkyberneees commented 4 years ago

Hi @benjamin-wss, it is hard to track what is happening on your code, specially because of imported middlewares.

Could you please have a look at this related thread: https://github.com/jkyberneees/ana/issues/81

Regards