33 lines
806 B
Go
33 lines
806 B
Go
|
|
package decortsdk
|
||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
"net/http"
|
||
|
|
)
|
||
|
|
|
||
|
|
// HTTPError is returned when the API responds with a non-successful status code.
|
||
|
|
type HTTPError struct {
|
||
|
|
StatusCode int
|
||
|
|
Body []byte
|
||
|
|
}
|
||
|
|
|
||
|
|
func (e *HTTPError) Error() string {
|
||
|
|
return string(e.Body)
|
||
|
|
}
|
||
|
|
|
||
|
|
// IsNotFound reports whether err contains an HTTPError with status 404.
|
||
|
|
func IsNotFound(err error) bool {
|
||
|
|
return HasStatusCode(err, http.StatusNotFound)
|
||
|
|
}
|
||
|
|
|
||
|
|
// HasStatusCode reports whether err contains an HTTPError with the given status code.
|
||
|
|
func HasStatusCode(err error, code int) bool {
|
||
|
|
var httpErr *HTTPError
|
||
|
|
return errors.As(err, &httpErr) && httpErr.StatusCode == code
|
||
|
|
}
|
||
|
|
|
||
|
|
func newHTTPError(prefix string, statusCode int, body []byte) error {
|
||
|
|
return fmt.Errorf("%s: %w", prefix, &HTTPError{StatusCode: statusCode, Body: body})
|
||
|
|
}
|