/
error.go
76 lines (65 loc) · 1.82 KB
/
error.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package client
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"github.com/conductorone/cone/pkg/output"
"github.com/spf13/viper"
)
const defaultJSONError = `{"error": "unable to marshal error to JSON %s"}`
type JSONError struct {
Error string `json:"error"`
}
type HTTPError struct {
StatusCode int `json:"status_code"`
Body string `json:"body"`
}
func NewHTTPError(resp *http.Response) *HTTPError {
// This is added temporarily to ensure we return an error if we get a non-success status code.
// Eventually (ideally), we'll be generating this error handling as part of the SDK
if resp.StatusCode >= http.StatusBadRequest {
var httpErr HTTPError
body, err := io.ReadAll(resp.Body)
if err != nil {
httpErr = HTTPError{
StatusCode: resp.StatusCode,
Body: fmt.Errorf("unable to read response body: %w", err).Error(),
}
} else {
httpErr = HTTPError{
StatusCode: resp.StatusCode,
Body: string(body),
}
}
return &httpErr
}
return nil
}
func (e *HTTPError) Error() string {
return fmt.Sprintf("%d: %s", e.StatusCode, e.Body)
}
func HandleErrors(ctx context.Context, v *viper.Viper, input error) error {
if v == nil {
return input
}
outputType := v.GetString("output")
if outputType != "json" && outputType != output.JSONPretty {
return input
}
var jsonError []byte
var httpErr *HTTPError
if errors.As(input, &httpErr) {
jsonError, err := output.MakeJSONFromInterface(ctx, httpErr, outputType == output.JSONPretty)
if err != nil {
return fmt.Errorf(defaultJSONError, httpErr.Error())
}
return fmt.Errorf(string(jsonError))
}
jsonError, err := output.MakeJSONFromInterface(ctx, JSONError{Error: input.Error()}, outputType == output.JSONPretty)
if err != nil {
return fmt.Errorf(defaultJSONError, input.Error())
}
return fmt.Errorf(string(jsonError))
}