clean-error-maker 1.0.0
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 +50 -0
- package/index.js +17 -0
- package/package.json +18 -0
- package/test/demo.js +17 -0
package/Readme.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# clean-error-maker
|
|
2
|
+
|
|
3
|
+
A tiny utility for creating consistent, structured HTTP errors in Node.js and Express.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
npm install clean-error-maker
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
const { httpError } = require("clean-error-maker");
|
|
15
|
+
|
|
16
|
+
throw httpError(404, "User not found");
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Example Output
|
|
20
|
+
|
|
21
|
+
```json
|
|
22
|
+
{
|
|
23
|
+
"status": 404,
|
|
24
|
+
"message": "User not found",
|
|
25
|
+
"success": false,
|
|
26
|
+
"timestamp": "2025-12-12T15:00:10.000Z",
|
|
27
|
+
"data": null
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## With Extra Data
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
throw httpError(400, "Invalid input", { field: "email" });
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Why Use This?
|
|
38
|
+
|
|
39
|
+
* Consistent error structure
|
|
40
|
+
* Ready for Express API responses
|
|
41
|
+
* Includes timestamp
|
|
42
|
+
* Preserves stack trace
|
|
43
|
+
* Supports custom data
|
|
44
|
+
|
|
45
|
+
## Ideal For
|
|
46
|
+
|
|
47
|
+
* Backend APIs
|
|
48
|
+
* Express.js apps
|
|
49
|
+
* Microservices
|
|
50
|
+
* Clean error handling
|
package/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// clean-error-maker
|
|
2
|
+
|
|
3
|
+
function httpError(status = 500, message = "Something went wrong", data = null) {
|
|
4
|
+
const error = new Error(message);
|
|
5
|
+
|
|
6
|
+
error.status = status;
|
|
7
|
+
error.success = false;
|
|
8
|
+
error.timestamp = new Date().toISOString();
|
|
9
|
+
|
|
10
|
+
if (data) {
|
|
11
|
+
error.data = data;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return error;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default { httpError };
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "clean-error-maker",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A tiny utility to create consistent and structured HTTP errors for Node.js and Express applications.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type":"module",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"http-error",
|
|
9
|
+
"error",
|
|
10
|
+
"express",
|
|
11
|
+
"nodejs",
|
|
12
|
+
"backend",
|
|
13
|
+
"error-handler",
|
|
14
|
+
"structured-error"
|
|
15
|
+
],
|
|
16
|
+
"author": "YOUR NAME",
|
|
17
|
+
"license": "MIT"
|
|
18
|
+
}
|
package/test/demo.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// test/demo.js
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
const { httpError } = require(join("..", "index"));
|
|
4
|
+
|
|
5
|
+
try {
|
|
6
|
+
throw httpError(400, "Invalid input", { field: "email" });
|
|
7
|
+
} catch (err) {
|
|
8
|
+
// Collect all own property names (includes stack & message)
|
|
9
|
+
const out = {};
|
|
10
|
+
Object.getOwnPropertyNames(err).forEach((k) => {
|
|
11
|
+
out[k] = err[k];
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
// Print a clean JSON representation
|
|
15
|
+
console.log("=== Serialized error ===");
|
|
16
|
+
console.log(JSON.stringify(out, null, 2));
|
|
17
|
+
}
|