mirror of
https://github.com/vale981/Vulcan
synced 2025-03-05 09:31:43 -05:00
Simplify error handling
This commit is contained in:
parent
26899c7ff6
commit
448f9c16b8
2 changed files with 19 additions and 31 deletions
|
@ -18,6 +18,8 @@ const getFirstWord = input => {
|
||||||
|
|
||||||
Parse a GraphQL error message
|
Parse a GraphQL error message
|
||||||
|
|
||||||
|
TODO: check if still useful?
|
||||||
|
|
||||||
Sample message:
|
Sample message:
|
||||||
|
|
||||||
"GraphQL error: Variable "$data" got invalid value {"meetingDate":"2018-08-07T06:05:51.704Z"}.
|
"GraphQL error: Variable "$data" got invalid value {"meetingDate":"2018-08-07T06:05:51.704Z"}.
|
||||||
|
@ -28,6 +30,11 @@ In field "addresses": Expected "[JSON]!", found null."
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const parseErrorMessage = message => {
|
const parseErrorMessage = message => {
|
||||||
|
|
||||||
|
if (!message) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// note: optionally add .slice(1) at the end to get rid of the first error, which is not that helpful
|
// note: optionally add .slice(1) at the end to get rid of the first error, which is not that helpful
|
||||||
let fieldErrors = message.split('\n');
|
let fieldErrors = message.split('\n');
|
||||||
|
|
||||||
|
@ -52,6 +59,7 @@ const parseErrorMessage = message => {
|
||||||
});
|
});
|
||||||
return fieldErrors;
|
return fieldErrors;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
Errors can have the following properties stored on their `data` property:
|
Errors can have the following properties stored on their `data` property:
|
||||||
|
@ -60,37 +68,17 @@ Errors can have the following properties stored on their `data` property:
|
||||||
- properties: additional data. Will be passed to vulcan-i18n as values
|
- properties: additional data. Will be passed to vulcan-i18n as values
|
||||||
- message: if id cannot be used as i81n key, message will be used
|
- message: if id cannot be used as i81n key, message will be used
|
||||||
|
|
||||||
Scenario 1: normal error thrown with new Error(), put it in array and return it
|
|
||||||
|
|
||||||
Scenario 2: multiple GraphQL errors stored on data.errors
|
|
||||||
|
|
||||||
Scenario 3: single GraphQL error with data property
|
|
||||||
|
|
||||||
Scenario 4: single GraphQL error with no data property
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
export const getErrors = error => {
|
export const getErrors = error => {
|
||||||
|
|
||||||
// 1. by default, return raw error wrapped in array
|
const graphQLErrors = error.graphQLErrors;
|
||||||
let errors = [error];
|
|
||||||
|
// error thrown using new ApolloError
|
||||||
|
const apolloErrors = get(graphQLErrors, '0.extensions.exception.data.errors');
|
||||||
|
|
||||||
|
// regular server error (with schema stitching)
|
||||||
|
const regularErrors = get(graphQLErrors, '0.extensions.exception.errors');
|
||||||
|
|
||||||
|
return apolloErrors || regularErrors || graphQLErrors;
|
||||||
|
|
||||||
// if this is one or more GraphQL errors, extract and convert them
|
|
||||||
if (error.graphQLErrors && error.graphQLErrors.length > 0) {
|
|
||||||
// get first graphQL error (see https://github.com/thebigredgeek/apollo-errors/issues/12)
|
|
||||||
const graphQLError = error.graphQLErrors[0];
|
|
||||||
const data = get(graphQLError, 'extensions.exception.data')
|
|
||||||
if (data && !isEmpty(data)) {
|
|
||||||
if (data.errors) {
|
|
||||||
// 2. there are multiple errors on the data.errors object
|
|
||||||
errors = data.errors;
|
|
||||||
} else {
|
|
||||||
// 3. there is only one error
|
|
||||||
errors = [data];
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// 4. there is no data object, try to parse raw error message
|
|
||||||
errors = parseErrorMessage(graphQLError.message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return errors;
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { ApolloError } from 'apollo-server';
|
import { UserInputError } from 'apollo-server';
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
|
@ -11,5 +11,5 @@ An error should have:
|
||||||
*/
|
*/
|
||||||
export const throwError = error => {
|
export const throwError = error => {
|
||||||
const { id, } = error;
|
const { id, } = error;
|
||||||
throw new ApolloError(id, 'VALIDATION_ERROR', error);
|
throw new UserInputError(id, error);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue