Skip to content

Commit

Permalink
Improve error reporting/logging
Browse files Browse the repository at this point in the history
  • Loading branch information
twavv committed Aug 10, 2021
1 parent eb5c53a commit 538761d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.so
*.dylib
main
pbauthor
dist/

# Test binary, built with `go test -c`
Expand Down
14 changes: 12 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
27 changes: 20 additions & 7 deletions internal/api/codex_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

type UploadCodexRequest struct {
Expand All @@ -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"`
Expand All @@ -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 != "" {
Expand All @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions internal/codex/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`.
Expand Down

0 comments on commit 538761d

Please sign in to comment.