@zudojs/rpc 1.4.0 → 1.4.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 CHANGED
@@ -245,7 +245,7 @@ Every error class from `@zudojs/errors`' RPC family is re-exported. The
245
245
  server maps them to wire codes (`RPC_PROCEDURE_NOT_FOUND`,
246
246
  `RPC_VALIDATION_ERROR`, `RPC_UNAUTHENTICATED`, `RPC_FORBIDDEN`,
247
247
  `RPC_RATE_LIMITED`, `RPC_TIMEOUT`, …). A custom `RPCError` subclass keeps its
248
- own `code`.
248
+ own `code` only when it is built with `expose: true` (see below).
249
249
 
250
250
  `mapRPCError(error)` is the mapping the server and the fetch handler share. Use it in a custom transport to produce the same wire payloads.
251
251
 
@@ -262,7 +262,15 @@ thrown with `expose: false` — an `RPCInternalError`, an
262
262
  `expose: false`), a non-exposed `BaseError`, or any other error — is
263
263
  answered with the fixed `INTERNAL_ERROR_MESSAGE`; the
264
264
  original error is handed to `onInternalError(error, requestId)` so it can be
265
- logged against the request id. A handler result that fails the procedure's
265
+ logged against the request id.
266
+
267
+ A non-exposed error's code is withheld as well: `new RPCError("…", { code:
268
+ "TASK_SECRET" })` goes out as `{ code: "RPC_INTERNAL_ERROR", message:
269
+ INTERNAL_ERROR_MESSAGE }`, because a custom code is server detail just as the
270
+ message is. The one exception is a standard wire code — a key of
271
+ `RPC_HTTP_STATUS`, such as `RPC_UNAVAILABLE` or `RPC_TIMEOUT` — which is
272
+ public vocabulary a client acts on (retries, status), so it travels with the
273
+ generic message. To send a custom code, build the error with `expose: true`. A handler result that fails the procedure's
266
274
  `output` schema is treated the same way: it is the server's fault, not the
267
275
  caller's.
268
276
 
@@ -13,4 +13,14 @@ import type { RPCErrorPayload } from "../types/rpcResponse.type.js";
13
13
  * safe. Returns `undefined` for anything else, which stays internal.
14
14
  */
15
15
  export declare function mapExposedBaseError(error: unknown): RPCErrorPayload | undefined;
16
+ /**
17
+ * The wire code for an `RPCError` that was not built to be exposed.
18
+ *
19
+ * A standard wire code (a key of `RPC_HTTP_STATUS`, such as
20
+ * `RPC_UNAVAILABLE`) is public vocabulary and callers act on it, so it
21
+ * travels. Any other code — `new RPCError("…", { code: "TASK_SECRET" })` —
22
+ * is server detail just as the message is, and goes out as
23
+ * `RPC_INTERNAL_ERROR`.
24
+ */
25
+ export declare function withheldRPCErrorCode(code: string): string;
16
26
  //# sourceMappingURL=rpcBaseErrorMapping.helper.d.ts.map
@@ -4,6 +4,7 @@
4
4
  * procedure calls.
5
5
  */
6
6
  import { isBaseError, RateLimitError, ValidationError } from "@zudojs/errors";
7
+ import { RPC_HTTP_STATUS } from "../transport/http/rpcHttpStatus.helper.js";
7
8
  /** Wire code for each HTTP-style status an exposable error can carry. */
8
9
  const STATUS_WIRE_CODES = new Map([
9
10
  [400, "RPC_VALIDATION_ERROR"],
@@ -54,4 +55,16 @@ export function mapExposedBaseError(error) {
54
55
  ...(details !== undefined ? { details } : {}),
55
56
  };
56
57
  }
58
+ /**
59
+ * The wire code for an `RPCError` that was not built to be exposed.
60
+ *
61
+ * A standard wire code (a key of `RPC_HTTP_STATUS`, such as
62
+ * `RPC_UNAVAILABLE`) is public vocabulary and callers act on it, so it
63
+ * travels. Any other code — `new RPCError("…", { code: "TASK_SECRET" })` —
64
+ * is server detail just as the message is, and goes out as
65
+ * `RPC_INTERNAL_ERROR`.
66
+ */
67
+ export function withheldRPCErrorCode(code) {
68
+ return Object.hasOwn(RPC_HTTP_STATUS, code) ? code : "RPC_INTERNAL_ERROR";
69
+ }
57
70
  //# sourceMappingURL=rpcBaseErrorMapping.helper.js.map
@@ -21,7 +21,8 @@ export interface RPCMappedError {
21
21
  * exposed — an `RPCInternalError`, a non-exposed custom `RPCError` or
22
22
  * `BaseError`, or any other error — is answered with
23
23
  * {@link INTERNAL_ERROR_MESSAGE}, so exception text, stack traces and
24
- * causes never reach the remote side.
24
+ * causes never reach the remote side — nor does a non-exposed custom
25
+ * code ({@link withheldRPCErrorCode}).
25
26
  */
26
27
  export declare function mapRPCError(error: unknown): RPCMappedError;
27
28
  /**
@@ -1,6 +1,6 @@
1
1
  import { isRPCError, RPCAuthenticationError, RPCCancelledError, RPCDeadlineExceededError, RPCDeserializationError, RPCForbiddenError, RPCInternalError, RPCInvalidRequestError, RPCProcedureNotFoundError, RPCRateLimitedError, RPCSerializationError, RPCTimeoutError, RPCTransportError, RPCUnavailableError, RPCValidationError, } from "../errors/rpc.errors.js";
2
2
  import { INTERNAL_ERROR_MESSAGE } from "../constants/rpcConstants.core.js";
3
- import { mapExposedBaseError } from "./rpcBaseErrorMapping.helper.js";
3
+ import { mapExposedBaseError, withheldRPCErrorCode } from "./rpcBaseErrorMapping.helper.js";
4
4
  /**
5
5
  * Wire codes for the error types the server maps.
6
6
  *
@@ -34,7 +34,8 @@ const ERROR_CODES = [
34
34
  * exposed — an `RPCInternalError`, a non-exposed custom `RPCError` or
35
35
  * `BaseError`, or any other error — is answered with
36
36
  * {@link INTERNAL_ERROR_MESSAGE}, so exception text, stack traces and
37
- * causes never reach the remote side.
37
+ * causes never reach the remote side — nor does a non-exposed custom
38
+ * code ({@link withheldRPCErrorCode}).
38
39
  */
39
40
  export function mapRPCError(error) {
40
41
  if (error instanceof RPCInternalError || !(error instanceof Error)) {
@@ -48,9 +49,9 @@ export function mapRPCError(error) {
48
49
  }
49
50
  }
50
51
  if (isRPCError(error)) {
51
- return error.expose === false
52
- ? internalFailure(error.code)
53
- : exposed(error.code, error.message, undefined);
52
+ return error.expose === true
53
+ ? exposed(error.code, error.message, undefined)
54
+ : internalFailure(withheldRPCErrorCode(error.code));
54
55
  }
55
56
  const base = mapExposedBaseError(error);
56
57
  if (base !== undefined) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zudojs/rpc",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "description": "Remote procedure call infrastructure for Zudojs applications.",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -27,11 +27,11 @@
27
27
  "node": ">=24.0.0"
28
28
  },
29
29
  "dependencies": {
30
- "@zudojs/constants": "1.1.2",
31
- "@zudojs/errors": "1.3.0",
32
- "@zudojs/schema": "1.2.0",
33
- "@zudojs/security": "1.3.0",
34
- "@zudojs/serialization": "1.2.0",
30
+ "@zudojs/constants": "1.1.3",
31
+ "@zudojs/errors": "1.3.1",
32
+ "@zudojs/schema": "1.2.2",
33
+ "@zudojs/security": "1.3.2",
34
+ "@zudojs/serialization": "1.2.2",
35
35
  "@zudojs/types": "1.2.0"
36
36
  },
37
37
  "devDependencies": {