github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/services/rpcsrv/error.go (about)

     1  package rpcsrv
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/nspcc-dev/neo-go/pkg/neorpc"
     7  )
     8  
     9  // abstractResult is an interface which represents either single JSON-RPC 2.0 response
    10  // or batch JSON-RPC 2.0 response.
    11  type abstractResult interface {
    12  	RunForErrors(f func(jsonErr *neorpc.Error))
    13  }
    14  
    15  // abstract represents abstract JSON-RPC 2.0 response. It is used as a server-side response
    16  // representation.
    17  type abstract struct {
    18  	neorpc.Header
    19  	Error  *neorpc.Error `json:"error,omitempty"`
    20  	Result any           `json:"result,omitempty"`
    21  }
    22  
    23  // RunForErrors implements abstractResult interface.
    24  func (a abstract) RunForErrors(f func(jsonErr *neorpc.Error)) {
    25  	if a.Error != nil {
    26  		f(a.Error)
    27  	}
    28  }
    29  
    30  // abstractBatch represents abstract JSON-RPC 2.0 batch-response.
    31  type abstractBatch []abstract
    32  
    33  // RunForErrors implements abstractResult interface.
    34  func (ab abstractBatch) RunForErrors(f func(jsonErr *neorpc.Error)) {
    35  	for _, a := range ab {
    36  		if a.Error != nil {
    37  			f(a.Error)
    38  		}
    39  	}
    40  }
    41  
    42  func getHTTPCodeForError(respErr *neorpc.Error) int {
    43  	var httpCode int
    44  	switch respErr.Code {
    45  	case neorpc.BadRequestCode:
    46  		httpCode = http.StatusBadRequest
    47  	case neorpc.MethodNotFoundCode:
    48  		httpCode = http.StatusMethodNotAllowed
    49  	case neorpc.InternalServerErrorCode:
    50  		httpCode = http.StatusInternalServerError
    51  	default:
    52  		httpCode = http.StatusUnprocessableEntity
    53  	}
    54  	return httpCode
    55  }