1

Let say if I throw GeneralError when call a service, how to make error object like what I want:

{
"status": "Failed",
"code": "500_1",
"detail": "Something is wrong with your API"
} 

I already try add this on error hook

hook => {
hook.error = {
"status": "Failed",
"code": "500_1",
"detail": "Something is wrong with your API"
} 
return hook
}

But still cannot, and still return default error object of feathers:

{
    "name": "GeneralError",
    "message": "Error",
    "code": 500,
    "className": "general-error",
    "data": {},
    "errors": {}
}

4 Answers4

1

You can create your own custom error.

Example:

const { FeathersError } = require('@feathersjs/errors');

class UnsupportedMediaType extends FeathersError {
  constructor(message, data) {
    super(message, 'unsupported-media-type', 415, 'UnsupportedMediaType', data);
  }
}

const error = new UnsupportedMediaType('Not supported');
console.log(error.toJSON());
Idan Dagan
  • 10,273
  • 5
  • 34
  • 41
1

As per @daff comment above, this is how you can customize the returned error object. Here includes extending built-in errors as well as a custom error

custom-errors.js

const { FeathersError } = require('@feathersjs/errors');

class CustomError extends FeathersError {
    constructor(message, name, code) {
        super(message, name, code);
    }

    toJSON() {
        return {
            status: "Failed",
            code: this.code,
            detail: this.message,
        }
    }
}

class BadRequest extends CustomError {
    constructor(message) {
        super(message, 'bad-request', 400);
    }
}

class NotAuthenticated extends CustomError {
    constructor(message) {
        super(message, 'not-authenticated', 401);
    }
}

class MyCustomError extends CustomError {
    constructor(message) {
        super(message, 'my-custom-error', 500_1);
    }
}

Throw error like

throw new MyCustomError('Something is wrong with your API');

Output (in Postman)

{
    "status": "Failed",
    "code": 500_1
    "detail": "Something is wrong with your API",
code
}
JBB
  • 317
  • 1
  • 9
0

All of the feathers errors can be provided with a custom message, which overrides the default message in the payload:

throw new GeneralError('The server is sleeping. Come back later.');

You can also pass additional data and/or errors. It's all documented: https://docs.feathersjs.com/api/errors.html

Joe
  • 41,484
  • 20
  • 104
  • 125
  • But if I do this it will just override the message, what I need is to override the whole error object so it can return this when error happen { "status": "Failed", "code": "500_1", "detail": "Something is wrong with your API" } – alimuddinhasan Nov 03 '19 at 04:41
  • 2
    You can still create a custom error (https://docs.feathersjs.com/api/errors.html#custom-errors) with its own toJSON method that returns the format you want. – Daff Nov 03 '19 at 06:02
0

To return error from a hook you need to return Promise.reject

const errors = require('@feathersjs/errors');

module.exports = function () {
  return async context => {
    if (yourtest) {
      return Promise.reject(new errors.NotAuthenticated('User authentication failed (0001)'));
    }
  };
};

This will give the response

enter image description here

Lars Ladegaard
  • 116
  • 1
  • 10