Skip to content

Commit

Permalink
Merge pull request #140 from zcstarr/master
Browse files Browse the repository at this point in the history
feat: add support for custom errors
  • Loading branch information
zcstarr authored Sep 23, 2019
2 parents dc00508 + c50e42c commit 1098034
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class JSONRPCError extends Error {
public code: number;
public message: string;
public data?: any;
constructor(message: string, code: number, data?: any) {
super();
this.code = code;
this.message = message;
this.data = data;
}
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Server, { IServerOptions } from "./server";
import { Router } from "./router";
import { JSONRPCError } from "./error";

export {
Server,
IServerOptions,
Router,
JSONRPCError,
};
25 changes: 24 additions & 1 deletion src/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import {
ExamplePairingObject,
MethodObject,
} from "@open-rpc/meta-schema";
import { JSONRPCError } from "./error";
const jsf = require("json-schema-faker"); // tslint:disable-line

const makeMethodMapping = (methods: MethodObject[]): IMethodMapping => {
return _.chain(methods)
const methodMapping = _.chain(methods)
.keyBy("name")
.mapValues((methodObject: MethodObject) => async (...args: any): Promise<any> => {
const foundExample = _.find(
Expand All @@ -28,6 +29,9 @@ const makeMethodMapping = (methods: MethodObject[]): IMethodMapping => {
}
})
.value();
methodMapping["test-error"] = async () => { throw new JSONRPCError("test error", 9998, { meta: "data" }); };
methodMapping["unknown-error"] = async () => { throw new Error("unanticpated crash"); };
return methodMapping;
};

describe("router", () => {
Expand All @@ -37,6 +41,11 @@ describe("router", () => {
let parsedExample: OpenRPC;
beforeAll(async () => {
parsedExample = await parseOpenRPCDocument(JSON.stringify(example));
// Mock error methods used to test routing calls
const testErrorMethod = { name: "test-error", params: [], result: { name: "test-error-result", schema: {} } };
const unknownErrorMethod = Object.assign({}, testErrorMethod, { name: "unknown-error" });
parsedExample.methods.push(testErrorMethod);
parsedExample.methods.push(unknownErrorMethod);
});

it("is constructed with an OpenRPC document and a method mapping", () => {
Expand Down Expand Up @@ -68,6 +77,20 @@ describe("router", () => {
expect(result.error.code).toBe(-32602);
});

it("returns JSONRPCError data when thrown", async () => {
const router = new Router(parsedExample, makeMethodMapping(parsedExample.methods));
const result = await router.call("test-error", []);
expect(result.error.code).toBe(9998);
expect(result.error.message).toBe("test error");
});

it("returns Unknown Error data when thrown", async () => {
const router = new Router(parsedExample, makeMethodMapping(parsedExample.methods));
const result = await router.call("unknown-error", []);
expect(result.error.code).toBe(6969);
expect(result.error.message).toBe("unknown error");
});

it("implements service discovery", async () => {
const router = new Router(parsedExample, makeMethodMapping(parsedExample.methods));
const result = await router.call("rpc.discover", []);
Expand Down
7 changes: 6 additions & 1 deletion src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
OpenRPC,
} from "@open-rpc/meta-schema";
import { MethodCallValidator, MethodNotFoundError, ParameterValidationError } from "@open-rpc/schema-utils-js";
import { JSONRPCError } from "./error";

const jsf = require("json-schema-faker"); // tslint:disable-line

export interface IMethodMapping {
Expand Down Expand Up @@ -61,7 +63,10 @@ export class Router {
try {
return await this.methods[methodName](...params);
} catch (e) {
return { code: 6969, message: "unknown error" };
if (e instanceof JSONRPCError) {
return {error: { code: e.code, message: e.message, data: e.data }};
}
return { error: { code: 6969, message: "unknown error" } };
}
}

Expand Down

0 comments on commit 1098034

Please sign in to comment.