apollo-error-converter
apollo-error-converter copied to clipboard
More customization for get-map-item
Currently custom error can be catched by name, code or type.
/**
* Retrieves a MapItem for converting the Error
* - the MapItem can be associated by error.[name, code, type]
* @param {Error} originalError original Error object
*/
function getMapItem(originalError) {
const { name, code, type } = originalError;
return (
this.errorMap[name] || this.errorMap[code] || this.errorMap[type] || null
);
}
module.exports = getMapItem;
Unfortunately this is not enough using NestJS + GraphQL.
Typical error object in NestJS may look like:
{
"response": {
"statusCode": 400,
"message": [
"body should not be empty"
],
"error": "Bad Request"
},
"status": 400,
"message": "Bad Request Exception"
}
name = 'Error'
code = undefined
type = undefined
Possible ways to improve, to add more checks:
- Check for
originalError.constructor.name - Check
status - Check
message
Ideal way is to have custom function which accepts originalError and returns key.
@the-vampiire Will you accept PR?
Sure as long as it has tests and passes existing tests. What changes do you propose?
Sure as long as it has tests and passes existing tests. What changes do you propose?
Check for originalError.constructor.name
I'll try to create PR on the weekends.
deal way is to have custom function which accepts originalError and returns key.
This is a great idea and really rounds out the modularity of the tool. I'm open to adding it as a separate PR after I merge this one in.
This option can be added and just fall back to these checks as default behavior.