backend-error 1.0.2 → 1.0.4
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/README.md
CHANGED
|
@@ -53,8 +53,8 @@ try {
|
|
|
53
53
|
if (!user) throw BackendError.NotFound("User not found");
|
|
54
54
|
res.json(user);
|
|
55
55
|
} catch (err) {
|
|
56
|
-
const { status, body } = await httpErrorFormatter(err);
|
|
57
|
-
res.status(status).json(
|
|
56
|
+
const { status, body, message, showUser } = await httpErrorFormatter(err);
|
|
57
|
+
res.status(status).json(body);
|
|
58
58
|
}
|
|
59
59
|
```
|
|
60
60
|
|
|
@@ -1,4 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
2
13
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
14
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
15
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -38,24 +49,41 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
38
49
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
50
|
exports.httpErrorFormatter = void 0;
|
|
40
51
|
var BackendError_1 = require("./BackendError");
|
|
52
|
+
function isValidStatusCode(code) {
|
|
53
|
+
return typeof code === "number" && Number.isInteger(code) && code >= 100 && code <= 599;
|
|
54
|
+
}
|
|
41
55
|
function httpErrorFormatter(_a) {
|
|
42
56
|
var err = _a.err;
|
|
43
57
|
return __awaiter(this, void 0, void 0, function () {
|
|
44
|
-
var
|
|
58
|
+
var status_1, showUser_1, message_1, body_1, status, message, showUser, maybeStatus, body;
|
|
45
59
|
return __generator(this, function (_b) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
status_1 =
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
60
|
+
// Handle your custom BackendError type
|
|
61
|
+
if (err instanceof BackendError_1.BackendError) {
|
|
62
|
+
status_1 = isValidStatusCode(err.code) ? err.code : 400;
|
|
63
|
+
showUser_1 = typeof err.showUser === "boolean" ? err.showUser : status_1 < 500;
|
|
64
|
+
message_1 = err.message || "Error";
|
|
65
|
+
body_1 = showUser_1
|
|
66
|
+
? __assign(__assign({ message: message_1 }, (err.data !== undefined && { data: err.data })), { code: status_1, severity: err.severity }) : { message: "Internal Server Error" };
|
|
67
|
+
return [2 /*return*/, { status: status_1, body: body_1, showUser: showUser_1, message: message_1 }];
|
|
68
|
+
}
|
|
69
|
+
status = 500;
|
|
70
|
+
message = "Internal Server Error";
|
|
71
|
+
showUser = false;
|
|
72
|
+
if (err instanceof Error) {
|
|
73
|
+
message = err.message;
|
|
74
|
+
maybeStatus = err.status || err.code;
|
|
75
|
+
if (isValidStatusCode(maybeStatus)) {
|
|
76
|
+
status = maybeStatus;
|
|
77
|
+
showUser = status < 500; // show message for 4xx errors by default
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
else if (typeof err === "string") {
|
|
81
|
+
message = err;
|
|
82
|
+
status = 400;
|
|
83
|
+
showUser = true;
|
|
53
84
|
}
|
|
54
|
-
|
|
55
|
-
return [2 /*return*/, {
|
|
56
|
-
status: 500,
|
|
57
|
-
body: JSON.stringify({ message: message }),
|
|
58
|
-
}];
|
|
85
|
+
body = showUser ? { message: message } : { message: "Internal Server Error" };
|
|
86
|
+
return [2 /*return*/, { status: status, body: body, showUser: showUser, message: message }];
|
|
59
87
|
});
|
|
60
88
|
});
|
|
61
89
|
}
|
package/package.json
CHANGED
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
"email": "hej@eriksturesson.se",
|
|
10
10
|
"url": "https://eriksturesson.se"
|
|
11
11
|
},
|
|
12
|
-
"version": "1.0.
|
|
13
|
-
"description": "Error library
|
|
12
|
+
"version": "1.0.4",
|
|
13
|
+
"description": "Simple Error handling library.",
|
|
14
14
|
"keywords": [
|
|
15
15
|
"error",
|
|
16
16
|
"fail",
|