apollo-error-converter
Version:
Global Apollo Server Error handling made easy. Remove verbose and repetitive resolver / data source Error handling. Ensures no implementation details are ever leaked while preserving internal Error logging.
32 lines (28 loc) • 818 B
JavaScript
const { toApolloError } = require("apollo-server-core");
const { defaultFallback } = require("../core/constants");
/**
* Extracts and shapes the arguments for formatting the Error
*
* defaults:
* - data = null
* - code = 'INTERNAL_SERVER_ERROR'
* @param {GraphQLError} error GraphQL Error from formatError
* @param {MapItem} mapItem MapItem to parse
* @returns {[object]} arguments list
*/
function createApolloError(graphQLError, mapItem) {
const { originalError, path, locations } = graphQLError;
const { message, data = {}, code = defaultFallback.code } = mapItem;
return toApolloError(
{
path,
message,
locations,
extensions: {
data: typeof data === "function" ? data(originalError) : data,
},
},
code,
);
}
module.exports = createApolloError;