【问题标题】:How to use GraphQLError for customize messaging?如何使用 GraphQLError 自定义消息?
【发布时间】:2019-01-14 06:34:03
【问题描述】:

我正在尝试使用 GraphQLError 自定义消息传递。

我想用 GraphQL 错误处理几个用例:

  • 当用户名和密码不匹配时,我想返回自定义用户名和密码不匹配的消息。

  • 当用户输入无效邮箱时,我想返回自定义输入邮箱无效的消息。

  • 还有一些其他用例。

我创建了一个 ValidateError.js 文件来使用 GraphQLError 处理函数:

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;
      }, {});
  }
}

这是我的应用程序索引文件 app.js 的代码:

app.use('/graphql', graphqlExpress(req => ({
  schema,
  context: {
    user: req.user
  },
  formatError(err) {
    return {
      message: err.message,
      code: err.originalError && err.originalError.code,   
      locations: err.locations,
      path: err.path
    };
  }
})));

我的问题是如何使用这个函数来抓取graphQLError

格式错误

提前致谢。

【问题讨论】:

    标签: graphql express-graphql


    【解决方案1】:

    "apollo-server-express": "^1.3.5"

    "graphql": "^0.13.2"

    只需在resolver 中抛出您的错误,formatError 函数将捕获解析器中抛出的每个错误。

    这是我的作品:

    appError.js

    class AppError extends Error {
      constructor(opts) {
        super(opts.msg);
        this.code = opts.code;
      }
    }
    
    exports.AppError = AppError;
    

    resolver 中抛出自定义错误:

    throw new AppError({ msg: 'authorization failed', code: 1001 });

    formatError 中捕获此错误:

      formatError: error => {
        const { code, message } = error.originalError;
        return { code, message };
      },
    

    其他示例:

    resolver 中抛出你的错误:

    const resolvers = {
      Query: {
        books: () => {
          throw new GraphQLError('something bad happened');
        }
      }
    };
    

    formatError 中捕获错误:

    graphqlExpress(req => {
        return {
          schema,
          formatError: err => {
            console.log('format error');
            return err;
          }
        };
      })
    

    这是输出:

    format error
    GraphQLError: something bad happened
        at books (/Users/ldu020/workspace/apollo-server-express-starter/src/graphql-error/index.js:23:13)
        at /Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql-tools/dist/schemaGenerator.js:518:26
        at resolveFieldValueOrError (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/execution/execute.js:531:18)
        at resolveField (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/execution/execute.js:495:16)
        at /Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/execution/execute.js:364:18
        at Array.reduce (<anonymous>)
        at executeFields (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/execution/execute.js:361:42)
        at executeOperation (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/execution/execute.js:289:122)
        at executeImpl (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/execution/execute.js:154:14)
        at Object.execute (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/execution/execute.js:131:229)
    

    【讨论】:

      【解决方案2】:

      在 GraphQLError 内部,您应该可以访问 GraphQLErrorExtensions,它应该允许您在抛出错误时附加自定义消息。

      这个答案是根据阿波罗服务器通过这个扩展选项抛出自定义错误的知识编写的。这可能是可以实现的,但更复杂。我建议检查 apollo 服务器错误:https://github.com/apollographql/apollo-server/blob/main/packages/apollo-server-errors/src/index.ts


      看起来你可以做的只是通过扩展传递任何额外的信息,但你只能在构造函数中设置它:new GraphQLError('your error message', null, null, null, null, null, {"message1": [ "custom message1" ], "message2": [ "customer message1", "custom message2" ]})

      【讨论】:

        猜你喜欢
        • 2015-09-22
        • 2022-01-21
        • 2020-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-05
        • 2021-08-20
        • 2021-11-14
        相关资源
        最近更新 更多