openapi-backend icon indicating copy to clipboard operation
openapi-backend copied to clipboard

Support global error handler

Open piczmar opened this issue 5 years ago • 5 comments

Is it possible to register global error handler? E.g. if I register an API like:

getPets: async (c, event: Lambda.APIGatewayProxyEvent, context: Lambda.Context) => {
    throw new Error('unexpected error')
  },

which as you can see throws an error, I would like to handle such error and return error response in API response, e.g:

{
  "error" : "unexpected error",
  "status": 500, 
  "code" : INTERNAL_ERROR
}

In global error response I could map some common errors like "object not found" or my business case errors like "operation not permitted" etc..

piczmar avatar Aug 22 '20 08:08 piczmar

You mean like

try {
  return await api.handleRequest(req);
} catch (err) {
  // global error handling here
}

?

anttiviljami avatar Oct 08 '20 06:10 anttiviljami

agree with @piczmar, I think he means this:

import OpenAPIBackend from 'openapi-backend';

// create api with your definition file or object
const api = new OpenAPIBackend({ definition: './petstore.yml' });

// register your framework specific request handlers here
api.register({
  getPets: (c, req, res) => res.status(200).json({ result: 'ok' }),
  getPetById: (c, req, res) => res.status(200).json({ result: 'ok' }),
  validationFail: (c, req, res) => res.status(400).json({ err: c.validation.errors }),
  notFound: (c, req, res) => res.status(404).json({ err: 'not found' }),
  // error function is called when throw in operation methods occured
  error:  (c, req, res) => res.status(404).json({ "error" : "unexpected error", "status": 500,  "code" : INTERNAL_ERROR }),
});

// initalize the backend
api.init();

maxjava-dev avatar Jun 04 '21 13:06 maxjava-dev

@sa1dai @piczmar sure, seems reasonable enough if you prefer that to try ... catch.

PRs accepted :)

anttiviljami avatar Jun 05 '21 11:06 anttiviljami

I think it is not about try/catch. It is about being able to put custom exceptions in your code which ripple up the call stack and then turn into a customized json packet back to the client.

I started by inventing lots of different exceptions with different contents, then to separate my exceptions from normal exceptions I needed a load of extra exception checkers, then they all needed an internal code number so I could tell them apart and then blar blar blar all exception boiler plate which is how I ended up here!

I just want to throw my custom exception from the middle of my code and not have try/catches everywhere and for the framework to handle converting my exceptions to 'real' json by custom handlers.

davidnewcomb avatar Jan 06 '22 17:01 davidnewcomb

I took a shot at implementing this here: https://github.com/anttiviljami/openapi-backend/pull/454 @anttiviljami would appreciate your thoughts on this approach. Basically an error can be thrown from a handler, the error will be captured in the context as thrownError and provided to the errorHandler if registered.

julrich1 avatar Oct 24 '22 15:10 julrich1