BackendStack21 / restana

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

Reason of errClass in error handler response #59

Closed nandanugg closed 4 years ago

nandanugg commented 5 years ago

Hi! 👋Very nice work creating this framework!

I just wanna ask, what is the reason behind errClass reply prop inside error handler?

jkyberneees commented 5 years ago

Hi @nandanugg, thanks for your kind words. The errClass property was part of the initial err handling approach (https://github.com/jkyberneees/ana/blob/master/libs/response-extensions.js#L29), it basically indicates the class of the error for developers convenience. This is only available on the response event of restana.

Basically this approach can be revisit, as we have a new global error handler listener now available.

Would you have another input here?

Thanks and Regards

nandanugg commented 5 years ago

Hmm, to be honest, I can't find any use case where frontend need a error class. Because of that, do you have any plan to support custom error response template in the future? Or it's already implemented?

jkyberneees commented 5 years ago

Hi @nandanugg , not sure if we are in the same page here. This feature is for backend devs to process errors on their API.

Can you describe in more details what do you mean with a "custom error response template" feature?

Regards

nandanugg commented 5 years ago

in response-extensions.js there is an assignment like:

data = {
      errClass: data.constructor.name,
      code,
      message: data.message,
      data: data.data
    }

And I have your api gateway with error handler, I want to use this to tell client if anything goes wrong

restana: {
    errorHandler: (err, req, res) => {
      const modifiedErr = err;
      modifiedErr.code = 400;
      modifiedErr.status = false;
      modifiedErr.data = null;
      res.send(err);
    },
  },

After I test it, I get the reply

{
    "errClass": "TypeError", // Client don't need this
    "code": 400,
    "message": "",
    "data": null
}

I'm so sorry if I post this issue on wrong repo, I just see the root cause is on this repo.

jkyberneees commented 5 years ago

Hi @nandanugg, this is the correct repo. Thanks.

Can you please just try:

restana: {
    errorHandler: (err, req, res) => {
      ...
      res.send({
          status: false,
          message: err.message
      }, 400);
    },
  }
  ...

I guess removing the err reference will fix issue.