1// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
3package shared
4
5import (
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
15type paramUnion = param.APIUnion
16
17// aliased to make [param.APIObject] private when embedding
18type paramObj = param.APIObject
19
20type APIErrorObject struct {
21 Message string `json:"message,required"`
22 Type constant.APIError `json:"type,required"`
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
33func (r APIErrorObject) RawJSON() string { return r.JSON.raw }
34func (r *APIErrorObject) UnmarshalJSON(data []byte) error {
35 return apijson.UnmarshalRoot(data, r)
36}
37
38func (APIErrorObject) ImplErrorObjectUnion() {}
39
40type AuthenticationError struct {
41 Message string `json:"message,required"`
42 Type constant.AuthenticationError `json:"type,required"`
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
53func (r AuthenticationError) RawJSON() string { return r.JSON.raw }
54func (r *AuthenticationError) UnmarshalJSON(data []byte) error {
55 return apijson.UnmarshalRoot(data, r)
56}
57
58func (AuthenticationError) ImplErrorObjectUnion() {}
59
60type BillingError struct {
61 Message string `json:"message,required"`
62 Type constant.BillingError `json:"type,required"`
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
73func (r BillingError) RawJSON() string { return r.JSON.raw }
74func (r *BillingError) UnmarshalJSON(data []byte) error {
75 return apijson.UnmarshalRoot(data, r)
76}
77
78func (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.
88type 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]
103type 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// }
122func (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
146func (u ErrorObjectUnion) AsInvalidRequestError() (v InvalidRequestError) {
147 apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
148 return
149}
150
151func (u ErrorObjectUnion) AsAuthenticationError() (v AuthenticationError) {
152 apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
153 return
154}
155
156func (u ErrorObjectUnion) AsBillingError() (v BillingError) {
157 apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
158 return
159}
160
161func (u ErrorObjectUnion) AsPermissionError() (v PermissionError) {
162 apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
163 return
164}
165
166func (u ErrorObjectUnion) AsNotFoundError() (v NotFoundError) {
167 apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
168 return
169}
170
171func (u ErrorObjectUnion) AsRateLimitError() (v RateLimitError) {
172 apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
173 return
174}
175
176func (u ErrorObjectUnion) AsTimeoutError() (v GatewayTimeoutError) {
177 apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
178 return
179}
180
181func (u ErrorObjectUnion) AsAPIError() (v APIErrorObject) {
182 apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
183 return
184}
185
186func (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
192func (u ErrorObjectUnion) RawJSON() string { return u.JSON.raw }
193
194func (r *ErrorObjectUnion) UnmarshalJSON(data []byte) error {
195 return apijson.UnmarshalRoot(data, r)
196}
197
198type ErrorResponse struct {
199 Error ErrorObjectUnion `json:"error,required"`
200 Type constant.Error `json:"type,required"`
201 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
202 JSON struct {
203 Error respjson.Field
204 Type respjson.Field
205 ExtraFields map[string]respjson.Field
206 raw string
207 } `json:"-"`
208}
209
210// Returns the unmodified JSON received from the API
211func (r ErrorResponse) RawJSON() string { return r.JSON.raw }
212func (r *ErrorResponse) UnmarshalJSON(data []byte) error {
213 return apijson.UnmarshalRoot(data, r)
214}
215
216type GatewayTimeoutError struct {
217 Message string `json:"message,required"`
218 Type constant.TimeoutError `json:"type,required"`
219 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
220 JSON struct {
221 Message respjson.Field
222 Type respjson.Field
223 ExtraFields map[string]respjson.Field
224 raw string
225 } `json:"-"`
226}
227
228// Returns the unmodified JSON received from the API
229func (r GatewayTimeoutError) RawJSON() string { return r.JSON.raw }
230func (r *GatewayTimeoutError) UnmarshalJSON(data []byte) error {
231 return apijson.UnmarshalRoot(data, r)
232}
233
234func (GatewayTimeoutError) ImplErrorObjectUnion() {}
235
236type InvalidRequestError struct {
237 Message string `json:"message,required"`
238 Type constant.InvalidRequestError `json:"type,required"`
239 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
240 JSON struct {
241 Message respjson.Field
242 Type respjson.Field
243 ExtraFields map[string]respjson.Field
244 raw string
245 } `json:"-"`
246}
247
248// Returns the unmodified JSON received from the API
249func (r InvalidRequestError) RawJSON() string { return r.JSON.raw }
250func (r *InvalidRequestError) UnmarshalJSON(data []byte) error {
251 return apijson.UnmarshalRoot(data, r)
252}
253
254func (InvalidRequestError) ImplErrorObjectUnion() {}
255
256type NotFoundError struct {
257 Message string `json:"message,required"`
258 Type constant.NotFoundError `json:"type,required"`
259 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
260 JSON struct {
261 Message respjson.Field
262 Type respjson.Field
263 ExtraFields map[string]respjson.Field
264 raw string
265 } `json:"-"`
266}
267
268// Returns the unmodified JSON received from the API
269func (r NotFoundError) RawJSON() string { return r.JSON.raw }
270func (r *NotFoundError) UnmarshalJSON(data []byte) error {
271 return apijson.UnmarshalRoot(data, r)
272}
273
274func (NotFoundError) ImplErrorObjectUnion() {}
275
276type OverloadedError struct {
277 Message string `json:"message,required"`
278 Type constant.OverloadedError `json:"type,required"`
279 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
280 JSON struct {
281 Message respjson.Field
282 Type respjson.Field
283 ExtraFields map[string]respjson.Field
284 raw string
285 } `json:"-"`
286}
287
288// Returns the unmodified JSON received from the API
289func (r OverloadedError) RawJSON() string { return r.JSON.raw }
290func (r *OverloadedError) UnmarshalJSON(data []byte) error {
291 return apijson.UnmarshalRoot(data, r)
292}
293
294func (OverloadedError) ImplErrorObjectUnion() {}
295
296type PermissionError struct {
297 Message string `json:"message,required"`
298 Type constant.PermissionError `json:"type,required"`
299 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
300 JSON struct {
301 Message respjson.Field
302 Type respjson.Field
303 ExtraFields map[string]respjson.Field
304 raw string
305 } `json:"-"`
306}
307
308// Returns the unmodified JSON received from the API
309func (r PermissionError) RawJSON() string { return r.JSON.raw }
310func (r *PermissionError) UnmarshalJSON(data []byte) error {
311 return apijson.UnmarshalRoot(data, r)
312}
313
314func (PermissionError) ImplErrorObjectUnion() {}
315
316type RateLimitError struct {
317 Message string `json:"message,required"`
318 Type constant.RateLimitError `json:"type,required"`
319 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
320 JSON struct {
321 Message respjson.Field
322 Type respjson.Field
323 ExtraFields map[string]respjson.Field
324 raw string
325 } `json:"-"`
326}
327
328// Returns the unmodified JSON received from the API
329func (r RateLimitError) RawJSON() string { return r.JSON.raw }
330func (r *RateLimitError) UnmarshalJSON(data []byte) error {
331 return apijson.UnmarshalRoot(data, r)
332}
333
334func (RateLimitError) ImplErrorObjectUnion() {}