Merge pull request #425 from trouble/graphql-errors

gql errors include formatted field validation data
This commit is contained in:
Dan Ribbens
2020-10-20 09:36:27 -04:00
committed by GitHub
5 changed files with 41 additions and 40 deletions

View File

@@ -1,22 +1,28 @@
const errorHandler = async (info, debug, afterErrorHook) => {
return Promise.all(info.result.errors.map(async (err) => {
// TODO: use payload logging
console.error(err.stack);
/**
*
* @param info
* @param debug
* @param afterErrorHook
* @returns {Promise<unknown[]>}
*/
const errorHandler = async (info, debug, afterErrorHook) => Promise.all(info.result.errors.map(async (err) => {
// TODO: use payload logging
console.error(err.stack);
let response = {
...err,
};
let response = {
message: err.message,
data: err?.originalError?.data,
};
if (afterErrorHook) {
({ response } = await afterErrorHook(err, response) || { response });
}
if (afterErrorHook) {
({ response } = await afterErrorHook(err, response) || { response });
}
if (debug && debug === true) {
response.stack = err.stack;
}
if (debug && debug === true) {
response.stack = err.stack;
}
return response;
}));
};
return response;
}));
module.exports = errorHandler;

View File

@@ -20,6 +20,7 @@ const initCollections = require('../collections/graphql/init');
const initGlobals = require('../globals/graphql/init');
const buildWhereInputType = require('./schema/buildWhereInputType');
const access = require('../auth/graphql/resolvers/access');
const errorHandler = require('./errorHandler');
class InitializeGraphQL {
constructor(init) {
@@ -93,31 +94,23 @@ class InitializeGraphQL {
this.schema = new GraphQLSchema(schema);
// this.errorExtensions = [];
// this.errorExtensionIteration = 0;
// this.extensions = async (info) => {
// const { result } = info;
// if (result.errors) {
// const afterErrorHook = typeof this.config.hooks.afterError === 'function' ? this.config.hooks.afterError : null;
// this.errorExtensions = await errorHandler(info, this.config.debug, afterErrorHook);
// }
// return null;
// };
this.extensions = async (info) => {
const { result } = info;
if (result.errors) {
const afterErrorHook = typeof this.config.hooks.afterError === 'function' ? this.config.hooks.afterError : null;
this.errorResponse = await errorHandler(info, this.config.debug, afterErrorHook);
}
return null;
};
}
init(req, res) {
this.errorResponse = null;
return graphQLHTTP(
async (request, response, { variables }) => ({
schema: this.schema,
// customFormatErrorFn: () => {
// const response = {
// ...this.errorExtensions[this.errorExtensionIteration],
// };
// this.errorExtensionIteration += 1;
// return response;
// },
// extensions: this.extensions,
customFormatErrorFn: () => (this.errorResponse),
extensions: this.extensions,
context: { req, res },
validationRules: [
queryComplexity({