Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21x 21x 21x 21x 21x 1x 1x 21x 2x 2x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 1x 1x 1x 15x 15x 1x 1x 1x 2x 2x 1x 1x 1x 11x 11x 1x 1x 21x 21x 21x 21x 21x 20x 20x 21x 21x 1x 1x 1x 1x 1x 1x 1x 26x 26x 22x 26x 1x 1x 21x 21x 21x 26x 26x 20x 20x 20x 20x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 2x | /**
* Structured ActrError bridge.
*
* The native napi-rs binding signals every protocol-level failure by
* throwing a plain `Error` whose `.message` is a JSON payload:
*
* { "kind": "Client", "code": "DependencyNotFound",
* "message": "…", "service_name": "echo" }
*
* Consumers typically just want to branch on fault domain (retry vs. DLQ
* vs. fail fast). Parsing JSON off `.message` at every call site is a
* paper cut, so we wrap each native call in `mapNativeError` and surface a
* proper `ActrError` subclass of `Error` that carries strongly-typed
* classification fields.
*/
export type ActrErrorKind = 'Transient' | 'Client' | 'Internal' | 'Corrupt';
export type ActrErrorCode =
| 'Unavailable'
| 'ConnectionNotReady'
| 'TimedOut'
| 'NotFound'
| 'PermissionDenied'
| 'InvalidArgument'
| 'UnknownRoute'
| 'DependencyNotFound'
| 'DecodeFailure'
| 'NotImplemented'
| 'Internal'
| 'Config'
| 'HyperBootstrap';
interface StructuredPayload {
kind: ActrErrorKind;
code: ActrErrorCode;
message: string;
service_name?: string;
retry_after_ms?: number | null;
}
/**
* Typed error thrown from every ACTR native call.
*
* `kind` is the fault-domain bucket (drive retry / DLQ policy off this),
* `code` is the exact protocol variant, and `service_name` is populated
* only when `code === 'DependencyNotFound'`.
*
* When `code === 'ConnectionNotReady'`, `retry_after_ms` is an optional hint
* for backing off before the next send attempt. The readiness hook is still
* the authoritative signal.
*/
export class ActrError extends Error {
readonly kind: ActrErrorKind;
readonly code: ActrErrorCode;
readonly service_name?: string;
readonly retry_after_ms?: number | null;
constructor(payload: StructuredPayload) {
super(payload.message);
this.name = 'ActrError';
this.kind = payload.kind;
this.code = payload.code;
if (payload.service_name !== undefined) {
this.service_name = payload.service_name;
}
if (payload.retry_after_ms !== undefined) {
this.retry_after_ms = payload.retry_after_ms;
}
// Preserve V8 stack-trace ergonomics in Node.
if (
typeof (Error as { captureStackTrace?: unknown }).captureStackTrace ===
'function'
) {
(
Error as unknown as {
captureStackTrace: (t: unknown, c: unknown) => void;
}
).captureStackTrace(this, ActrError);
}
}
/** `true` iff the error is in the Transient fault domain. */
isRetryable(): boolean {
return this.kind === 'Transient';
}
/** `true` iff this send was stopped before entering transport. */
isConnectionNotReady(): boolean {
return this.code === 'ConnectionNotReady';
}
/** `true` iff the error should be routed to a Dead Letter Queue. */
requiresDlq(): boolean {
return this.kind === 'Corrupt';
}
}
function isStructuredPayload(value: unknown): value is StructuredPayload {
if (typeof value !== 'object' || value === null) return false;
const p = value as Record<string, unknown>;
return (
typeof p.kind === 'string' &&
typeof p.code === 'string' &&
typeof p.message === 'string'
);
}
/**
* If `err` carries a JSON payload produced by the Rust binding, re-wrap
* it as an `ActrError`; otherwise return it unchanged so non-ACTR errors
* keep their original identity.
*/
export function mapNativeError(err: unknown): unknown {
if (err instanceof ActrError) return err;
if (!(err instanceof Error)) return err;
const raw = err.message;
if (typeof raw !== 'string' || raw.length === 0 || raw[0] !== '{') {
return err;
}
let parsed: unknown;
try {
parsed = JSON.parse(raw);
} catch {
// Not a structured payload — leave the error alone so consumers can
// still see the original message.
return err;
}
if (!isStructuredPayload(parsed)) return err;
const wrapped = new ActrError(parsed);
if (err.stack) wrapped.stack = err.stack;
return wrapped;
}
/**
* Invoke an async native call and re-throw ACTR failures as `ActrError`.
*
* Used by the thin TS wrappers around the napi-rs-generated classes.
*/
export async function callNative<T>(fn: () => Promise<T>): Promise<T> {
try {
return await fn();
} catch (err) {
throw mapNativeError(err);
}
}
|