@sera4/essentia 1.0.9 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/formatter/index.js +80 -0
- package/index.js +4 -2
- package/logger/s4-logger.js +5 -2
- package/package.json +1 -1
- package/package.tar.gz +0 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
3
|
+
class FormatError extends Error {
|
|
4
|
+
constructor(m) {
|
|
5
|
+
super(m);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// helps to clean up response formats for services responding to rest requests
|
|
10
|
+
class S4Formatter {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.defaults = {
|
|
13
|
+
status: 422,
|
|
14
|
+
errors: {}
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
_isArray (value) {
|
|
19
|
+
return value && typeof value === 'object' && value.constructor === Array;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
_fromHash(error) {
|
|
23
|
+
let response = Object.assign({}, this.defaults);
|
|
24
|
+
|
|
25
|
+
if (error.status) {
|
|
26
|
+
let errorStatus = parseInt(error.status);
|
|
27
|
+
|
|
28
|
+
if ((errorStatus >= 200) && (errorStatus < 522)) {
|
|
29
|
+
response.status = errorStatus
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (error.error && (typeof(error.error) === "string")) {
|
|
34
|
+
response.errors["generic"] = { msg: error.error }
|
|
35
|
+
} else if (error.errors) {
|
|
36
|
+
if (this._isArray(error.errors)) {
|
|
37
|
+
// many legacy responses which are errors: ["error_name"]
|
|
38
|
+
error.errors.forEach(function(el, index) {
|
|
39
|
+
response.errors[`generic_${index}`] = { msg: el }
|
|
40
|
+
})
|
|
41
|
+
} else if (typeof(error.errors) === "object") {
|
|
42
|
+
response.errors = error.errors
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return response;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Desired Response format
|
|
51
|
+
*{
|
|
52
|
+
* "error": {
|
|
53
|
+
* "status": 422,
|
|
54
|
+
* "errors": {
|
|
55
|
+
* "password": {
|
|
56
|
+
* "location": "body",
|
|
57
|
+
* "param": "password",
|
|
58
|
+
* "value": "mypassword",
|
|
59
|
+
* "msg": "weak"
|
|
60
|
+
* }
|
|
61
|
+
* }
|
|
62
|
+
* },
|
|
63
|
+
* "error_id": "a2ddf895-2aaf-4189-88a2-8e11f6843f91"
|
|
64
|
+
*}
|
|
65
|
+
*/
|
|
66
|
+
formatError(error) {
|
|
67
|
+
let response;
|
|
68
|
+
if (error === null) {
|
|
69
|
+
response = this.defaults;
|
|
70
|
+
} else if (typeof(error) === "object") {
|
|
71
|
+
response = this._fromHash(error);
|
|
72
|
+
} else {
|
|
73
|
+
return new FormatError("invalid_error_format");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return response;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
module.exports = new S4Formatter();
|
package/index.js
CHANGED
|
@@ -3,5 +3,7 @@ module.exports = {
|
|
|
3
3
|
sqlPaginator : require("./paginator/sql-pagination"),
|
|
4
4
|
logger : require("./logger/s4-logger"),
|
|
5
5
|
lastCommit : require("./last_commit"),
|
|
6
|
-
halDecorator : require("./hal")
|
|
7
|
-
|
|
6
|
+
halDecorator : require("./hal"),
|
|
7
|
+
formatter : require("./formatter")
|
|
8
|
+
}
|
|
9
|
+
//console.log(module.exports.formatter.formatError({ status: 200, error: ["error1"] }));
|
package/logger/s4-logger.js
CHANGED
|
@@ -11,7 +11,7 @@ logger.add(winston.transports.Console, {
|
|
|
11
11
|
|
|
12
12
|
class S4Logger {
|
|
13
13
|
setLevel(level) {
|
|
14
|
-
if (level && ["debug", "info", "error"].indexOf(level.toLowerCase())) {
|
|
14
|
+
if (level && ["debug", "info", "error", "warn"].indexOf(level.toLowerCase())) {
|
|
15
15
|
logger.transports[loggerName].level = level;
|
|
16
16
|
}
|
|
17
17
|
}
|
|
@@ -24,6 +24,9 @@ class S4Logger {
|
|
|
24
24
|
debug(str, args) {
|
|
25
25
|
logger.log("debug", str, args);
|
|
26
26
|
}
|
|
27
|
+
warn(str, args) {
|
|
28
|
+
logger.log("warn", str, args);
|
|
29
|
+
}
|
|
27
30
|
info(str, args) {
|
|
28
31
|
logger.log("info", str, args);
|
|
29
32
|
}
|
|
@@ -32,4 +35,4 @@ class S4Logger {
|
|
|
32
35
|
}
|
|
33
36
|
}
|
|
34
37
|
|
|
35
|
-
module.exports = new S4Logger();
|
|
38
|
+
module.exports = new S4Logger();
|
package/package.json
CHANGED
package/package.tar.gz
CHANGED
|
Binary file
|