-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy patherror-methods.js
85 lines (79 loc) · 2 KB
/
error-methods.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var errorCode = require('./error-code');
module.exports = {
get: function (statusCode, error) {
var name;
if (error.code == "LOGIN_FAILED") {
name = "Error"
} else {
name = "ValidationError"
}
return ({
error: {
statusCode: statusCode,
name: name,
message: error.message,
code: error.code
}
})
},
validationError: function (statusCode, errors, model) {
console.log(errors);
var codes = [],
messages = [],
errorCode;
errors.map((error, i) => {
var errorConcat = "";
error.dataPath = error.dataPath.slice(1);
console.log("error.dataPath initial",error.dataPath);
if (!error.dataPath) {
error.dataPath = error.params.missingProperty;
}
// codes - array of codes
var errorKeyword = error.dataPath.split(".");
errorKeyword.map((values) => {
errorConcat += values + "_"
});
switch (error.keyword) {
case "required":
errorCode = (errorConcat + error.keyword).toUpperCase();
break;
case "format":
errorCode = errorConcat.toUpperCase() + "INVALID_FORMAT";
break;
case "type":
errorCode = errorConcat.toUpperCase() + "INVALID_TYPE";
break;
case "minLength":
case "maxLength":
case "maximum":
case "minimum":
errorCode = errorConcat.toUpperCase() + "INVALID_LENGTH";
break;
default:
errorCode = error.keyword.toUpperCase();
}
codes.push(errorCode);
let message = {};
message[errorCode] = error.message
messages.push(message);
})
return ({
error: {
statusCode: statusCode,
name: "ValidationError",
message: 'Unprocessable Entity',
details: {
context: model,
codes: codes,
messages: messages
}
}
})
},
getErrorCode: function (statusCodeValue) {
if (errorCode[statusCodeValue])
return errorCode[statusCodeValue];
else
return errorCode.INVALID_DATA
}
}