errore 0.6.0 → 0.7.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 +72 -0
- package/dist/{index.mjs → index.cjs} +48 -2
- package/dist/index.js +2 -48
- package/package.json +2 -1
- /package/dist/{index.d.mts → index.d.cts} +0 -0
package/README.md
CHANGED
|
@@ -67,6 +67,78 @@ if (errore.isError(user)) {
|
|
|
67
67
|
console.log(user.name)
|
|
68
68
|
```
|
|
69
69
|
|
|
70
|
+
## Example: API Error Handling
|
|
71
|
+
|
|
72
|
+
A complete example with custom base class, HTTP status codes, and error reporting:
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import * as errore from 'errore'
|
|
76
|
+
|
|
77
|
+
// Base class with shared functionality
|
|
78
|
+
class AppError extends Error {
|
|
79
|
+
statusCode: number = 500
|
|
80
|
+
|
|
81
|
+
toResponse() {
|
|
82
|
+
return { error: this.message, code: this.statusCode }
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Specific errors with status codes
|
|
87
|
+
class NotFoundError extends errore.TaggedError('NotFoundError', AppError)<{
|
|
88
|
+
resource: string
|
|
89
|
+
message: string
|
|
90
|
+
}>() {
|
|
91
|
+
statusCode = 404
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
class ValidationError extends errore.TaggedError('ValidationError', AppError)<{
|
|
95
|
+
field: string
|
|
96
|
+
message: string
|
|
97
|
+
}>() {
|
|
98
|
+
statusCode = 400
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
class UnauthorizedError extends errore.TaggedError('UnauthorizedError', AppError)<{
|
|
102
|
+
message: string
|
|
103
|
+
}>() {
|
|
104
|
+
statusCode = 401
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Service function
|
|
108
|
+
async function updateUser(
|
|
109
|
+
userId: string,
|
|
110
|
+
data: { email?: string }
|
|
111
|
+
): Promise<NotFoundError | ValidationError | UnauthorizedError | User> {
|
|
112
|
+
const session = await getSession()
|
|
113
|
+
if (!session) {
|
|
114
|
+
return new UnauthorizedError({ message: 'Not logged in' })
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const user = await db.users.find(userId)
|
|
118
|
+
if (!user) {
|
|
119
|
+
return new NotFoundError({ resource: 'user', message: `User ${userId} not found` })
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (data.email && !isValidEmail(data.email)) {
|
|
123
|
+
return new ValidationError({ field: 'email', message: 'Invalid email format' })
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return db.users.update(userId, data)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// API handler
|
|
130
|
+
app.post('/users/:id', async (req, res) => {
|
|
131
|
+
const result = await updateUser(req.params.id, req.body)
|
|
132
|
+
|
|
133
|
+
if (errore.isError(result)) {
|
|
134
|
+
// All errors have toResponse() from AppError base
|
|
135
|
+
return res.status(result.statusCode).json(result.toResponse())
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return res.json(result)
|
|
139
|
+
})
|
|
140
|
+
```
|
|
141
|
+
|
|
70
142
|
## API
|
|
71
143
|
|
|
72
144
|
### Type Guards
|
|
@@ -1,3 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
TaggedError: () => TaggedError,
|
|
24
|
+
UnhandledError: () => UnhandledError,
|
|
25
|
+
andThen: () => andThen,
|
|
26
|
+
andThenAsync: () => andThenAsync,
|
|
27
|
+
flatten: () => flatten,
|
|
28
|
+
isError: () => isError,
|
|
29
|
+
isOk: () => isOk,
|
|
30
|
+
isTaggedError: () => isTaggedError,
|
|
31
|
+
map: () => map,
|
|
32
|
+
mapError: () => mapError,
|
|
33
|
+
match: () => match,
|
|
34
|
+
matchError: () => matchError,
|
|
35
|
+
matchErrorPartial: () => matchErrorPartial,
|
|
36
|
+
partition: () => partition,
|
|
37
|
+
tap: () => tap,
|
|
38
|
+
tapAsync: () => tapAsync,
|
|
39
|
+
tryAsync: () => tryAsync,
|
|
40
|
+
tryFn: () => tryFn,
|
|
41
|
+
unwrap: () => unwrap,
|
|
42
|
+
unwrapOr: () => unwrapOr
|
|
43
|
+
});
|
|
44
|
+
module.exports = __toCommonJS(index_exports);
|
|
45
|
+
|
|
1
46
|
// src/error.ts
|
|
2
47
|
var serializeCause = (cause) => {
|
|
3
48
|
if (cause instanceof Error) {
|
|
@@ -191,7 +236,8 @@ function partition(values) {
|
|
|
191
236
|
function flatten(value) {
|
|
192
237
|
return value;
|
|
193
238
|
}
|
|
194
|
-
export
|
|
239
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
240
|
+
0 && (module.exports = {
|
|
195
241
|
TaggedError,
|
|
196
242
|
UnhandledError,
|
|
197
243
|
andThen,
|
|
@@ -212,4 +258,4 @@ export {
|
|
|
212
258
|
tryFn,
|
|
213
259
|
unwrap,
|
|
214
260
|
unwrapOr
|
|
215
|
-
};
|
|
261
|
+
});
|
package/dist/index.js
CHANGED
|
@@ -1,48 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
TaggedError: () => TaggedError,
|
|
24
|
-
UnhandledError: () => UnhandledError,
|
|
25
|
-
andThen: () => andThen,
|
|
26
|
-
andThenAsync: () => andThenAsync,
|
|
27
|
-
flatten: () => flatten,
|
|
28
|
-
isError: () => isError,
|
|
29
|
-
isOk: () => isOk,
|
|
30
|
-
isTaggedError: () => isTaggedError,
|
|
31
|
-
map: () => map,
|
|
32
|
-
mapError: () => mapError,
|
|
33
|
-
match: () => match,
|
|
34
|
-
matchError: () => matchError,
|
|
35
|
-
matchErrorPartial: () => matchErrorPartial,
|
|
36
|
-
partition: () => partition,
|
|
37
|
-
tap: () => tap,
|
|
38
|
-
tapAsync: () => tapAsync,
|
|
39
|
-
tryAsync: () => tryAsync,
|
|
40
|
-
tryFn: () => tryFn,
|
|
41
|
-
unwrap: () => unwrap,
|
|
42
|
-
unwrapOr: () => unwrapOr
|
|
43
|
-
});
|
|
44
|
-
module.exports = __toCommonJS(index_exports);
|
|
45
|
-
|
|
46
1
|
// src/error.ts
|
|
47
2
|
var serializeCause = (cause) => {
|
|
48
3
|
if (cause instanceof Error) {
|
|
@@ -236,8 +191,7 @@ function partition(values) {
|
|
|
236
191
|
function flatten(value) {
|
|
237
192
|
return value;
|
|
238
193
|
}
|
|
239
|
-
|
|
240
|
-
0 && (module.exports = {
|
|
194
|
+
export {
|
|
241
195
|
TaggedError,
|
|
242
196
|
UnhandledError,
|
|
243
197
|
andThen,
|
|
@@ -258,4 +212,4 @@ function flatten(value) {
|
|
|
258
212
|
tryFn,
|
|
259
213
|
unwrap,
|
|
260
214
|
unwrapOr
|
|
261
|
-
}
|
|
215
|
+
};
|
package/package.json
CHANGED
|
File without changes
|