-
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.
* JSON errors * Fixed ish * Fixed errors * Linting * lint
- Loading branch information
1 parent
d9440ed
commit 1a8c274
Showing
4 changed files
with
60 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package output | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
const defaultJSONError = `{"error": "unable to marshal error to JSON %s"}` | ||
|
||
type JSONError struct { | ||
Error string `json:"error"` | ||
} | ||
|
||
func HandleErrors(ctx context.Context, v *viper.Viper, input error) error { | ||
outputType := v.GetString("output") | ||
if outputType != "json" && outputType != JSONPretty { | ||
return input | ||
} | ||
// TODO: @anthony - handle errors better, for example, HTTP errors could be better, see client.go | ||
jsonError, err := MakeJSONFromInterface(ctx, JSONError{Error: input.Error()}, outputType == 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