github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/rpc/lib/types/error_codes.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  )
     7  
     8  // From JSONRPC 2.0 spec
     9  type RPCErrorCode int
    10  
    11  const (
    12  	RPCErrorCodeParseError     RPCErrorCode = -32700
    13  	RPCErrorCodeInvalidRequest RPCErrorCode = -32600
    14  	RPCErrorCodeMethodNotFound RPCErrorCode = -32601
    15  	RPCErrorCodeInvalidParams  RPCErrorCode = -32602
    16  	RPCErrorCodeInternalError  RPCErrorCode = -32603
    17  	// Available for custom server-defined errors
    18  	RPCErrorCodeServerErrorStart RPCErrorCode = -32000
    19  	RPCErrorCodeServerErrorEnd   RPCErrorCode = -32099
    20  )
    21  
    22  func (code RPCErrorCode) String() string {
    23  	switch code {
    24  	case RPCErrorCodeParseError:
    25  		return "Parse Error"
    26  	case RPCErrorCodeInvalidRequest:
    27  		return "Parse Error"
    28  	case RPCErrorCodeMethodNotFound:
    29  		return "Method Not Found"
    30  	case RPCErrorCodeInvalidParams:
    31  		return "Invalid Params"
    32  	case RPCErrorCodeInternalError:
    33  		return "Internal Error"
    34  	default:
    35  		if code.IsServerError() {
    36  			return fmt.Sprintf("Server Error %d", code)
    37  		}
    38  		return fmt.Sprintf("Unknown Error %d", code)
    39  	}
    40  }
    41  
    42  func (code RPCErrorCode) IsServerError() bool {
    43  	return code >= RPCErrorCodeServerErrorStart && code <= RPCErrorCodeServerErrorEnd
    44  }
    45  
    46  func (code RPCErrorCode) HTTPStatusCode() int {
    47  	switch code {
    48  	case RPCErrorCodeInvalidRequest:
    49  		return http.StatusBadRequest
    50  	case RPCErrorCodeMethodNotFound:
    51  		return http.StatusMethodNotAllowed
    52  	default:
    53  		return http.StatusInternalServerError
    54  	}
    55  }
    56  
    57  func (code RPCErrorCode) Error() string {
    58  	return code.String()
    59  }