rpc: add HTTPError type for HTTP error responses (#22677)
The new error type is returned by client operations contains details of the response error code and response body. Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
@ -18,6 +18,35 @@ package rpc
|
||||
|
||||
import "fmt"
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// Error wraps RPC errors, which contain an error code in addition to the message.
|
||||
type Error interface {
|
||||
Error() string // returns the message
|
||||
ErrorCode() int // returns the code
|
||||
}
|
||||
|
||||
// A DataError contains some data in addition to the error message.
|
||||
type DataError interface {
|
||||
Error() string // returns the message
|
||||
ErrorData() interface{} // returns the error data
|
||||
}
|
||||
|
||||
// Error types defined below are the built-in JSON-RPC errors.
|
||||
|
||||
var (
|
||||
_ Error = new(methodNotFoundError)
|
||||
_ Error = new(subscriptionNotFoundError)
|
||||
|
Reference in New Issue
Block a user