我一直试图从一个羽毛服务查找调用的承诺中引发apollo服务器错误。但是错误总是出现在
process.on('unhandledRejection', (reason, p) =>
logger.error('Unhandled Rejection at: Promise ', p, reason)
);
但是如果我直接从我的突变中扔掉它。转到graphql ui
import * as commonModule from './../../../commonModule/index.js';
export default function(Address,USER,mutationName){
const usersResolvers = {
Mutation:{
`registerUser`: (root, args, context) => {
console.log()
let errors= [];
errors = errors.concat(commonModule.validator.numberValidator(args.cardNumber));
errors = errors.concat(commonModule.validator.stringValidator(args.name, 48, 'name'));
errors = errors.concat(commonModule.validator.checkKeyExists(args.loginIdentifiers,'mobile'));
errors = errors.concat(commonModule.validator.loginIdentifierValidator(args.loginIdentifiers));
//THIS ERROR IS THROWN PROPERLY
if(errors.length) throw new commonModule.GenericError('Validation Error',errors);
//Check if user already exists with the mobile number
USER.find({query:{"MOBILE_NUMBER":args.loginIdentifiers.mobile,'$limit': 1}}).then((res) => {
//BUT THIS ERROR JUST LOGS A UNHANDLED REJECTION
if(res.length) throw new commonModule.GenericError('User Already Exists',[{key:'User',message:'user already exists'}]);
},err => {
console.log(err);
})
}
}
在我正在执行的通用错误类中
import { ApolloError } from 'apollo-server-express';
class GenericError extends ApolloError {
constructor(message,errors) {
super(message);
this.params = 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;
}, {});
}
}
export default GenericError;
我只想从我的服务器抛出apollo服务器错误。我该怎么办?