【发布时间】:2019-10-13 13:15:20
【问题描述】:
我正在尝试在 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');
}
},
});
【问题讨论】:
-
最好创建易于调试的自定义错误
标签: javascript error-handling graphql graphql-js