Files
bc-netts-energy/pkg/errors/api_error.go
2025-11-03 19:26:48 +08:00

37 lines
695 B
Go

package errors
import "fmt"
// APIError represents a structured error from an upstream API.
type APIError struct {
Code int
Message string
HTTPStatus int
}
func (e *APIError) Error() string {
if e == nil {
return "<nil>"
}
if e.HTTPStatus > 0 {
return fmt.Sprintf("api error: status=%d code=%d message=%s", e.HTTPStatus, e.Code, e.Message)
}
return fmt.Sprintf("api error: code=%d message=%s", e.Code, e.Message)
}
// Temporary returns true when the error is potentially recoverable.
func (e *APIError) Temporary() bool {
if e == nil {
return false
}
if e.HTTPStatus >= 500 {
return true
}
switch e.Code {
case 429:
return true
default:
return false
}
}