backend-error 0.0.5 β 1.0.1
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 +23 -50
- package/package.json +1 -5
package/README.md
CHANGED
|
@@ -1,18 +1,14 @@
|
|
|
1
|
-
<center>
|
|
2
|
-
|
|
3
1
|
# Backend-error
|
|
4
2
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
<img alt="GitHub package.json version (master)" src="https://img.shields.io/github/package-json/v/eriksturesson/backendError/master">
|
|
8
|
-
<img alt="npm" src="https://img.shields.io/npm/dy/@eriksturesson/backend-error?label=npm%20downloads">
|
|
3
|
+
BackendError is a lightweight utility for structured and user-aware error handling in Node.js backends. It helps distinguish operational errors from unexpected crashes, and supports standardized error responses across services.
|
|
9
4
|
|
|
10
|
-
|
|
5
|
+
[](https://github.com/eriksturesson/backendError)
|
|
6
|
+
[](https://www.npmjs.com/package/backend-error)
|
|
11
7
|
|
|
12
8
|
## Installation
|
|
13
9
|
|
|
14
10
|
```bash
|
|
15
|
-
npm install
|
|
11
|
+
npm install backend-error
|
|
16
12
|
```
|
|
17
13
|
|
|
18
14
|
## π₯ Custom BackendError class
|
|
@@ -22,7 +18,7 @@ Use `BackendError` class for standardized backend error handling:
|
|
|
22
18
|
## Usage
|
|
23
19
|
|
|
24
20
|
```ts
|
|
25
|
-
import { BackendError } from "
|
|
21
|
+
import { BackendError } from "backend-error";
|
|
26
22
|
|
|
27
23
|
throw BackendError.BadRequest("Missing required field");
|
|
28
24
|
```
|
|
@@ -48,12 +44,24 @@ Properties available:
|
|
|
48
44
|
- `severity`: "info" | "warning" | "error" | "critical"
|
|
49
45
|
- `data`: Additional metadata (optional and anything accepted)
|
|
50
46
|
|
|
51
|
-
|
|
47
|
+
## π§ Example where you also import `httpErrorFormatter`:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { BackendError, httpErrorFormatter } from "backend-error";
|
|
51
|
+
try {
|
|
52
|
+
const user = null;
|
|
53
|
+
if (!user) throw BackendError.NotFound("User not found");
|
|
54
|
+
res.json(user);
|
|
55
|
+
} catch (err) {
|
|
56
|
+
const { status, body } = await httpErrorFormatter(err); //returns status and body
|
|
57
|
+
res.status(status).json({ body });
|
|
58
|
+
}
|
|
59
|
+
```
|
|
52
60
|
|
|
53
|
-
## π§ Example
|
|
61
|
+
## π§ Example of manual showUser handling (done automatically in httpErrorFormatter above)
|
|
54
62
|
|
|
55
63
|
```ts
|
|
56
|
-
import { BackendError } from "
|
|
64
|
+
import { BackendError } from "backend-error";
|
|
57
65
|
|
|
58
66
|
app.get("/user/:id", async (req, res, next) => {
|
|
59
67
|
try {
|
|
@@ -66,7 +74,6 @@ app.get("/user/:id", async (req, res, next) => {
|
|
|
66
74
|
} else {
|
|
67
75
|
res.status(500).json({ error: "Internal Server Error" });
|
|
68
76
|
}
|
|
69
|
-
next(err);
|
|
70
77
|
}
|
|
71
78
|
});
|
|
72
79
|
```
|
|
@@ -82,42 +89,6 @@ app.get("/user/:id", async (req, res, next) => {
|
|
|
82
89
|
- `BackendError.Internal(message: string)` // 500, showUser: false
|
|
83
90
|
- `BackendError.ServiceUnavailable(message: string)` // 503, showUser: false
|
|
84
91
|
|
|
85
|
-
## π§© httpErrorFormatter(error) β Format backend errors for HTTP responses
|
|
86
|
-
|
|
87
|
-
This helper takes an Error (or BackendError) and returns a plain object with:
|
|
88
|
-
|
|
89
|
-
β
status β an HTTP status code (number)
|
|
90
|
-
|
|
91
|
-
β
body β a JSON.stringify'd string representing the error (already parsed)
|
|
92
|
-
|
|
93
|
-
Itβs designed to be simple and universal β you can use it with any framework (Azure Functions, Express, etc).
|
|
94
|
-
|
|
95
|
-
π§ Example usage
|
|
96
|
-
|
|
97
|
-
```ts
|
|
98
|
-
import { BackendError, httpErrorFormatter } from "@eriksturesson/backend-error";
|
|
99
|
-
|
|
100
|
-
try {
|
|
101
|
-
throw BackendError.Internal("Something went very wrong."); // π your static factory pattern
|
|
102
|
-
} catch (err) {
|
|
103
|
-
const { status, body } = await httpErrorFormatter(err);
|
|
104
|
-
|
|
105
|
-
return {
|
|
106
|
-
status,
|
|
107
|
-
headers: {
|
|
108
|
-
...getCorsHeaders(request.headers.get("origin")), // Add CORS headers yourself
|
|
109
|
-
},
|
|
110
|
-
body,
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
β οΈ Important
|
|
116
|
-
This function does not include any HTTP headers β especially no CORS headers.
|
|
117
|
-
Why? Because every environment has different CORS rules.
|
|
118
|
-
|
|
119
|
-
If you're using Azure Functions, Express, or something else, you'll need to add CORS manually:
|
|
120
|
-
|
|
121
92
|
## π§© Types
|
|
122
93
|
|
|
123
94
|
```ts
|
|
@@ -135,11 +106,13 @@ export interface BackendErrorOptions {
|
|
|
135
106
|
}
|
|
136
107
|
```
|
|
137
108
|
|
|
109
|
+
> π¬ Tip: This package doesn't handle headers or CORS. If you're building an API for browsers, remember to configure CORS separately.
|
|
110
|
+
|
|
138
111
|
---
|
|
139
112
|
|
|
140
113
|
## π Repo
|
|
141
114
|
|
|
142
|
-
[https://github.com/eriksturesson/backendError](https://github.com/eriksturesson/
|
|
115
|
+
[https://github.com/eriksturesson/backendError](https://github.com/eriksturesson/backendError)
|
|
143
116
|
|
|
144
117
|
---
|
|
145
118
|
|
package/package.json
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "backend-error",
|
|
3
|
-
"publishConfig": {
|
|
4
|
-
"access": "public"
|
|
5
|
-
},
|
|
6
3
|
"license": "MIT",
|
|
7
4
|
"maintainers": [
|
|
8
5
|
"Erik Sturesson"
|
|
@@ -12,7 +9,7 @@
|
|
|
12
9
|
"email": "hej@eriksturesson.se",
|
|
13
10
|
"url": "https://eriksturesson.se"
|
|
14
11
|
},
|
|
15
|
-
"version": "
|
|
12
|
+
"version": "1.0.1",
|
|
16
13
|
"description": "Logger library supporting multiple cloud platforms with simple error handling.",
|
|
17
14
|
"keywords": [
|
|
18
15
|
"error",
|
|
@@ -46,7 +43,6 @@
|
|
|
46
43
|
},
|
|
47
44
|
"dependencies": {
|
|
48
45
|
"dotenv": "^16.5.0",
|
|
49
|
-
"npm": "^11.4.1",
|
|
50
46
|
"typescript": "^4.7.4"
|
|
51
47
|
},
|
|
52
48
|
"exports": {
|