Skip to content

Don't use status code to detect errors #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 50 additions & 5 deletions lib/browser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
import { JsonRpcRequest } from './jsonRpc';
import { JsonRpcRequest, JsonRpcResponse } from './jsonRpc';
import { isPlainObject, hasOwnProperty } from './validation';

function isJsonRpc(result: unknown): result is JsonRpcResponse {
return (
isPlainObject(result) &&
hasOwnProperty(result, 'jsonrpc') &&
result.jsonrpc === '2.0' &&
hasOwnProperty(result, 'id') &&
(result.id === null ||
typeof result.id === 'string' ||
typeof result.id === 'number') &&
(!hasOwnProperty(result, 'error') ||
(hasOwnProperty(result, 'error') &&
isPlainObject(result.error) &&
hasOwnProperty(result.error, 'code') &&
typeof result.error.code === 'number' &&
hasOwnProperty(result.error, 'message') &&
typeof result.error.message === 'string'))
);
}

function escapeRegExp(string: string): string {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
Expand All @@ -18,6 +38,23 @@ function rewriteStacktrace(error: Error): Error {
return error;
}

function getJsonRpcErrorMessage(code: number): string {
switch (code) {
case -32700:
return 'Parse error';
case -32600:
return 'Invalid Request';
case -32601:
return 'Method not found';
case -32602:
return 'Invalid params';
case -32603:
return 'Internal error';
default:
return 'Server error';
}
}

type NextRpcCall = (...params: any[]) => any;

let nextId = 1;
Expand All @@ -37,22 +74,30 @@ function createRpcFetcher(url: string, method: string): NextRpcCall {
},
})
.then(function (res) {
if (!res.ok) {
throw new Error('Unexpected HTTP status ' + res.status);
}
return res.json();
})
.then(function (json) {
if (!isJsonRpc(json)) {
throw new Error('Invalid Response');
}

if (json.error) {
let err = Object.assign(
if (json.error.code >= -32768 && json.error.code <= -32000) {
throw new Error(getJsonRpcErrorMessage(json.error.code));
}

let err: Error = Object.assign(
new Error(json.error.message),
json.error.data
);

if (process.env.NODE_ENV !== 'production') {
err = rewriteStacktrace(err);
}

throw err;
}

return json.result;
});
};
Expand Down
18 changes: 18 additions & 0 deletions lib/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
type Require<T, K extends keyof T> = T & {
[P in K]-?: T[P];
};

type Ensure<U, K extends PropertyKey> = K extends keyof U
? Require<U, K>
: U & Record<K, unknown>;

export function hasOwnProperty<X extends {}, Y extends PropertyKey>(
obj: X,
prop: Y
): obj is Ensure<X, Y> {
return obj.hasOwnProperty(prop);
}

export function isPlainObject(value: unknown): value is {} {
return !!value && typeof value === 'object' && !Array.isArray(value);
}