|
| 1 | +// Package errors provides typed error types for the SCM Go SDK. |
| 2 | +// |
| 3 | +// This package defines a comprehensive hierarchy of error types that match |
| 4 | +// the Palo Alto Networks Strata Cloud Manager API error responses. All errors |
| 5 | +// implement the ScmError interface and provide rich context about what went wrong. |
| 6 | +// |
| 7 | +// Error Hierarchy: |
| 8 | +// |
| 9 | +// ScmError (interface) |
| 10 | +// ├── ClientError (4xx errors) |
| 11 | +// │ ├── AuthenticationError (401) |
| 12 | +// │ │ ├── NotAuthenticatedError |
| 13 | +// │ │ ├── InvalidCredentialError |
| 14 | +// │ │ └── KeyExpiredError |
| 15 | +// │ ├── AuthorizationError (403) |
| 16 | +// │ ├── BadRequestError (400) |
| 17 | +// │ │ ├── InvalidObjectError |
| 18 | +// │ │ ├── MissingQueryParameterError |
| 19 | +// │ │ ├── InvalidQueryParameterError |
| 20 | +// │ │ └── MalformedCommandError |
| 21 | +// │ ├── NotFoundError (404) |
| 22 | +// │ │ └── ObjectNotPresentError |
| 23 | +// │ ├── ConflictError (409) |
| 24 | +// │ │ ├── NameNotUniqueError |
| 25 | +// │ │ ├── ObjectNotUniqueError |
| 26 | +// │ │ └── ReferenceNotZeroError |
| 27 | +// │ ├── MethodNotAllowedError (405) |
| 28 | +// │ ├── RequestTimeoutError (408) |
| 29 | +// │ ├── TooManyRequestsError (429) |
| 30 | +// │ └── SessionTimedOutError |
| 31 | +// └── ServerError (5xx errors) |
| 32 | +// ├── InternalServerError (500) |
| 33 | +// ├── BadGatewayError (502) |
| 34 | +// ├── ServiceUnavailableError (503) |
| 35 | +// └── GatewayTimeoutError (504) |
| 36 | +// |
| 37 | +// Usage Example: |
| 38 | +// |
| 39 | +// import ( |
| 40 | +// scmErrors "github.com/paloaltonetworks/scm-go/errors" |
| 41 | +// ) |
| 42 | +// |
| 43 | +// resp, _, err := api.CreateAddresses(ctx).Addresses(addr).Execute() |
| 44 | +// if err != nil { |
| 45 | +// // Check for specific error types |
| 46 | +// if scmErrors.IsNameNotUnique(err) { |
| 47 | +// nameErr := err.(*scmErrors.NameNotUniqueError) |
| 48 | +// log.Printf("Name conflict: %s", nameErr.ObjectName) |
| 49 | +// return handleNameConflict(nameErr) |
| 50 | +// } |
| 51 | +// |
| 52 | +// if scmErrors.IsObjectNotPresent(err) { |
| 53 | +// log.Printf("Object not found") |
| 54 | +// return nil |
| 55 | +// } |
| 56 | +// |
| 57 | +// // Generic error handling |
| 58 | +// return err |
| 59 | +// } |
| 60 | +package errors |
| 61 | + |
| 62 | +import "fmt" |
| 63 | + |
| 64 | +// ScmError is the base interface for all SCM SDK errors. |
| 65 | +// All typed errors in this package implement this interface. |
| 66 | +type ScmError interface { |
| 67 | + error |
| 68 | + // IsScmError identifies this as an SCM SDK error |
| 69 | + IsScmError() bool |
| 70 | + // HTTPStatusCode returns the HTTP status code if applicable |
| 71 | + HTTPStatusCode() int |
| 72 | + // ErrorCode returns the SCM-specific error code (e.g., "E003") |
| 73 | + ErrorCode() string |
| 74 | + // ErrorMessage returns the detailed error message |
| 75 | + ErrorMessage() string |
| 76 | + // ErrorDetails returns additional error context |
| 77 | + ErrorDetails() map[string]interface{} |
| 78 | +} |
| 79 | + |
| 80 | +// BaseError provides common functionality for all SCM errors. |
| 81 | +// It implements the ScmError interface and can be embedded in specific error types. |
| 82 | +type BaseError struct { |
| 83 | + StatusCode int |
| 84 | + Code string |
| 85 | + Message string |
| 86 | + Details map[string]interface{} |
| 87 | +} |
| 88 | + |
| 89 | +// Error implements the error interface |
| 90 | +func (e *BaseError) Error() string { |
| 91 | + if e.Code != "" { |
| 92 | + return fmt.Sprintf("[%s] %s", e.Code, e.Message) |
| 93 | + } |
| 94 | + return e.Message |
| 95 | +} |
| 96 | + |
| 97 | +// IsScmError identifies this as an SCM error |
| 98 | +func (e *BaseError) IsScmError() bool { return true } |
| 99 | + |
| 100 | +// HTTPStatusCode returns the HTTP status code |
| 101 | +func (e *BaseError) HTTPStatusCode() int { return e.StatusCode } |
| 102 | + |
| 103 | +// ErrorCode returns the SCM error code |
| 104 | +func (e *BaseError) ErrorCode() string { return e.Code } |
| 105 | + |
| 106 | +// ErrorMessage returns the error message |
| 107 | +func (e *BaseError) ErrorMessage() string { return e.Message } |
| 108 | + |
| 109 | +// ErrorDetails returns additional error details |
| 110 | +func (e *BaseError) ErrorDetails() map[string]interface{} { |
| 111 | + if e.Details == nil { |
| 112 | + return make(map[string]interface{}) |
| 113 | + } |
| 114 | + return e.Details |
| 115 | +} |
| 116 | + |
| 117 | +// ============================================================================ |
| 118 | +// Client Errors (4xx) |
| 119 | +// ============================================================================ |
| 120 | + |
| 121 | +// ClientError represents a 4xx client error. |
| 122 | +// This is the base type for all client-side errors. |
| 123 | +type ClientError struct { |
| 124 | + BaseError |
| 125 | +} |
| 126 | + |
| 127 | +// ============================================================================ |
| 128 | +// Authentication Errors (401) |
| 129 | +// ============================================================================ |
| 130 | + |
| 131 | +// AuthenticationError represents a 401 authentication error. |
| 132 | +// This indicates the request lacks valid authentication credentials. |
| 133 | +type AuthenticationError struct { |
| 134 | + ClientError |
| 135 | +} |
| 136 | + |
| 137 | +// NotAuthenticatedError indicates the request is not authenticated. |
| 138 | +// Error Code: E003 |
| 139 | +type NotAuthenticatedError struct { |
| 140 | + AuthenticationError |
| 141 | +} |
| 142 | + |
| 143 | +// InvalidCredentialError indicates invalid credentials were provided. |
| 144 | +// Error Code: E004 |
| 145 | +type InvalidCredentialError struct { |
| 146 | + AuthenticationError |
| 147 | +} |
| 148 | + |
| 149 | +// KeyExpiredError indicates the API key has expired. |
| 150 | +// Error Code: E013 |
| 151 | +type KeyExpiredError struct { |
| 152 | + AuthenticationError |
| 153 | +} |
| 154 | + |
| 155 | +// ============================================================================ |
| 156 | +// Authorization Errors (403) |
| 157 | +// ============================================================================ |
| 158 | + |
| 159 | +// AuthorizationError represents a 403 authorization error. |
| 160 | +// This indicates the authenticated user lacks permission for the operation. |
| 161 | +// Error Code: E009 |
| 162 | +type AuthorizationError struct { |
| 163 | + ClientError |
| 164 | +} |
| 165 | + |
| 166 | +// ============================================================================ |
| 167 | +// Bad Request Errors (400) |
| 168 | +// ============================================================================ |
| 169 | + |
| 170 | +// BadRequestError represents a 400 bad request error. |
| 171 | +// This is the base type for malformed or invalid requests. |
| 172 | +type BadRequestError struct { |
| 173 | + ClientError |
| 174 | +} |
| 175 | + |
| 176 | +// InvalidObjectError indicates the object failed validation. |
| 177 | +// Error Code: E023 |
| 178 | +type InvalidObjectError struct { |
| 179 | + BadRequestError |
| 180 | + ObjectName string |
| 181 | +} |
| 182 | + |
| 183 | +// MissingQueryParameterError indicates a required query parameter is missing. |
| 184 | +// Error Code: E007 |
| 185 | +type MissingQueryParameterError struct { |
| 186 | + BadRequestError |
| 187 | + ParameterName string |
| 188 | +} |
| 189 | + |
| 190 | +// InvalidQueryParameterError indicates a query parameter has an invalid value. |
| 191 | +// Error Code: E024 |
| 192 | +type InvalidQueryParameterError struct { |
| 193 | + BadRequestError |
| 194 | + ParameterName string |
| 195 | + ParameterValue string |
| 196 | +} |
| 197 | + |
| 198 | +// MalformedCommandError indicates the command syntax is invalid. |
| 199 | +// Error Code: E006 |
| 200 | +type MalformedCommandError struct { |
| 201 | + BadRequestError |
| 202 | +} |
| 203 | + |
| 204 | +// ============================================================================ |
| 205 | +// Not Found Errors (404) |
| 206 | +// ============================================================================ |
| 207 | + |
| 208 | +// NotFoundError represents a 404 not found error. |
| 209 | +// This is the base type for resource-not-found errors. |
| 210 | +type NotFoundError struct { |
| 211 | + ClientError |
| 212 | +} |
| 213 | + |
| 214 | +// ObjectNotPresentError indicates the requested object doesn't exist. |
| 215 | +// Error Code: E005 |
| 216 | +type ObjectNotPresentError struct { |
| 217 | + NotFoundError |
| 218 | + ObjectID string |
| 219 | + ObjectName string |
| 220 | +} |
| 221 | + |
| 222 | +// ============================================================================ |
| 223 | +// Conflict Errors (409) |
| 224 | +// ============================================================================ |
| 225 | + |
| 226 | +// ConflictError represents a 409 conflict error. |
| 227 | +// This is the base type for resource conflicts. |
| 228 | +type ConflictError struct { |
| 229 | + ClientError |
| 230 | +} |
| 231 | + |
| 232 | +// NameNotUniqueError indicates a name conflict (duplicate name). |
| 233 | +// Error Code: E016 |
| 234 | +type NameNotUniqueError struct { |
| 235 | + ConflictError |
| 236 | + ObjectName string |
| 237 | +} |
| 238 | + |
| 239 | +// ObjectNotUniqueError indicates an object conflict. |
| 240 | +// Error Code: E017 |
| 241 | +type ObjectNotUniqueError struct { |
| 242 | + ConflictError |
| 243 | + ObjectID string |
| 244 | + ObjectName string |
| 245 | +} |
| 246 | + |
| 247 | +// ReferenceNotZeroError indicates the object has references preventing deletion. |
| 248 | +// Error Code: E018 |
| 249 | +type ReferenceNotZeroError struct { |
| 250 | + ConflictError |
| 251 | + ObjectName string |
| 252 | + ReferenceCount int |
| 253 | +} |
| 254 | + |
| 255 | +// ============================================================================ |
| 256 | +// Other Client Errors |
| 257 | +// ============================================================================ |
| 258 | + |
| 259 | +// MethodNotAllowedError represents a 405 method not allowed error. |
| 260 | +// Error Code: E010 |
| 261 | +type MethodNotAllowedError struct { |
| 262 | + ClientError |
| 263 | + Method string |
| 264 | +} |
| 265 | + |
| 266 | +// RequestTimeoutError represents a 408 request timeout error. |
| 267 | +// Error Code: E011 |
| 268 | +type RequestTimeoutError struct { |
| 269 | + ClientError |
| 270 | +} |
| 271 | + |
| 272 | +// TooManyRequestsError represents a 429 too many requests error (rate limiting). |
| 273 | +// Error Code: E012 |
| 274 | +type TooManyRequestsError struct { |
| 275 | + ClientError |
| 276 | + RetryAfter int // Seconds to wait before retrying |
| 277 | +} |
| 278 | + |
| 279 | +// SessionTimedOutError indicates the session has expired. |
| 280 | +// Error Code: E019 |
| 281 | +type SessionTimedOutError struct { |
| 282 | + ClientError |
| 283 | +} |
| 284 | + |
| 285 | +// ============================================================================ |
| 286 | +// Server Errors (5xx) |
| 287 | +// ============================================================================ |
| 288 | + |
| 289 | +// ServerError represents a 5xx server error. |
| 290 | +// This is the base type for all server-side errors. |
| 291 | +type ServerError struct { |
| 292 | + BaseError |
| 293 | +} |
| 294 | + |
| 295 | +// InternalServerError represents a 500 internal server error. |
| 296 | +// Error Code: E020 |
| 297 | +type InternalServerError struct { |
| 298 | + ServerError |
| 299 | +} |
| 300 | + |
| 301 | +// BadGatewayError represents a 502 bad gateway error. |
| 302 | +// Error Code: E021 |
| 303 | +type BadGatewayError struct { |
| 304 | + ServerError |
| 305 | +} |
| 306 | + |
| 307 | +// ServiceUnavailableError represents a 503 service unavailable error. |
| 308 | +// Error Code: E022 |
| 309 | +type ServiceUnavailableError struct { |
| 310 | + ServerError |
| 311 | +} |
| 312 | + |
| 313 | +// GatewayTimeoutError represents a 504 gateway timeout error. |
| 314 | +// Error Code: E024 |
| 315 | +type GatewayTimeoutError struct { |
| 316 | + ServerError |
| 317 | +} |
0 commit comments