-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It's internal server thing that is not for the outside users.
- Loading branch information
1 parent
7481b59
commit 5d8a5f5
Showing
4 changed files
with
181 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package server | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/nspcc-dev/neo-go/pkg/rpc/response" | ||
) | ||
|
||
// serverError represents RPC error response on the server side. | ||
type serverError struct { | ||
*response.Error | ||
HTTPCode int // HTTPCode won't be marshalled because Error's marshaller is used. | ||
} | ||
|
||
// AbstractResult is an interface which represents either single JSON-RPC 2.0 response | ||
// or batch JSON-RPC 2.0 response. | ||
type AbstractResult interface { | ||
RunForErrors(f func(jsonErr *response.Error)) | ||
} | ||
|
||
// Abstract represents abstract JSON-RPC 2.0 response, it differs from Raw in | ||
// that Result field is an interface here. It is used as a server-side response | ||
// representation. | ||
type Abstract struct { | ||
response.Header | ||
Error *serverError `json:"error,omitempty"` | ||
Result interface{} `json:"result,omitempty"` | ||
} | ||
|
||
// RunForErrors implements AbstractResult interface. | ||
func (a Abstract) RunForErrors(f func(jsonErr *response.Error)) { | ||
if a.Error != nil { | ||
f(a.Error.Error) | ||
} | ||
} | ||
|
||
// AbstractBatch represents abstract JSON-RPC 2.0 batch-response. | ||
type AbstractBatch []Abstract | ||
|
||
// RunForErrors implements AbstractResult interface. | ||
func (ab AbstractBatch) RunForErrors(f func(jsonErr *response.Error)) { | ||
for _, a := range ab { | ||
if a.Error != nil { | ||
f(a.Error.Error) | ||
} | ||
} | ||
} | ||
|
||
func packClientError(respErr *response.Error) *serverError { | ||
var httpCode int | ||
switch respErr.Code { | ||
case response.BadRequestCode: | ||
httpCode = http.StatusBadRequest | ||
case response.InvalidRequestCode, response.RPCErrorCode, response.InvalidParamsCode: | ||
httpCode = http.StatusUnprocessableEntity | ||
case response.MethodNotFoundCode: | ||
httpCode = http.StatusMethodNotAllowed | ||
case response.InternalServerErrorCode: | ||
httpCode = http.StatusInternalServerError | ||
default: | ||
httpCode = http.StatusUnprocessableEntity | ||
} | ||
return &serverError{ | ||
Error: respErr, | ||
HTTPCode: httpCode, | ||
} | ||
} |
Oops, something went wrong.