Skip to content

Commit

Permalink
Merge pull request #27 from LOKE/feature/fix-error-tostring
Browse files Browse the repository at this point in the history
Fix: error.toString shows [object Object]
  • Loading branch information
denwilliams authored Jan 17, 2024
2 parents d5f4d35 + ac32bf8 commit ebb5334
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
33 changes: 31 additions & 2 deletions lib/client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { format } from "util";
import fetch from "node-fetch";
import * as context from "@loke/context";

Expand Down Expand Up @@ -110,7 +111,26 @@ function mapError(serviceName: string, methodName: string, errResult: any) {
throw new RpcResponseError(source, errResult);
}

class RpcResponseError {
const EXCLUDED_META_KEYS = [
"type",
"code",
"expose",
"message",
"namespace",
"instance",
"source",
];

function metaToString(meta: Record<string, any>) {
if (!meta) return "";

return Object.keys(meta)
.filter((k) => !EXCLUDED_META_KEYS.includes(k))
.map((k) => format("%s=%j", k, meta[k]))
.join(" ");
}

export class RpcResponseError {
source?: string[];

constructor(source: string, responseBody: any) {
Expand All @@ -121,8 +141,9 @@ class RpcResponseError {
writable: true,
});

// .message .code .type .expose .instance .type are applied here
Object.assign(this, responseBody);
if (!this.source) this.source = [];

this.source = [source, ...(this.source || [])];

Object.defineProperty(this, "stack", {
Expand All @@ -138,6 +159,14 @@ class RpcResponseError {
writable: true,
});
}

toString() {
// defineProperty not recognized by typescript, nor is the result of assign recognizable
const { name, message, instance } = this as any;
const meta = metaToString(this);

return `${name}: ${message} [${instance}]${meta ? " " + meta : ""}`;
}
}

export class RPCClient extends BaseClient {
Expand Down
36 changes: 36 additions & 0 deletions test/rpc-response-error.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import test from "ava";

import { RpcResponseError } from "../lib/client";

test("RpcResponseError", (t) => {
const err: any = new RpcResponseError("my_source", {
type: "FooError",
code: 123,
expose: true,
message: "foo",
namespace: "foo",
instance: "bar",
source: ["foo", "bar"],
upstreamCode: 456,
upstreamMessage: "a bad thing happened",
});

t.is(
err.toString(),
'RpcResponseError: foo [bar] upstreamCode=456 upstreamMessage="a bad thing happened"'
);
t.is(err.name, "RpcResponseError");
t.is(err.type, "FooError");
t.is(err.code, 123);
t.is(err.expose, true);
t.is(err.message, "foo");
t.is(err.namespace, "foo");
t.is(err.instance, "bar");
t.deepEqual(err.source, ["my_source", "foo", "bar"]);
t.is(
err.stack,
'RpcResponseError: foo [bar] upstreamCode=456 upstreamMessage="a bad thing happened"\n via bar\n via foo\n via my_source'
);
t.is(err.upstreamCode, 456);
t.is(err.upstreamMessage, "a bad thing happened");
});

0 comments on commit ebb5334

Please sign in to comment.