@shivaedev/errors 0.1.0 → 0.1.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.
- package/README.md +18 -8
- package/dist/boundary.d.ts.map +1 -1
- package/dist/boundary.js +20 -3
- package/dist/boundary.js.map +1 -1
- package/package.json +5 -5
- package/src/boundary.ts +27 -3
package/README.md
CHANGED
|
@@ -31,9 +31,9 @@ import { Rpc } from "effect/rpc";
|
|
|
31
31
|
import { BadRequest, Conflict } from "@shivaedev/errors/taxonomy.ts";
|
|
32
32
|
|
|
33
33
|
export const Rename = Rpc.make("Rename", {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
error: Schema.Union([BadRequest, Conflict]),
|
|
35
|
+
payload: { name: Schema.String },
|
|
36
|
+
success: Schema.String,
|
|
37
37
|
});
|
|
38
38
|
```
|
|
39
39
|
|
|
@@ -41,22 +41,32 @@ A handler fails with `new Conflict({ field: "name", message: "Name is taken" })`
|
|
|
41
41
|
|
|
42
42
|
### Mapping unexpected failures at the handler edge
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
Use `rpcBoundary` only at the RPC handler edge, never inside services. Services keep their typed failures and defects so callers can handle them. At that edge, `rpcBoundary(schema, options)` keeps declared rejections and maps other failures and defects into one declared fallback. It leaves successful values and interruption intact:
|
|
45
45
|
|
|
46
46
|
```ts
|
|
47
47
|
import { Schema } from "effect";
|
|
48
|
+
import { Rpc, RpcGroup } from "effect/rpc";
|
|
48
49
|
import { Conflict, InternalServerError } from "@shivaedev/errors/taxonomy.ts";
|
|
49
50
|
import { rpcBoundary } from "@shivaedev/errors/boundary.ts";
|
|
50
51
|
|
|
51
52
|
const Rejection = Schema.Union([Conflict, InternalServerError]);
|
|
52
53
|
const expose = rpcBoundary(Rejection, {
|
|
53
|
-
|
|
54
|
+
unexpected: (message) => new InternalServerError({ message }),
|
|
54
55
|
});
|
|
55
56
|
|
|
56
|
-
|
|
57
|
+
const Api = RpcGroup.make(
|
|
58
|
+
Rpc.make("Save", {
|
|
59
|
+
error: Rejection,
|
|
60
|
+
success: Schema.String,
|
|
61
|
+
}),
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
export const Handlers = Api.toLayer({
|
|
65
|
+
Save: () => storageSave.pipe(expose),
|
|
66
|
+
});
|
|
57
67
|
```
|
|
58
68
|
|
|
59
|
-
`storageSave` is the application's Effect. `unexpected` constructs a member of the declared schema from the public message. The default message is `Internal server error`; `message` supplies application text, with credential-shaped strings redacted. The original Effect cause is kept on the server rejection's non-enumerable `cause`, so a server span can trace it. Keep `cause` out of the wire schema, as the taxonomy classes do. `ServiceUnavailable` describes a declared availability failure without implying a session-provider outage.
|
|
69
|
+
`storageSave` is the application's Effect. `unexpected` constructs a member of the declared schema from the public message. The default message is `Internal server error`; `message` supplies application text, with credential-shaped strings redacted. Before masking an unexpected failure, the boundary reports its diagnostic-redacted cause through Effect's registered `ErrorReporter`s at `Error` severity. Request tracing logs that masked failure at ERROR with its redacted stack, while declared rejections stay at INFO. The original Effect cause is kept on the server rejection's non-enumerable `cause`, so a server span can trace it. Keep `cause` out of the wire schema, as the taxonomy classes do. `ServiceUnavailable` describes a declared availability failure without implying a session-provider outage.
|
|
60
70
|
|
|
61
71
|
### Masking secrets in failures
|
|
62
72
|
|
|
@@ -64,7 +74,7 @@ export const save = storageSave.pipe(expose);
|
|
|
64
74
|
|
|
65
75
|
`redact.ts` applies that policy to structured values. `redact(value, sensitive?)` masks values under sensitive keys and credential text inside strings, summarises large collections and binary data, and stops cyclic payloads at a depth limit; use it for diagnostics. `redactWire(value, sensitive?)` applies the same masking without summaries or truncation; use it for failure data sent to a client.
|
|
66
76
|
|
|
67
|
-
`redactCause.ts` applies it to Effect causes. `redactCause(cause, sensitive?)` and `redactDiagnosticCause(cause, sensitive?)`
|
|
77
|
+
`redactCause.ts` applies it to Effect causes. `redactCause(cause, sensitive?)` rebuilds a cause with masked defects and keeps its typed failures, interruptions and annotations as they are, so the failure channel keeps its type. `redactDiagnosticCause(cause, sensitive?)` masks failures, defects and annotations for diagnostics, `redactDefect(defect, sensitive?)` masks one defect, and `redactingErrorReporter(reporter, sensitive?)` wraps an Effect error reporter so it only ever receives diagnostic-masked causes.
|
|
68
78
|
|
|
69
79
|
The policy matches names, not values: it does not scan prose for personal data, so keep secrets out of free text and span names.
|
|
70
80
|
|
package/dist/boundary.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"boundary.d.ts","sourceRoot":"","sources":["../src/boundary.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"boundary.d.ts","sourceRoot":"","sources":["../src/boundary.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,MAAM,EAA4B,MAAM,EAAE,MAAM,QAAQ,CAAC;AAqBlF,MAAM,WAAW,kBAAkB,CAAC,CAAC;IACpC,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,UAAU,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,CAAC,CAAC;CAC5C;AACD,wBAAgB,WAAW,CAC1B,OAAO,SAAS,MAAM,CAAC,iBAAiB,CAAC;IACxC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CACzB,CAAC,EACD,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,kBAAkB,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,IAEtD,CAAC,EAAE,CAAC,EAAE,CAAC,QAAQ,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,KAAG,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAkBpF"}
|
package/dist/boundary.js
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
|
-
import { Cause, Effect, Schema } from "effect";
|
|
1
|
+
import { Cause, Context, Effect, ErrorReporter, Predicate, Schema } from "effect";
|
|
2
|
+
import { redactDiagnosticCause } from "#redactCause.ts";
|
|
2
3
|
import { isSensitiveKey, redactText } from "#sensitive.ts";
|
|
4
|
+
function reportable(value) {
|
|
5
|
+
return Object.assign(Predicate.isObject(value) ? value : new Error(String(value)), { [ErrorReporter.severity]: "Error" });
|
|
6
|
+
}
|
|
7
|
+
function reportUnexpected(cause) {
|
|
8
|
+
const diagnostic = redactDiagnosticCause(cause);
|
|
9
|
+
return ErrorReporter.report(Cause.fromReasons(diagnostic.reasons.map((reason) => {
|
|
10
|
+
if (Cause.isInterruptReason(reason)) {
|
|
11
|
+
return reason;
|
|
12
|
+
}
|
|
13
|
+
const reported = Cause.isFailReason(reason) ? Cause.makeFailReason(reportable(reason.error)) : Cause.makeDieReason(reportable(reason.defect));
|
|
14
|
+
return reported.annotate(Context.makeUnsafe(reason.annotations));
|
|
15
|
+
})));
|
|
16
|
+
}
|
|
3
17
|
export function rpcBoundary(schema, options) {
|
|
4
18
|
const declared = Schema.is(schema);
|
|
5
19
|
return (self) => Effect.catchCause(self, (cause) => {
|
|
@@ -11,8 +25,11 @@ export function rpcBoundary(schema, options) {
|
|
|
11
25
|
return Effect.fail(reason.error);
|
|
12
26
|
}
|
|
13
27
|
const rejection = options.unexpected(redactText(options.message ?? "Internal server error", isSensitiveKey));
|
|
14
|
-
Object.
|
|
15
|
-
|
|
28
|
+
Object.defineProperties(rejection, {
|
|
29
|
+
cause: { configurable: true, enumerable: false, value: cause },
|
|
30
|
+
[ErrorReporter.ignore]: { configurable: true, enumerable: false, value: true },
|
|
31
|
+
});
|
|
32
|
+
return reportUnexpected(cause).pipe(Effect.andThen(Effect.failCause(Cause.fromReasons([...Cause.fail(rejection).reasons, ...cause.reasons.filter(Cause.isInterruptReason)]))));
|
|
16
33
|
});
|
|
17
34
|
}
|
|
18
35
|
//# sourceMappingURL=boundary.js.map
|
package/dist/boundary.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"boundary.js","sourceRoot":"","sources":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AAK,EAAE,KAAK,EAAE,IAAI,EAAE;SAC9E,CAAC,CAAC;QACH,OAAO,gBAAgB,CAAC,KAAK,CAAC,CAAC,IAAI,CAClC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,OAAO,EAAE,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAAC,CACzI,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shivaedev/errors",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Typed expected failures and secret-safe failure reporting for Effect applications",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"effect"
|
|
@@ -44,15 +44,15 @@
|
|
|
44
44
|
"README.md"
|
|
45
45
|
],
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@shivaedev/quality": "0.
|
|
48
|
-
"@shivaedev/test": "0.
|
|
47
|
+
"@shivaedev/quality": "0.46.0",
|
|
48
|
+
"@shivaedev/test": "0.11.0",
|
|
49
49
|
"@types/node": "24.19.2",
|
|
50
|
-
"effect": "4.0.
|
|
50
|
+
"effect": "4.0.6",
|
|
51
51
|
"typescript": "7.0.2",
|
|
52
52
|
"vitest": "5.0.5"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
|
-
"effect": "4.0.
|
|
55
|
+
"effect": "4.0.6"
|
|
56
56
|
},
|
|
57
57
|
"engines": {
|
|
58
58
|
"node": ">=24"
|
package/src/boundary.ts
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
|
-
import { Cause, Effect, Schema } from "effect";
|
|
1
|
+
import { Cause, Context, Effect, ErrorReporter, Predicate, Schema } from "effect";
|
|
2
|
+
import { redactDiagnosticCause } from "#redactCause.ts";
|
|
2
3
|
import { isSensitiveKey, redactText } from "#sensitive.ts";
|
|
4
|
+
|
|
5
|
+
function reportable(value: unknown) {
|
|
6
|
+
return Object.assign(Predicate.isObject(value) ? value : new Error(String(value)), { [ErrorReporter.severity]: "Error" });
|
|
7
|
+
}
|
|
8
|
+
function reportUnexpected(cause: Cause.Cause<unknown>): Effect.Effect<void> {
|
|
9
|
+
const diagnostic = redactDiagnosticCause(cause);
|
|
10
|
+
return ErrorReporter.report(
|
|
11
|
+
Cause.fromReasons(
|
|
12
|
+
diagnostic.reasons.map((reason) => {
|
|
13
|
+
if (Cause.isInterruptReason(reason)) {
|
|
14
|
+
return reason;
|
|
15
|
+
}
|
|
16
|
+
const reported = Cause.isFailReason(reason) ? Cause.makeFailReason(reportable(reason.error)) : Cause.makeDieReason(reportable(reason.defect));
|
|
17
|
+
return reported.annotate(Context.makeUnsafe(reason.annotations));
|
|
18
|
+
}),
|
|
19
|
+
),
|
|
20
|
+
);
|
|
21
|
+
}
|
|
3
22
|
export interface RpcBoundaryOptions<E> {
|
|
4
23
|
readonly message?: string;
|
|
5
24
|
readonly unexpected: (message: string) => E;
|
|
@@ -20,7 +39,12 @@ export function rpcBoundary<
|
|
|
20
39
|
return Effect.fail(reason.error);
|
|
21
40
|
}
|
|
22
41
|
const rejection = options.unexpected(redactText(options.message ?? "Internal server error", isSensitiveKey));
|
|
23
|
-
Object.
|
|
24
|
-
|
|
42
|
+
Object.defineProperties(rejection, {
|
|
43
|
+
cause: { configurable: true, enumerable: false, value: cause },
|
|
44
|
+
[ErrorReporter.ignore]: { configurable: true, enumerable: false, value: true },
|
|
45
|
+
});
|
|
46
|
+
return reportUnexpected(cause).pipe(
|
|
47
|
+
Effect.andThen(Effect.failCause(Cause.fromReasons([...Cause.fail(rejection).reasons, ...cause.reasons.filter(Cause.isInterruptReason)]))),
|
|
48
|
+
);
|
|
25
49
|
});
|
|
26
50
|
}
|