Skip to content

Commit 9144f7e

Browse files
committed
custom error in graphql server
1 parent 81f8d9f commit 9144f7e

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

server.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ app.use('/login', cors(), (req, res)=>{
8787
context: {
8888
request: req,
8989
acl: acl
90+
},
91+
formatError(error){
92+
return {
93+
message: error.message,
94+
details: error.originalError.errors,
95+
path: error.path
96+
};
9097
}
9198
})));
9299

utils/errors.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
class customArrayError extends Error {
3+
constructor(errors_array, message){
4+
super();
5+
this.message = message;
6+
this.errors = errors_array;
7+
}
8+
}
9+
10+
class otherError extends Error {
11+
constructor(oneError,message){
12+
super();
13+
this.message = message;
14+
this.detail = oneError;
15+
}
16+
}
17+
18+
handleError = function(error){
19+
if(error.name === "SequelizeValidationError"){
20+
throw new customArrayError(error.errors, "Validation error");
21+
}else{
22+
throw new Error(error)
23+
}
24+
}
25+
26+
module.exports = { handleError}

0 commit comments

Comments
 (0)