github.com/anthropics/anthropic-sdk-go@v1.70.1/shared/shared.go (about) 1 // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. 2 3 package shared 4 5 import ( 6 "encoding/json" 7 8 "github.com/anthropics/anthropic-sdk-go/internal/apijson" 9 "github.com/anthropics/anthropic-sdk-go/packages/param" 10 "github.com/anthropics/anthropic-sdk-go/packages/respjson" 11 "github.com/anthropics/anthropic-sdk-go/shared/constant" 12 ) 13 14 // aliased to make [param.APIUnion] private when embedding 15 type paramUnion = param.APIUnion 16 17 // aliased to make [param.APIObject] private when embedding 18 type paramObj = param.APIObject 19 20 type APIErrorObject struct { 21 Message string `json:"message" api:"required"` 22 Type constant.APIError `json:"type" default:"api_error"` 23 // JSON contains metadata for fields, check presence with [respjson.Field.Valid]. 24 JSON struct { 25 Message respjson.Field 26 Type respjson.Field 27 ExtraFields map[string]respjson.Field 28 raw string 29 } `json:"-"` 30 } 31 32 // Returns the unmodified JSON received from the API 33 func (r APIErrorObject) RawJSON() string { return r.JSON.raw } 34 func (r *APIErrorObject) UnmarshalJSON(data []byte) error { 35 return apijson.UnmarshalRoot(data, r) 36 } 37 38 func (APIErrorObject) ImplErrorObjectUnion() {} 39 40 type AuthenticationError struct { 41 Message string `json:"message" api:"required"` 42 Type constant.AuthenticationError `json:"type" default:"authentication_error"` 43 // JSON contains metadata for fields, check presence with [respjson.Field.Valid]. 44 JSON struct { 45 Message respjson.Field 46 Type respjson.Field 47 ExtraFields map[string]respjson.Field 48 raw string 49 } `json:"-"` 50 } 51 52 // Returns the unmodified JSON received from the API 53 func (r AuthenticationError) RawJSON() string { return r.JSON.raw } 54 func (r *AuthenticationError) UnmarshalJSON(data []byte) error { 55 return apijson.UnmarshalRoot(data, r) 56 } 57 58 func (AuthenticationError) ImplErrorObjectUnion() {} 59 60 type BillingError struct { 61 Message string `json:"message" api:"required"` 62 Type constant.BillingError `json:"type" default:"billing_error"` 63 // JSON contains metadata for fields, check presence with [respjson.Field.Valid]. 64 JSON struct { 65 Message respjson.Field 66 Type respjson.Field 67 ExtraFields map[string]respjson.Field 68 raw string 69 } `json:"-"` 70 } 71 72 // Returns the unmodified JSON received from the API 73 func (r BillingError) RawJSON() string { return r.JSON.raw } 74 func (r *BillingError) UnmarshalJSON(data []byte) error { 75 return apijson.UnmarshalRoot(data, r) 76 } 77 78 func (BillingError) ImplErrorObjectUnion() {} 79 80 // ErrorObjectUnion contains all possible properties and values from 81 // [InvalidRequestError], [AuthenticationError], [BillingError], [PermissionError], 82 // [NotFoundError], [RateLimitError], [GatewayTimeoutError], [APIErrorObject], 83 // [OverloadedError]. 84 // 85 // Use the [ErrorObjectUnion.AsAny] method to switch on the variant. 86 // 87 // Use the methods beginning with 'As' to cast the union to one of its variants. 88 type ErrorObjectUnion struct { 89 Message string `json:"message"` 90 // Any of "invalid_request_error", "authentication_error", "billing_error", 91 // "permission_error", "not_found_error", "rate_limit_error", "timeout_error", 92 // "api_error", "overloaded_error". 93 Type string `json:"type"` 94 JSON struct { 95 Message respjson.Field 96 Type respjson.Field 97 raw string 98 } `json:"-"` 99 } 100 101 // anyErrorObject is implemented by each variant of [ErrorObjectUnion] to add type 102 // safety for the return type of [ErrorObjectUnion.AsAny] 103 type anyErrorObject interface { 104 ImplErrorObjectUnion() 105 } 106 107 // Use the following switch statement to find the correct variant 108 // 109 // switch variant := ErrorObjectUnion.AsAny().(type) { 110 // case shared.InvalidRequestError: 111 // case shared.AuthenticationError: 112 // case shared.BillingError: 113 // case shared.PermissionError: 114 // case shared.NotFoundError: 115 // case shared.RateLimitError: 116 // case shared.GatewayTimeoutError: 117 // case shared.APIErrorObject: 118 // case shared.OverloadedError: 119 // default: 120 // fmt.Errorf("no variant present") 121 // } 122 func (u ErrorObjectUnion) AsAny() anyErrorObject { 123 switch u.Type { 124 case "invalid_request_error": 125 return u.AsInvalidRequestError() 126 case "authentication_error": 127 return u.AsAuthenticationError() 128 case "billing_error": 129 return u.AsBillingError() 130 case "permission_error": 131 return u.AsPermissionError() 132 case "not_found_error": 133 return u.AsNotFoundError() 134 case "rate_limit_error": 135 return u.AsRateLimitError() 136 case "timeout_error": 137 return u.AsTimeoutError() 138 case "api_error": 139 return u.AsAPIError() 140 case "overloaded_error": 141 return u.AsOverloadedError() 142 } 143 return nil 144 } 145 146 func (u ErrorObjectUnion) AsInvalidRequestError() (v InvalidRequestError) { 147 apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v) 148 return 149 } 150 151 func (u ErrorObjectUnion) AsAuthenticationError() (v AuthenticationError) { 152 apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v) 153 return 154 } 155 156 func (u ErrorObjectUnion) AsBillingError() (v BillingError) { 157 apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v) 158 return 159 } 160 161 func (u ErrorObjectUnion) AsPermissionError() (v PermissionError) { 162 apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v) 163 return 164 } 165 166 func (u ErrorObjectUnion) AsNotFoundError() (v NotFoundError) { 167 apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v) 168 return 169 } 170 171 func (u ErrorObjectUnion) AsRateLimitError() (v RateLimitError) { 172 apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v) 173 return 174 } 175 176 func (u ErrorObjectUnion) AsTimeoutError() (v GatewayTimeoutError) { 177 apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v) 178 return 179 } 180 181 func (u ErrorObjectUnion) AsAPIError() (v APIErrorObject) { 182 apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v) 183 return 184 } 185 186 func (u ErrorObjectUnion) AsOverloadedError() (v OverloadedError) { 187 apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v) 188 return 189 } 190 191 // Returns the unmodified JSON received from the API 192 func (u ErrorObjectUnion) RawJSON() string { return u.JSON.raw } 193 194 func (r *ErrorObjectUnion) UnmarshalJSON(data []byte) error { 195 return apijson.UnmarshalRoot(data, r) 196 } 197 198 type ErrorResponse struct { 199 Error ErrorObjectUnion `json:"error" api:"required"` 200 RequestID string `json:"request_id" api:"required"` 201 Type constant.Error `json:"type" default:"error"` 202 // JSON contains metadata for fields, check presence with [respjson.Field.Valid]. 203 JSON struct { 204 Error respjson.Field 205 RequestID respjson.Field 206 Type respjson.Field 207 ExtraFields map[string]respjson.Field 208 raw string 209 } `json:"-"` 210 } 211 212 // Returns the unmodified JSON received from the API 213 func (r ErrorResponse) RawJSON() string { return r.JSON.raw } 214 func (r *ErrorResponse) UnmarshalJSON(data []byte) error { 215 return apijson.UnmarshalRoot(data, r) 216 } 217 218 type ErrorType string 219 220 const ( 221 ErrorTypeInvalidRequestError ErrorType = "invalid_request_error" 222 ErrorTypeAuthenticationError ErrorType = "authentication_error" 223 ErrorTypePermissionError ErrorType = "permission_error" 224 ErrorTypeNotFoundError ErrorType = "not_found_error" 225 ErrorTypeRateLimitError ErrorType = "rate_limit_error" 226 ErrorTypeTimeoutError ErrorType = "timeout_error" 227 ErrorTypeOverloadedError ErrorType = "overloaded_error" 228 ErrorTypeAPIError ErrorType = "api_error" 229 ErrorTypeBillingError ErrorType = "billing_error" 230 ) 231 232 type GatewayTimeoutError struct { 233 Message string `json:"message" api:"required"` 234 Type constant.TimeoutError `json:"type" default:"timeout_error"` 235 // JSON contains metadata for fields, check presence with [respjson.Field.Valid]. 236 JSON struct { 237 Message respjson.Field 238 Type respjson.Field 239 ExtraFields map[string]respjson.Field 240 raw string 241 } `json:"-"` 242 } 243 244 // Returns the unmodified JSON received from the API 245 func (r GatewayTimeoutError) RawJSON() string { return r.JSON.raw } 246 func (r *GatewayTimeoutError) UnmarshalJSON(data []byte) error { 247 return apijson.UnmarshalRoot(data, r) 248 } 249 250 func (GatewayTimeoutError) ImplErrorObjectUnion() {} 251 252 type InvalidRequestError struct { 253 Message string `json:"message" api:"required"` 254 Type constant.InvalidRequestError `json:"type" default:"invalid_request_error"` 255 // JSON contains metadata for fields, check presence with [respjson.Field.Valid]. 256 JSON struct { 257 Message respjson.Field 258 Type respjson.Field 259 ExtraFields map[string]respjson.Field 260 raw string 261 } `json:"-"` 262 } 263 264 // Returns the unmodified JSON received from the API 265 func (r InvalidRequestError) RawJSON() string { return r.JSON.raw } 266 func (r *InvalidRequestError) UnmarshalJSON(data []byte) error { 267 return apijson.UnmarshalRoot(data, r) 268 } 269 270 func (InvalidRequestError) ImplErrorObjectUnion() {} 271 272 type NotFoundError struct { 273 Message string `json:"message" api:"required"` 274 Type constant.NotFoundError `json:"type" default:"not_found_error"` 275 // JSON contains metadata for fields, check presence with [respjson.Field.Valid]. 276 JSON struct { 277 Message respjson.Field 278 Type respjson.Field 279 ExtraFields map[string]respjson.Field 280 raw string 281 } `json:"-"` 282 } 283 284 // Returns the unmodified JSON received from the API 285 func (r NotFoundError) RawJSON() string { return r.JSON.raw } 286 func (r *NotFoundError) UnmarshalJSON(data []byte) error { 287 return apijson.UnmarshalRoot(data, r) 288 } 289 290 func (NotFoundError) ImplErrorObjectUnion() {} 291 292 type OverloadedError struct { 293 Message string `json:"message" api:"required"` 294 Type constant.OverloadedError `json:"type" default:"overloaded_error"` 295 // JSON contains metadata for fields, check presence with [respjson.Field.Valid]. 296 JSON struct { 297 Message respjson.Field 298 Type respjson.Field 299 ExtraFields map[string]respjson.Field 300 raw string 301 } `json:"-"` 302 } 303 304 // Returns the unmodified JSON received from the API 305 func (r OverloadedError) RawJSON() string { return r.JSON.raw } 306 func (r *OverloadedError) UnmarshalJSON(data []byte) error { 307 return apijson.UnmarshalRoot(data, r) 308 } 309 310 func (OverloadedError) ImplErrorObjectUnion() {} 311 312 type PermissionError struct { 313 Message string `json:"message" api:"required"` 314 Type constant.PermissionError `json:"type" default:"permission_error"` 315 // JSON contains metadata for fields, check presence with [respjson.Field.Valid]. 316 JSON struct { 317 Message respjson.Field 318 Type respjson.Field 319 ExtraFields map[string]respjson.Field 320 raw string 321 } `json:"-"` 322 } 323 324 // Returns the unmodified JSON received from the API 325 func (r PermissionError) RawJSON() string { return r.JSON.raw } 326 func (r *PermissionError) UnmarshalJSON(data []byte) error { 327 return apijson.UnmarshalRoot(data, r) 328 } 329 330 func (PermissionError) ImplErrorObjectUnion() {} 331 332 type RateLimitError struct { 333 Message string `json:"message" api:"required"` 334 Type constant.RateLimitError `json:"type" default:"rate_limit_error"` 335 // JSON contains metadata for fields, check presence with [respjson.Field.Valid]. 336 JSON struct { 337 Message respjson.Field 338 Type respjson.Field 339 ExtraFields map[string]respjson.Field 340 raw string 341 } `json:"-"` 342 } 343 344 // Returns the unmodified JSON received from the API 345 func (r RateLimitError) RawJSON() string { return r.JSON.raw } 346 func (r *RateLimitError) UnmarshalJSON(data []byte) error { 347 return apijson.UnmarshalRoot(data, r) 348 } 349 350 func (RateLimitError) ImplErrorObjectUnion() {}