errore 0.6.0 → 0.7.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 +74 -2
- package/dist/{index.mjs → index.cjs} +50 -2
- package/dist/{index.d.mts → index.d.cts} +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +3 -48
- package/package.json +2 -1
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
|
|
@@ -89,10 +161,10 @@ if (errore.isError(result)) {
|
|
|
89
161
|
import * as errore from 'errore'
|
|
90
162
|
|
|
91
163
|
// Sync - wraps exceptions in UnhandledError
|
|
92
|
-
const parsed = errore.
|
|
164
|
+
const parsed = errore.try(() => JSON.parse(input))
|
|
93
165
|
|
|
94
166
|
// Sync - with custom error type
|
|
95
|
-
const parsed = errore.
|
|
167
|
+
const parsed = errore.try({
|
|
96
168
|
try: () => JSON.parse(input),
|
|
97
169
|
catch: e => new ParseError({ cause: e })
|
|
98
170
|
})
|
|
@@ -1,3 +1,49 @@
|
|
|
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
|
+
try: () => tryFn,
|
|
40
|
+
tryAsync: () => tryAsync,
|
|
41
|
+
tryFn: () => tryFn,
|
|
42
|
+
unwrap: () => unwrap,
|
|
43
|
+
unwrapOr: () => unwrapOr
|
|
44
|
+
});
|
|
45
|
+
module.exports = __toCommonJS(index_exports);
|
|
46
|
+
|
|
1
47
|
// src/error.ts
|
|
2
48
|
var serializeCause = (cause) => {
|
|
3
49
|
if (cause instanceof Error) {
|
|
@@ -191,7 +237,8 @@ function partition(values) {
|
|
|
191
237
|
function flatten(value) {
|
|
192
238
|
return value;
|
|
193
239
|
}
|
|
194
|
-
export
|
|
240
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
241
|
+
0 && (module.exports = {
|
|
195
242
|
TaggedError,
|
|
196
243
|
UnhandledError,
|
|
197
244
|
andThen,
|
|
@@ -208,8 +255,9 @@ export {
|
|
|
208
255
|
partition,
|
|
209
256
|
tap,
|
|
210
257
|
tapAsync,
|
|
258
|
+
try: null,
|
|
211
259
|
tryAsync,
|
|
212
260
|
tryFn,
|
|
213
261
|
unwrap,
|
|
214
262
|
unwrapOr
|
|
215
|
-
};
|
|
263
|
+
});
|
|
@@ -321,4 +321,4 @@ declare function partition<V>(values: V[]): [Exclude<V, Error>[], Extract<V, Err
|
|
|
321
321
|
*/
|
|
322
322
|
declare function flatten<V>(value: V): V;
|
|
323
323
|
|
|
324
|
-
export { type EnsureNotError, type Errore, type InferError, type InferValue, TaggedError, type TaggedErrorClass, type TaggedErrorInstance, UnhandledError, andThen, andThenAsync, flatten, isError, isOk, isTaggedError, map, mapError, match, matchError, matchErrorPartial, partition, tap, tapAsync, tryAsync, tryFn, unwrap, unwrapOr };
|
|
324
|
+
export { type EnsureNotError, type Errore, type InferError, type InferValue, TaggedError, type TaggedErrorClass, type TaggedErrorInstance, UnhandledError, andThen, andThenAsync, flatten, isError, isOk, isTaggedError, map, mapError, match, matchError, matchErrorPartial, partition, tap, tapAsync, tryFn as try, tryAsync, tryFn, unwrap, unwrapOr };
|
package/dist/index.d.ts
CHANGED
|
@@ -321,4 +321,4 @@ declare function partition<V>(values: V[]): [Exclude<V, Error>[], Extract<V, Err
|
|
|
321
321
|
*/
|
|
322
322
|
declare function flatten<V>(value: V): V;
|
|
323
323
|
|
|
324
|
-
export { type EnsureNotError, type Errore, type InferError, type InferValue, TaggedError, type TaggedErrorClass, type TaggedErrorInstance, UnhandledError, andThen, andThenAsync, flatten, isError, isOk, isTaggedError, map, mapError, match, matchError, matchErrorPartial, partition, tap, tapAsync, tryAsync, tryFn, unwrap, unwrapOr };
|
|
324
|
+
export { type EnsureNotError, type Errore, type InferError, type InferValue, TaggedError, type TaggedErrorClass, type TaggedErrorInstance, UnhandledError, andThen, andThenAsync, flatten, isError, isOk, isTaggedError, map, mapError, match, matchError, matchErrorPartial, partition, tap, tapAsync, tryFn as try, tryAsync, tryFn, unwrap, unwrapOr };
|
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,
|
|
@@ -254,8 +208,9 @@ function flatten(value) {
|
|
|
254
208
|
partition,
|
|
255
209
|
tap,
|
|
256
210
|
tapAsync,
|
|
211
|
+
tryFn as try,
|
|
257
212
|
tryAsync,
|
|
258
213
|
tryFn,
|
|
259
214
|
unwrap,
|
|
260
215
|
unwrapOr
|
|
261
|
-
}
|
|
216
|
+
};
|