45 lines
1.0 KiB
Go
45 lines
1.0 KiB
Go
package awsserversdk
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
type APIError struct {
|
|
StatusCode int
|
|
Code int
|
|
Message string
|
|
Data json.RawMessage
|
|
Body string
|
|
}
|
|
|
|
func (e *APIError) Error() string {
|
|
return fmt.Sprintf("aws server API: HTTP %d code %d: %s", e.StatusCode, e.Code, e.Message)
|
|
}
|
|
func IsUnauthorized(err error) bool {
|
|
value, ok := err.(*APIError)
|
|
return ok && (value.StatusCode == http.StatusUnauthorized || value.StatusCode == http.StatusForbidden)
|
|
}
|
|
func IsConflict(err error) bool {
|
|
value, ok := err.(*APIError)
|
|
return ok && value.StatusCode == http.StatusConflict
|
|
}
|
|
func IsBudgetExceeded(err error) bool {
|
|
value, ok := err.(*APIError)
|
|
if !ok || value.StatusCode != http.StatusConflict {
|
|
return false
|
|
}
|
|
var data map[string]any
|
|
return json.Unmarshal(value.Data, &data) == nil && data["shortfall"] != nil
|
|
}
|
|
|
|
type TaskError struct{ Task Task }
|
|
|
|
func (e *TaskError) Error() string {
|
|
if e.Task.ErrorMessage != "" {
|
|
return e.Task.ErrorMessage
|
|
}
|
|
return "operation task failed: " + e.Task.Status
|
|
}
|