-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* New Error Scheme * Linting * Using as * Get rid of useless field
- Loading branch information
1 parent
6b1c443
commit 42d62d9
Showing
12 changed files
with
94 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
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 { | ||
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.