...

Source file src/github.com/go-kit/kit/transport/http/jsonrpc/error.go

Documentation: github.com/go-kit/kit/transport/http/jsonrpc

     1  package jsonrpc
     2  
     3  // Error defines a JSON RPC error that can be returned
     4  // in a Response from the spec
     5  // http://www.jsonrpc.org/specification#error_object
     6  type Error struct {
     7  	Code    int         `json:"code"`
     8  	Message string      `json:"message"`
     9  	Data    interface{} `json:"data,omitempty"`
    10  }
    11  
    12  // Error implements error.
    13  func (e Error) Error() string {
    14  	if e.Message != "" {
    15  		return e.Message
    16  	}
    17  	return errorMessage[e.Code]
    18  }
    19  
    20  // ErrorCode returns the JSON RPC error code associated with the error.
    21  func (e Error) ErrorCode() int {
    22  	return e.Code
    23  }
    24  
    25  const (
    26  	// ParseError defines invalid JSON was received by the server.
    27  	// An error occurred on the server while parsing the JSON text.
    28  	ParseError int = -32700
    29  
    30  	// InvalidRequestError defines the JSON sent is not a valid Request object.
    31  	InvalidRequestError int = -32600
    32  
    33  	// MethodNotFoundError defines the method does not exist / is not available.
    34  	MethodNotFoundError int = -32601
    35  
    36  	// InvalidParamsError defines invalid method parameter(s).
    37  	InvalidParamsError int = -32602
    38  
    39  	// InternalError defines a server error
    40  	InternalError int = -32603
    41  )
    42  
    43  var errorMessage = map[int]string{
    44  	ParseError:          "An error occurred on the server while parsing the JSON text.",
    45  	InvalidRequestError: "The JSON sent is not a valid Request object.",
    46  	MethodNotFoundError: "The method does not exist / is not available.",
    47  	InvalidParamsError:  "Invalid method parameter(s).",
    48  	InternalError:       "Internal JSON-RPC error.",
    49  }
    50  
    51  // ErrorMessage returns a message for the JSON RPC error code. It returns the empty
    52  // string if the code is unknown.
    53  func ErrorMessage(code int) string {
    54  	return errorMessage[code]
    55  }
    56  
    57  type parseError string
    58  
    59  func (e parseError) Error() string {
    60  	return string(e)
    61  }
    62  func (e parseError) ErrorCode() int {
    63  	return ParseError
    64  }
    65  
    66  type invalidRequestError string
    67  
    68  func (e invalidRequestError) Error() string {
    69  	return string(e)
    70  }
    71  func (e invalidRequestError) ErrorCode() int {
    72  	return InvalidRequestError
    73  }
    74  
    75  type methodNotFoundError string
    76  
    77  func (e methodNotFoundError) Error() string {
    78  	return string(e)
    79  }
    80  func (e methodNotFoundError) ErrorCode() int {
    81  	return MethodNotFoundError
    82  }
    83  
    84  type invalidParamsError string
    85  
    86  func (e invalidParamsError) Error() string {
    87  	return string(e)
    88  }
    89  func (e invalidParamsError) ErrorCode() int {
    90  	return InvalidParamsError
    91  }
    92  
    93  type internalError string
    94  
    95  func (e internalError) Error() string {
    96  	return string(e)
    97  }
    98  func (e internalError) ErrorCode() int {
    99  	return InternalError
   100  }
   101  

View as plain text