Files
aws-server-sdk/errors.go
T
shiran 3d4c2dbb20
test / go (push) Successful in 1m7s
feat: initial AWS Server Go SDK
2026-08-22 22:14:08 +08:00

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
}