From 9144f7ef447b3fabd6d20f1f40aafb43b1d9d6ee Mon Sep 17 00:00:00 2001 From: vsuaste Date: Tue, 19 Feb 2019 15:30:04 -0600 Subject: [PATCH] custom error in graphql server --- server.js | 7 +++++++ utils/errors.js | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 utils/errors.js diff --git a/server.js b/server.js index 90eb79a..2a94d80 100644 --- a/server.js +++ b/server.js @@ -87,6 +87,13 @@ app.use('/login', cors(), (req, res)=>{ context: { request: req, acl: acl + }, + formatError(error){ + return { + message: error.message, + details: error.originalError.errors, + path: error.path + }; } }))); diff --git a/utils/errors.js b/utils/errors.js new file mode 100644 index 0000000..dd9b7ba --- /dev/null +++ b/utils/errors.js @@ -0,0 +1,26 @@ + +class customArrayError extends Error { + constructor(errors_array, message){ + super(); + this.message = message; + this.errors = errors_array; + } +} + +class otherError extends Error { + constructor(oneError,message){ + super(); + this.message = message; + this.detail = oneError; + } +} + +handleError = function(error){ + if(error.name === "SequelizeValidationError"){ + throw new customArrayError(error.errors, "Validation error"); + }else{ + throw new Error(error) + } +} + +module.exports = { handleError}