From 538761d691b20f416f8ae188b28b37536d0e0378 Mon Sep 17 00:00:00 2001 From: Travis DePrato Date: Mon, 9 Aug 2021 17:36:42 -0700 Subject: [PATCH] Improve error reporting/logging --- .gitignore | 1 + cmd/root.go | 14 ++++++++++++-- internal/api/client.go | 3 ++- internal/api/codex_upload.go | 27 ++++++++++++++++++++------- internal/codex/upload.go | 12 +++++++++--- 5 files changed, 44 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index fd9e2f1..3cf34d1 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ *.so *.dylib main +pbauthor dist/ # Test binary, built with `go test -c` diff --git a/cmd/root.go b/cmd/root.go index c3f3af4..73ec616 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -44,8 +44,18 @@ func Execute() { } func init() { - rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output") - rootCmd.PersistentFlags().StringVar(&config.PathbirdApiHost, "api-host", config.PathbirdApiHost, "Pathbird API host") + rootCmd.PersistentFlags().BoolVar( + &verbose, + "debug", + false, + "enable verbose logging (for debugging)", + ) + rootCmd.PersistentFlags().StringVar( + &config.PathbirdApiHost, + "api-host", + config.PathbirdApiHost, + "Pathbird API host", + ) rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(auth.Cmd) diff --git a/internal/api/client.go b/internal/api/client.go index de3b95a..f2ef6a0 100644 --- a/internal/api/client.go +++ b/internal/api/client.go @@ -122,7 +122,8 @@ func (r *response) unmarshalErrorBody() (*ErrorResponse, error) { }, nil } return nil, errors.Errorf( - "unknown api error response (status: %s, content-type: %s)", + "unknown api error response (route: %s, status: %s, content-type: %s)", + r.route, r.httpResponse.Status, contentType, ) diff --git a/internal/api/codex_upload.go b/internal/api/codex_upload.go index bed7425..d3a7589 100644 --- a/internal/api/codex_upload.go +++ b/internal/api/codex_upload.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "github.com/pkg/errors" + log "github.com/sirupsen/logrus" ) type UploadCodexRequest struct { @@ -29,7 +30,7 @@ type CodexParseError struct { Error string `json:"error"` Message string `json:"message"` SourcePosition string `json:"sourcePosition"` - SourceInfo struct{ + SourceInfo struct { SourceContext struct { Lines []string `json:"lines"` } `json:"sourceContext"` @@ -46,7 +47,9 @@ func (e *CodexParseFailedError) Error() string { var _ error = (*CodexParseFailedError)(nil) -func (c *Client) UploadCodex(r *UploadCodexRequest) (*UploadCodexResponse, *CodexParseFailedError, error) { +func (c *Client) UploadCodex( + r *UploadCodexRequest, +) (*UploadCodexResponse, *CodexParseFailedError, error) { fields := make(map[string]string) fields["codexCategoryId"] = r.CodexCategoryId if r.CodexId != "" { @@ -65,18 +68,28 @@ func (c *Client) UploadCodex(r *UploadCodexRequest) (*UploadCodexResponse, *Code return nil, nil, err } if statusError != nil { - e := statusError.error.Error - if e == "MySTParseFailedErr" || e == "CodexASTParseFailedErr" { + switch statusError.error.Error { + case "MySTParseFailedErr", "CodexASTParseFailedErr": var parseError CodexParseFailedError if err := json.Unmarshal(statusError.error.Details, &parseError); err != nil { return nil, nil, errors.Wrap(err, "failed to unmarshal codex parse error details") } return nil, &parseError, nil - } - if statusError.error.Error == "" { + + case "ErrUnauthenticated": + log.WithError(statusError).Debug("got ErrUnauthenticated") + return nil, nil, errors.New("You are not logged in (try running `pbauthor auth login`)") + + case "": return nil, nil, errors.Errorf("API returned an unknown error") + + default: + return nil, nil, errors.Errorf( + "API returned an error: %s: %s", + statusError.error.Error, + statusError.error.Message, + ) } - return nil, nil, errors.Errorf("API returned an error: %s %s", statusError.error.Error, statusError.error.Message) } resp := &UploadCodexResponse{} err = res.UnmarshalJson(resp) diff --git a/internal/codex/upload.go b/internal/codex/upload.go index f07494f..42d8a8e 100644 --- a/internal/codex/upload.go +++ b/internal/codex/upload.go @@ -14,7 +14,10 @@ type UploadCodexOptions struct { Dir string } -func UploadCodex(client *api.Client, opts *UploadCodexOptions) (*api.UploadCodexResponse, *api.CodexParseFailedError, error) { +func UploadCodex( + client *api.Client, + opts *UploadCodexOptions, +) (*api.UploadCodexResponse, *api.CodexParseFailedError, error) { config, err := GetOrInitCodexConfig(opts.Dir) if err != nil { return nil, nil, err @@ -49,13 +52,16 @@ func UploadCodex(client *api.Client, opts *UploadCodexOptions) (*api.UploadCodex config.Upload.CodexId = res.CodexId if err := config.Save(); err != nil { - return nil, nil, errors.Wrap(err, "codex upload succeeded, but failed to save codex config file") + return nil, nil, errors.Wrap( + err, + "codex upload succeeded, but failed to save codex config file", + ) } return res, nil, nil } -const maxFiles = 20 +const maxFiles = 100 // Get all the files associated with the codex. // Recursively walks the filesystem starting at `dir`.