-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
49 lines (40 loc) · 1.41 KB
/
errors.go
File metadata and controls
49 lines (40 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package unstructured
import (
"errors"
"fmt"
)
// HTTPValidationError represents the structure of validation error responses
// returned by the API when a 422 status code is returned.
type HTTPValidationError struct {
Detail []*ValidationError `json:"detail"`
}
func (e *HTTPValidationError) Error() string {
errs := make([]error, len(e.Detail))
for i, err := range e.Detail {
errs[i] = err
}
return fmt.Sprintf("%d validation errors: %s", len(e.Detail), errors.Join(errs...))
}
// ValidationError represents a single validation error within the HTTPValidationError response.
type ValidationError struct {
// Location is an array that can contain strings or integers indicating
// where the validation error occurred (e.g., field names, array indices).
Location []any `json:"loc"`
// Message is a string describing the validation error.
Message string `json:"msg"`
// Type is a string indicating the type of error.
Type string `json:"type"`
}
func (e *ValidationError) Error() string {
return fmt.Sprintf("%s at %v: %s", e.Type, e.Location, e.Message)
}
// APIError represents an error returned by the API when a non-200 status code is returned.
type APIError struct {
Code int
Err error
}
// Error returns a string representation of the API error.
func (e *APIError) Error() string {
return fmt.Sprintf("an API error occurred: [%d] %s", e.Code, e.Err.Error())
}
func (e *APIError) Unwrap() error { return e.Err }