package api import ( "fmt" "time" ) // APIError represents an error response from the Tangled XRPC API. type APIError struct { StatusCode int ErrorType string `json:"error"` Message string `json:"message"` NSID string `json:"-"` // the procedure that failed } func (e *APIError) Error() string { if e.Message != "" { return fmt.Sprintf("%s: %s (HTTP %d)", e.ErrorType, e.Message, e.StatusCode) } return fmt.Sprintf("%s (HTTP %d)", e.ErrorType, e.StatusCode) } // IsNotFound returns true if this is a 404 error. func (e *APIError) IsNotFound() bool { return e.StatusCode == 404 } // IsForbidden returns true if this is a 403 error. func (e *APIError) IsForbidden() bool { return e.StatusCode == 403 } // IsUnauthorized returns true if this is a 401 error. func (e *APIError) IsUnauthorized() bool { return e.StatusCode == 401 } // RateLimitError indicates the API rate limit has been exceeded. type RateLimitError struct { RetryAfter time.Duration Inner *APIError } func (e *RateLimitError) Error() string { return fmt.Sprintf("rate limited: retry after %s", e.RetryAfter) } func (e *RateLimitError) Unwrap() error { return e.Inner }