apollo-error-converter icon indicating copy to clipboard operation
apollo-error-converter copied to clipboard

More customization for get-map-item

Open unlight opened this issue 4 years ago • 4 comments

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:

  1. Check for originalError.constructor.name
  2. Check status
  3. Check message

Ideal way is to have custom function which accepts originalError and returns key.

unlight avatar Dec 05 '20 15:12 unlight

@the-vampiire Will you accept PR?

unlight avatar Jan 13 '21 11:01 unlight

Sure as long as it has tests and passes existing tests. What changes do you propose?

the-vampiire avatar Jan 13 '21 18:01 the-vampiire

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.

unlight avatar Jan 13 '21 20:01 unlight

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.

the-vampiire avatar Jan 22 '21 04:01 the-vampiire