From 5dca765bd169376fe06ed7730b4f84b3372dd6f8 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Sat, 15 Feb 2020 19:06:34 +0300 Subject: [PATCH] rpc: add custom error with code -100 Fixed getassetstate method: it should return error with code -100 when asset with specified id is not found (as in c# node). --- pkg/rpc/errors.go | 6 ++++++ pkg/rpc/server.go | 2 +- pkg/rpc/server_test.go | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pkg/rpc/errors.go b/pkg/rpc/errors.go index f7c3441d1..966f5f678 100644 --- a/pkg/rpc/errors.go +++ b/pkg/rpc/errors.go @@ -62,6 +62,12 @@ func NewInternalServerError(data string, cause error) *Error { return newError(-32603, http.StatusInternalServerError, "Internal error", data, cause) } +// NewRPCError creates a new error with +// code -100 +func NewRPCError(message string, data string, cause error) *Error { + return newError(-100, http.StatusUnprocessableEntity, message, data, cause) +} + // Error implements the error interface. func (e Error) Error() string { return fmt.Sprintf("%s (%d) - %s - %s", e.Message, e.Code, e.Data, e.Cause) diff --git a/pkg/rpc/server.go b/pkg/rpc/server.go index 3805da144..cec563a4a 100644 --- a/pkg/rpc/server.go +++ b/pkg/rpc/server.go @@ -234,7 +234,7 @@ Methods: if as != nil { results = wrappers.NewAssetState(as) } else { - results = "Invalid assetid" + resultsErr = NewRPCError("Unknown asset", "", nil) } case "getaccountstate": diff --git a/pkg/rpc/server_test.go b/pkg/rpc/server_test.go index 0c03ffb4d..dac20d31e 100644 --- a/pkg/rpc/server_test.go +++ b/pkg/rpc/server_test.go @@ -87,7 +87,7 @@ var rpcTestCases = map[string][]rpcTestCase{ { name: "negative", params: `["602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de2"]`, - result: func(e *executor) interface{} { return "Invalid assetid" }, + fail: true, }, { name: "no params",