Skip to content

Commit

Permalink
feat(methods): Print stack trace when recovering from panic
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamSLevy committed Jan 2, 2019
1 parent dda8de8 commit ff12d51
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
10 changes: 7 additions & 3 deletions methods.go
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"log"
"os"
"runtime"
)

// DebugMethodFunc controls whether additional debug information will be
Expand Down Expand Up @@ -57,9 +58,12 @@ func (method MethodFunc) call(params json.RawMessage) (res Response) {
res.Error = &InternalError
if DebugMethodFunc {
//res.Data = err
logger.Println(err)
logger.Printf("Params: %v", string(params))
logger.Printf("Return: %#v", result)
const size = 64 << 10
buf := make([]byte, size)
buf = buf[:runtime.Stack(buf, false)]
logger.Printf("jsonrpc2: panic running method %#v: %v\n%s", method, err, buf)
logger.Printf("jsonrpc2: Params: %v", string(params))
logger.Printf("jsonrpc2: Return: %#v", result)
}
}
}()
Expand Down
4 changes: 2 additions & 2 deletions methods_test.go
Expand Up @@ -55,8 +55,8 @@ func TestMethodFuncCall(t *testing.T) {
}
assert.Nil(res.Result, test.Name)
}
assert.Equal("MethodFunc error: Error.Code is reserved\nParams: \nReturn: jsonrpc2.Error{Code:-32601, Message:\"Method not found\", Data:interface {}(nil)}\nMethodFunc error: method returned nil\nParams: \nReturn: <nil>\nMethodFunc error: Error.Data: json: unsupported type: map[bool]bool\nParams: \nReturn: jsonrpc2.Error{Code:0, Message:\"e\", Data:map[bool]bool{true:true}}\nMethodFunc error: json: unsupported type: map[bool]bool\nParams: \nReturn: map[bool]bool{true:true}\n",
string(buf.Bytes()))
assert.Contains(string(buf.Bytes()),
"jsonrpc2: panic running method (jsonrpc2.MethodFunc)")

var f MethodFunc = func(_ json.RawMessage) interface{} {
return NewError(100, "custom", "data")
Expand Down

0 comments on commit ff12d51

Please sign in to comment.