backend-error 0.0.6 β†’ 1.0.2

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.
Files changed (2) hide show
  1. package/README.md +20 -47
  2. package/package.json +2 -6
package/README.md CHANGED
@@ -1,13 +1,9 @@
1
- <center>
2
-
3
1
  # Backend-error
4
2
 
5
- Simple logging
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/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
- </center>
5
+ [![GitHub package.json version (master)](https://img.shields.io/github/package-json/v/eriksturesson/backendError/master)](https://github.com/eriksturesson/backendError)
6
+ [![npm](https://img.shields.io/npm/dy/backend-error?label=npm%20downloads)](https://www.npmjs.com/package/backend-error)
11
7
 
12
8
  ## Installation
13
9
 
@@ -48,9 +44,21 @@ Properties available:
48
44
  - `severity`: "info" | "warning" | "error" | "critical"
49
45
  - `data`: Additional metadata (optional and anything accepted)
50
46
 
51
- You can extend it for custom domains too.
47
+ ## 🧠 Example where you also import `httpErrorFormatter`:
52
48
 
53
- ## 🧠 Example: With Express + showUser handling
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
+ ```
60
+
61
+ ## 🧠 Example of manual showUser handling (done automatically in httpErrorFormatter above)
54
62
 
55
63
  ```ts
56
64
  import { BackendError } from "backend-error";
@@ -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 "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/cloud-logger)
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,8 +9,8 @@
12
9
  "email": "hej@eriksturesson.se",
13
10
  "url": "https://eriksturesson.se"
14
11
  },
15
- "version": "0.0.6",
16
- "description": "Logger library supporting multiple cloud platforms with simple error handling.",
12
+ "version": "1.0.2",
13
+ "description": "Error library supporting multiple cloud platforms with simple error handling.",
17
14
  "keywords": [
18
15
  "error",
19
16
  "fail",
@@ -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": {