-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.js
74 lines (59 loc) · 2.08 KB
/
errors.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
'use strict';
function MMError(code, msg) {
return function MMError(error, message) {
const err = new Error();
err.code = code;
err.message = msg;
if (message) {
err.message += '. ' + message;
}
if (error) {
if (error.message) {
err.message += ': ' + error.message;
} else {
err.data = error.data || error;
}
}
return err;
}
}
const errors = {
////general error to return from your methods
Call: { code: 4000, message: 'Error' },
//auth errors
Unauthorized: { code: 4100, message: 'Unauthorized' },
ProviderNotFound: { code: 4104, message: 'Auth provider not found' },
Forbidden: { code: 4110, message: 'Forbidden' },
NotValidToken: { code: 4120, message: 'Token vnot alid or expired' },
//validation errors
RequestValidation: { code: 4200, message: 'Request validation failed' },
ResponseValidation: { code: 4210, message: 'Response validation failed' },
RequestTooLarge: { code: 4220, message: 'Request entity too large' },
RequestEncode: { code: 4230, message: 'Request encode error' },
RequestDecode: { code: 4235, message: 'Request decode error' },
ResponseEncode: { code: 4240, message: 'Response encode error' },
ResponseDecode: { code: 4245, message: 'Response decode error' },
UnsupportedMedia: { code: 4250, message: 'Unsupported media' },
NoFilesInRequest: { code: 4255, message: 'No files found in the request' },
//call errors
MethodNotFound: { code: 4400, message: 'Method not found' },
//response errors
Duplicate: { code: 4500, message: 'Duplicate entity' },
NotFound: { code: 4540, message: 'Not found' },
//server errors
ServerError: { code: 5000, message: 'Server error' },
NetworkError: { code: 5100, message: 'Network error' }
}
const genericErrors = Object.entries(errors).reduce((exp, [ name, value ]) => {
exp[name] = MMError(value.code, value.message);
return exp;
}, {})
const Errors = Object.entries(errors).reduce((exp, [ name, value ]) => {
exp[name] = value.code;
return exp;
}, {})
module.exports = {
Error: MMError,
Errors,
...genericErrors
};