我应该在 GraphQL Scalar 中抛出一般错误吗?

Should I throw generic error in GraphQL Scalar?

我试图在 GraphQL 中定义标量类型,graphql-yoga 为服务器定义标量类型。问题是我试图决定在这种情况下我应该抛出 GraphQLError 还是只抛出 TypeError

目前,我正在使用通用错误。

export const URIScalar = new GraphQLScalarType({
  name: 'URI',
  description: 'A URI whose scheme is \'http\' or \'https\'',
  serialize(value) {
    if (isURI(value)) {
      return value;
    } else {
      throw new Error('URI format is invalid');
    }
  },
  parseValue(value) {
    if (isURI(value)) {
      return value;
    } else {
      throw new Error('URI format is invalid');
    }
  },
  parseLiteral(ast) {
    if (ast.kind === 'StringValue') {
      if (isURI(ast.value)) {
        return ast.value;
      } else {
        throw new Error('URI format is invalid');
      }
    } else {
      throw new Error('URI type must be string');
    }
  },
});

一般错误是完美的,因为其中的信息是有用的。

客户端将按预期收到错误,您将能够在软件中找到错误的来源。

自定义错误非常适合您需要更多相关数据,但您拥有的数据将起作用。