Skip to content

Commit 6cb39dd

Browse files
authored
Merge pull request #3529 from fjl/console-error-fix
console: fix error message in faux JSON-RPC responses
2 parents 88cc1ca + 9bab0b8 commit 6cb39dd

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

console/bridge.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -270,18 +270,15 @@ func (b *bridge) Send(call otto.FunctionCall) (response otto.Value) {
270270
} else {
271271
resultVal, err := JSON.Call("parse", string(result))
272272
if err != nil {
273-
resp = newErrorResponse(call, -32603, err.Error(), &req.Id).Object()
273+
setError(resp, -32603, err.Error())
274274
} else {
275275
resp.Set("result", resultVal)
276276
}
277277
}
278278
case rpc.Error:
279-
resp.Set("error", map[string]interface{}{
280-
"code": err.ErrorCode(),
281-
"message": err.Error(),
282-
})
279+
setError(resp, err.ErrorCode(), err.Error())
283280
default:
284-
resp = newErrorResponse(call, -32603, err.Error(), &req.Id).Object()
281+
setError(resp, -32603, err.Error())
285282
}
286283
resps.Call("push", resp)
287284
}
@@ -300,12 +297,8 @@ func (b *bridge) Send(call otto.FunctionCall) (response otto.Value) {
300297
return response
301298
}
302299

303-
func newErrorResponse(call otto.FunctionCall, code int, msg string, id interface{}) otto.Value {
304-
// Bundle the error into a JSON RPC call response
305-
m := map[string]interface{}{"version": "2.0", "id": id, "error": map[string]interface{}{"code": code, msg: msg}}
306-
res, _ := json.Marshal(m)
307-
val, _ := call.Otto.Run("(" + string(res) + ")")
308-
return val
300+
func setError(resp *otto.Object, code int, msg string) {
301+
resp.Set("error", map[string]interface{}{"code": code, "message": msg})
309302
}
310303

311304
// throwJSException panics on an otto.Value. The Otto VM will recover from the

0 commit comments

Comments
 (0)