0

我尝试做的是在用户登录失败时使用 GraphQLError 添加自定义消息。

这是 app.js 文件中的 GraphQL 端点代码:

app.use('/graphql', graphqlExpress(req => ({
  schema,
  context: {
    user: req.user
  },
  formatError: function (error) {
    console.log(error)
    return error
  }
})),function (err, req, res, next) {
  if (err.name === 'UnauthorizedError') { // Send the error rather than to show it on the console
    //console.log(err);
      res.status(401).send(err);
  }
  else {
      next(err);
  }
}
);

这是突变逻辑代码:

if (validator.isEmpty(input.input.username)) {
                    errors.push({ key: 'username', message: 'Username must not be empty.' });
                }
                if (validator.isEmpty(input.input.password)) {
                    errors.push({ key: 'password', message: 'Password must not be empty.' });
                }
                if (errors.length) throw new ValidationError(errors);

                UserModel.find({login:input.input.username,sts:true}).exec((err,result)=>{
                    if(err){throw new ValidationError(err);}

                    if(!result.length)
                    {
                        errors.push({ key: 'Auth', message: 'User doesn not exists.' });
                        //if (errors.length) throw new ValidationError(errors);
                        callback(null,errors);

                    }

                });

这是 ValidationError.js 文件代码:

const { GraphQLError } = require('graphql');

module.exports  = class ValidationError extends GraphQLError {
  constructor(errors) {

    super('The request is invalid');

    var err = errors.reduce((result, error) => {

        if (Object.prototype.hasOwnProperty.call(result, error.key)) {
          result[error.key].push(error.message);
        } else {
          result[error.key] = [error.message];
        }

        return result;
      }, {});
        //this.message=err;
    //this.data = [{test:1}];
  }
}

我正在尝试执行以下 GraphQL 突变查询:

mutation{
  login(input:{username:"test",password:"1234"}) {
    _id,
    name{
      fname,
      lname,
      mname
    }
  }
}

它在 GraphQL 窗口中给我输出

{
  "data": {
    "login": null
  }
}

终端中出现未捕获的异常错误:

[ { key: 'Auth', message: 'User doesn not exists.' } ]
Sun, 05 Aug 2018 08:27:57 GMT uncaughtException: The request is invalid
GraphQLError: The request is invalid
    at new ValidationError (/Volumes/Drive B/dev/zapi/graphql/ValidationError.js:6:5)
    at UserModel.find.exec (/Volumes/Drive B/dev/zapi/graphql/mutations/user/login.js:56:50)
    at /Volumes/Drive B/dev/zapi/node_modules/mongoose/lib/model.js:4454:16
    at (anonymous function).call (/Volumes/Drive B/dev/zapi/node_modules/mongoose/lib/query.js:3395:7)
    at cb (/Volumes/Drive B/dev/zapi/node_modules/mongoose/lib/query.js:1434:14)
    at result (/Volumes/Drive B/dev/zapi/node_modules/mongodb/lib/utils.js:414:17)
    at executeCallback (/Volumes/Drive B/dev/zapi/node_modules/mongodb/lib/utils.js:406:9)
    at handleCallback (/Volumes/Drive B/dev/zapi/node_modules/mongodb/lib/utils.js:128:55)
    at cursor.close (/Volumes/Drive B/dev/zapi/node_modules/mongodb/lib/operations/cursor_ops.js:218:62)
    at handleCallback (/Volumes/Drive B/dev/zapi/node_modules/mongodb/lib/utils.js:128:55)
    at completeClose (/Volumes/Drive B/dev/zapi/node_modules/mongodb/lib/cursor.js:887:14)
    at Cursor.close (/Volumes/Drive B/dev/zapi/node_modules/mongodb/lib/cursor.js:906:10)
    at cursor._next (/Volumes/Drive B/dev/zapi/node_modules/mongodb/lib/operations/cursor_ops.js:218:23)
    at handleCallback (/Volumes/Drive B/dev/zapi/node_modules/mongodb-core/lib/cursor.js:202:5)
    at _setCursorNotifiedImpl (/Volumes/Drive B/dev/zapi/node_modules/mongodb-core/lib/cursor.js:560:38)
    at self._endSession (/Volumes/Drive B/dev/zapi/node_modules/mongodb-core/lib/cursor.js:568:46)
    at Cursor._endSession (/Volumes/Drive B/dev/zapi/node_modules/mongodb-core/lib/cursor.js:193:5)
    at Cursor._endSession (/Volumes/Drive B/dev/zapi/node_modules/mongodb/lib/cursor.js:226:59)
    at _setCursorNotifiedImpl (/Volumes/Drive B/dev/zapi/node_modules/mongodb-core/lib/cursor.js:568:17)
    at setCursorNotified (/Volumes/Drive B/dev/zapi/node_modules/mongodb-core/lib/cursor.js:560:3)
    at /Volumes/Drive B/dev/zapi/node_modules/mongodb-core/lib/cursor.js:687:16
    at queryCallback (/Volumes/Drive B/dev/zapi/node_modules/mongodb-core/lib/cursor.js:267:16)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! zapi@1.0.0 start: `DEBUG=my-zapi,express* node ./bin/www`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the zapi@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /var/root/.npm/_logs/2018-08-05T08_27_57_178Z-debug.log

为什么它给我未捕获的异常错误?请帮忙

提前致谢。

4

0 回答 0