@wnodex/errors 0.2.1 → 0.2.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.
@@ -1,7 +1,7 @@
1
1
 
2
- > @wnodex/errors@0.2.1 build /home/runner/work/wnodex/wnodex/packages/errors
2
+ > @wnodex/errors@0.2.2 build /home/runner/work/wnodex/wnodex/packages/errors
3
3
  > rolldown -c && tsc
4
4
 
5
5
  [log] <DIR>/index.js chunk │ size: 6.67 kB
6
6
  [log]
7
- [success] rolldown v1.0.0-rc.1 Finished in 49.36 ms
7
+ [success] rolldown v1.0.0-rc.1 Finished in 5.43 ms
package/README.md CHANGED
@@ -1,14 +1,89 @@
1
1
  # @wnodex/errors
2
2
 
3
- wnodex custom application errors
3
+ > wnodex custom application errors
4
4
 
5
- ---
5
+ Part of the [wnodex](https://github.com/wnodex/wnodex) ecosystem, this package provides custom error classes and a centralized error handler.
6
6
 
7
- ## Table of Content
7
+ ## About
8
8
 
9
- - [License](#license)
9
+ `@wnodex/errors` provides a structured approach to error handling in `wnodex` applications. It includes a set of custom error classes (`HttpError`, `ConfigError`, `ValidationError`) and a global error handling middleware that ensures consistent, predictable error responses.
10
10
 
11
- ---
11
+ ## Features
12
+
13
+ - **`errorHandler` middleware**: A global error handler that catches exceptions and sends formatted JSON error responses.
14
+ - **`HttpError` class**: For creating errors with a specific HTTP status code.
15
+ - **`BaseError` class**: A base for creating custom, structured errors with codes and context.
16
+ - **`ValidationError` & `ConfigError`**: Specialized error classes for common application failure scenarios.
17
+ - Hides stack traces in production for security.
18
+
19
+ ## Why use it?
20
+
21
+ Structured error handling is crucial for building robust APIs. This package enforces a consistent error response format, making it easier for clients to handle errors. By using custom error classes, your application code becomes more expressive and easier todebug. The global handler prevents unhandled exceptions from crashing the server.
22
+
23
+ ## Installation
24
+
25
+ You can install the package using your favorite package manager:
26
+
27
+ **pnpm**
28
+
29
+ ```bash
30
+ pnpm add @wnodex/errors
31
+ ```
32
+
33
+ **npm**
34
+
35
+ ```bash
36
+ npm install @wnodex/errors
37
+ ```
38
+
39
+ **yarn**
40
+
41
+ ```bash
42
+ yarn add @wnodex/errors
43
+ ```
44
+
45
+ **bun**
46
+
47
+ ```bash
48
+ bun add @wnodex/errors
49
+ ```
50
+
51
+ ## Usage
52
+
53
+ The `errorHandler` middleware is automatically registered as the last middleware by `wnodex`, so there is no need to configure it. You can use the exported error classes throughout your application.
54
+
55
+ ```typescript
56
+ import { Wnodex } from 'wnodex';
57
+ import { HttpError } from '@wnodex/errors';
58
+
59
+ const server = new Wnodex({
60
+ port: 3000,
61
+ });
62
+
63
+ const app = server.getApp();
64
+
65
+ app.get('/users/:id', (req, res) => {
66
+ if (req.params.id === '0') {
67
+ throw new HttpError('User not found', 404);
68
+ }
69
+ res.send({ id: req.params.id, name: 'John Doe' });
70
+ });
71
+
72
+ server.start();
73
+
74
+ // Request to /users/0 will result in a 404 response:
75
+ // {
76
+ // "error": {
77
+ // "message": "User not found"
78
+ // }
79
+ // }
80
+ ```
81
+
82
+ ### Custom Error Classes
83
+
84
+ - **`HttpError(message, statusCode)`**: Throws an error that results in a specific HTTP status.
85
+ - **`ValidationError(message, cause, context)`**: For data validation failures.
86
+ - **`ConfigError(message, cause, context)`**: For application configuration issues.
12
87
 
13
88
  ## License
14
89
 
package/package.json CHANGED
@@ -1,11 +1,15 @@
1
1
  {
2
2
  "name": "@wnodex/errors",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "private": false,
5
- "description": "wnodex custom application errors",
5
+ "description": "Provides custom error classes and a centralized error handler for wnodex applications.",
6
6
  "keywords": [
7
7
  "wnodex",
8
- "errors"
8
+ "errors",
9
+ "error-handling",
10
+ "middleware",
11
+ "express",
12
+ "http-error"
9
13
  ],
10
14
  "homepage": "https://github.com/wnodex/wnodex#readme",
11
15
  "bugs": {
@@ -38,7 +42,7 @@
38
42
  "@types/node": "^25.0.10",
39
43
  "rolldown": "1.0.0-rc.1",
40
44
  "typescript": "5.9.2",
41
- "@wnodex/typescript-config": "0.2.1"
45
+ "@wnodex/typescript-config": "0.2.2"
42
46
  },
43
47
  "publishConfig": {
44
48
  "access": "public"