0

I am new to feathers and am building an API, generated with feathers-cli. If the client performs an invalid GET request:

eg. http://localhost:3030/stations/?asdfasdf

it returns a 500 error:

ER_BAD_FIELD_ERROR: Unknown column 'stations.asdfasdf' in 'where clause'

I'd rather not report an error like that back to the client and would instead like to return a '400 Bad Request' instead. I've tried setting up an after hook using hook.error but this doesn't catch the sequelize error.

How can I catch the error and return a safer, more generic message to the client?

4

1 回答 1

1

error钩子是一个单独的新钩子类型。使用 1.x feathers-cli 更改您的服务索引文件,例如

// Set up our before hooks
messageService.before(hooks.before);

// Set up our after hooks
messageService.after(hooks.after);

// Set up hooks
messageService.hooks(hooks);

然后在hooks/index.js 文件中添加

exports.error = {
  all: [],
  find: [],
  get: [],
  create: [],
  update: [],
  patch: [],
  remove: []
};

您现在可以使用它来创建错误挂钩。对于你这样的情况:

常量错误 = 要求('羽毛错误');

exports.error = {
  all: [
    function(hook) {
      if(is(hook.error, 'ER_BAD_FIELD_ERROR')) { // Somehow check the Sequelize error type
        hook.error = new errors.BadRequest('Invalid query field');
      }
    }
  ],
  find: [],
  get: [],
  create: [],
  update: [],
  patch: [],
  remove: []
};
于 2017-03-18T20:41:50.260 回答