Files
decort-golang-sdk/errors.go

33 lines
806 B
Go
Raw Normal View History

2026-10-16 15:23:28 +03:00
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})
}