diff --git a/pkg/codec/errors.go b/pkg/codec/errors.go index f3736e65bbcf639dd610f92b12f17e16a98f76af..5efd2313a3b49bef61151426170da3dcf5ac596d 100644 --- a/pkg/codec/errors.go +++ b/pkg/codec/errors.go @@ -7,20 +7,22 @@ import ( "github.com/go-faster/jx" ) -// HTTPError is returned by client operations when the HTTP status code of the -// response is not a 2xx status. -type HTTPError struct { - StatusCode int - Status string - Body []byte -} +// Error types defined below are the built-in JSON-RPC errors. -func (err HTTPError) Error() string { - if len(err.Body) == 0 { - return err.Status - } - return fmt.Sprintf("%v: %s", err.Status, err.Body) -} +var ( + _ Error = new(ErrorMethodNotFound) + _ Error = new(ErrorSubscriptionNotFound) + _ Error = new(ErrorParse) + _ Error = new(ErrorInvalidRequest) + _ Error = new(ErrorInvalidMessage) + _ Error = new(ErrorInvalidParams) +) + +const ( + ErrorCodeDefault = -32000 + ErrorCodeApplication = -32080 + ErrorCodeJrpc = -42000 +) // Error wraps RPC errors, which contain an error code in addition to the message. type Error interface { @@ -88,27 +90,9 @@ func MakeJrpcErr(s string) error { return fmt.Errorf("%w: %s", &JrpcErr{}, s) } -// Error types defined below are the built-in JSON-RPC errors. - -var ( - _ Error = new(ErrorMethodNotFound) - _ Error = new(ErrorSubscriptionNotFound) - _ Error = new(ErrorParse) - _ Error = new(ErrorInvalidRequest) - _ Error = new(ErrorInvalidMessage) - _ Error = new(ErrorInvalidParams) -) - -const ErrorCodeDefault = -32000 - -const ErrorCodeApplication = -32080 - -const ErrorCodeJrpc = -42000 - type ErrorMethodNotFound struct{ method string } func (e *ErrorMethodNotFound) ErrorCode() int { return -32601 } - func (e *ErrorMethodNotFound) Error() string { return fmt.Sprintf("the method %s does not exist/is not available", e.method) } @@ -122,7 +106,6 @@ func NewMethodNotFoundError(method string) *ErrorMethodNotFound { type ErrorSubscriptionNotFound struct{ namespace, subscription string } func (e *ErrorSubscriptionNotFound) ErrorCode() int { return -32601 } - func (e *ErrorSubscriptionNotFound) Error() string { return fmt.Sprintf("no %q subscription in %s namespace", e.subscription, e.namespace) } @@ -131,38 +114,45 @@ func (e *ErrorSubscriptionNotFound) Error() string { type ErrorParse struct{ message string } func (e *ErrorParse) ErrorCode() int { return -32700 } - -func (e *ErrorParse) Error() string { return e.message } +func (e *ErrorParse) Error() string { return e.message } func NewInvalidRequestError(message string) *ErrorInvalidRequest { - return &ErrorInvalidRequest{ - message: message, - } + return &ErrorInvalidRequest{message: message} } // received message isn't a valid request type ErrorInvalidRequest struct{ message string } func (e *ErrorInvalidRequest) ErrorCode() int { return -32600 } - -func (e *ErrorInvalidRequest) Error() string { return e.message } +func (e *ErrorInvalidRequest) Error() string { return e.message } // received message is invalid type ErrorInvalidMessage struct{ message string } func (e *ErrorInvalidMessage) ErrorCode() int { return -32700 } - -func (e *ErrorInvalidMessage) Error() string { return e.message } +func (e *ErrorInvalidMessage) Error() string { return e.message } func NewInvalidParamsError(message string) *ErrorInvalidParams { - return &ErrorInvalidParams{ - message: message, - } + return &ErrorInvalidParams{message: message} } // unable to decode supplied params, or an invalid number of parameters type ErrorInvalidParams struct{ message string } func (e *ErrorInvalidParams) ErrorCode() int { return -32602 } +func (e *ErrorInvalidParams) Error() string { return e.message } -func (e *ErrorInvalidParams) Error() string { return e.message } +// HTTPError is returned by client operations when the HTTP status code of the +// response is not a 2xx status. +type HTTPError struct { + StatusCode int + Status string + Body []byte +} + +func (err HTTPError) Error() string { + if len(err.Body) == 0 { + return err.Status + } + return fmt.Sprintf("%v: %s", err.Status, err.Body) +}