-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(MethodCallValidator): add method not found error
fixes #156
- Loading branch information
Showing
6 changed files
with
114 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import MethodCallMethodNotFoundError from "./method-not-found-error"; | ||
import { OpenRPC } from "@open-rpc/meta-schema"; | ||
|
||
const exampleDoc = { | ||
info: { | ||
title: "testerino", | ||
version: "123", | ||
}, | ||
methods: [], | ||
openrpc: "1.1.9", | ||
} as OpenRPC; | ||
|
||
describe("MethodCallMethodNotFoundError", () => { | ||
it("can be instantiated", () => { | ||
const error = new MethodCallMethodNotFoundError("floobar", exampleDoc, ["abc", 123]); | ||
expect(error).toBeInstanceOf(MethodCallMethodNotFoundError); | ||
}); | ||
|
||
it("works when no params passed", () => { | ||
const error = new MethodCallMethodNotFoundError("floobar", exampleDoc); | ||
expect(error).toBeInstanceOf(MethodCallMethodNotFoundError); | ||
}); | ||
|
||
it("properly parses params in to a string", () => { | ||
const error = new MethodCallMethodNotFoundError("floobar", exampleDoc, ["abc", { abc: 123 }, 123]); | ||
expect(error).toBeInstanceOf(MethodCallMethodNotFoundError); | ||
expect(error.message).toBe(`Method Not Found Error for OpenRPC API named "testerino" | ||
The requested method: "floobar" not a valid method. | ||
debug info: | ||
params: "abc", {"abc":123}, 123`); | ||
}); | ||
|
||
it("it handles openrpc docs with a method", () => { | ||
exampleDoc.methods = [ | ||
{ | ||
name: "dooptiedoo", | ||
params: [], | ||
result: { | ||
name: "dooptie", | ||
schema: {}, | ||
}, | ||
}, | ||
]; | ||
const error = new MethodCallMethodNotFoundError("floobar", exampleDoc, ["abc", { abc: 123 }, 123]); | ||
expect(error).toBeInstanceOf(MethodCallMethodNotFoundError); | ||
expect(error.message).toContain("Valid method names are as follows: dooptiedoo"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { OpenRPC } from "@open-rpc/meta-schema"; | ||
|
||
/** | ||
* Provides an error interface for handling when a method is trying to be called but does not exist. | ||
*/ | ||
export default class MethodCallMethodNotFoundError implements Error { | ||
public name = "MethodCallMethodNotFoundError"; | ||
public message: string; | ||
|
||
/** | ||
* @param methodName The method name that was used. | ||
* @param openrpcDocument The OpenRPC document that the method was used against. | ||
* @param receievedParams The params, if any, that were used. | ||
*/ | ||
constructor( | ||
public methodName: string, | ||
public openrpcDocument: OpenRPC, | ||
public receievedParams: any[] = [], | ||
) { | ||
const msg = [ | ||
`Method Not Found Error for OpenRPC API named "${openrpcDocument.info.title}"`, | ||
`The requested method: "${methodName}" not a valid method.`, | ||
|
||
]; | ||
|
||
if (openrpcDocument.methods.length > 0) { | ||
msg.push( | ||
`Valid method names are as follows: ${openrpcDocument.methods.map(({ name }) => name).join(", ")}`, | ||
); | ||
} | ||
|
||
if (receievedParams.length > 0) { | ||
const stringedParams = receievedParams | ||
.map((p) => { try { return JSON.stringify(p); } catch (e) { return p; } }) | ||
.join(", "); | ||
|
||
msg.push(""); | ||
msg.push("debug info:"); | ||
msg.push(` params: ${stringedParams}`); | ||
} | ||
|
||
this.message = msg.join("\n"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters