Skip to content

Commit

Permalink
Properly handle RpcMessage parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
syyyr committed Jul 31, 2024
1 parent 022a50b commit b516ada
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/rpcmessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class RpcError extends Error {
}
}

export class ProtocolError extends Error {}

export class InvalidRequest extends RpcError {}
export class MethodNotFound extends RpcError {}
export class InvalidParams extends RpcError {}
Expand Down Expand Up @@ -135,6 +137,15 @@ class RpcMessage {

resultOrError() {
if (this.value.value[KeyError] !== undefined) {
if (!(this.value.value[KeyError] instanceof IMap)) {
return new ProtocolError("Response had an error, but this error was not a map");
}

const error_map = this.value.value[KeyError];
if (error_map.value[ERROR_CODE] === undefined) {
return new ProtocolError("Response had an error, but this error did not contain at least an error code");
}

const ErrorType = (() => {
switch ((this.value.value[KeyError] as ErrorMap).value[ERROR_CODE].value) {
case ErrorCode.InvalidRequest: return InvalidRequest;
Expand All @@ -155,7 +166,11 @@ class RpcMessage {
return new ErrorType(this.value.value[KeyError] as ErrorMap);
}

return this.value.value[KeyResult];
if (this.value.value[KeyResult] !== undefined) {
return this.value.value[KeyResult];
}

return new ProtocolError("Response included neither result nor error");
}

setResult(result: RpcValue) {
Expand Down

0 comments on commit b516ada

Please sign in to comment.