Skip to content

Commit

Permalink
Merge pull request #341 from mlbright/daily-quota-exceeded
Browse files Browse the repository at this point in the history
fix: exit if daily API quota is exceeded
  • Loading branch information
pacoorozco authored Dec 14, 2022
2 parents b546516 + 5f9e324 commit 6762fc0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (app App) Stop() error {
return err
}

app.Logger.Debug("All services has been shut down successfully")
app.Logger.Debug("All services have been shut down successfully")
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/app/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ func (app *App) AuthenticateFromWeb(ctx context.Context) (*http.Client, error) {
}

return oauth.Client(ctx, cfg, token)
}
}
15 changes: 14 additions & 1 deletion internal/cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package cmd
import (
"context"
"net/http"
"regexp"
"sort"
"time"

gphotos "github.com/gphotosuploader/google-photos-api-client-go/v2"
"github.com/gphotosuploader/google-photos-api-client-go/v2/uploader/resumable"
"github.com/schollz/progressbar/v3"
"github.com/spf13/cobra"
"google.golang.org/api/googleapi"

"github.com/gphotosuploader/gphotos-uploader-cli/internal/app"
"github.com/gphotosuploader/gphotos-uploader-cli/internal/cmd/flags"
Expand All @@ -20,6 +22,10 @@ import (
"github.com/gphotosuploader/gphotos-uploader-cli/internal/worker"
)

var (
requestQuotaErrorRe = regexp.MustCompile(`Quota exceeded for quota metric 'All requests' and limit 'All requests per day'`)
)

// PushCmd holds the required data for the push cmd
type PushCmd struct {
*flags.GlobalFlags
Expand Down Expand Up @@ -148,7 +154,14 @@ func (cmd *PushCmd) Run(cobraCmd *cobra.Command, args []string) error {
_ = bar.Add(1)

if r.Err != nil {
cli.Logger.Failf("Error processing %s", r.ID)
if googleApiErr, ok := r.Err.(*googleapi.Error); ok {
if requestQuotaErrorRe.MatchString(googleApiErr.Message) {
cli.Logger.Failf("returning 'quota exceeded' error")
return r.Err
}
} else {
cli.Logger.Failf("Error processing %s", r.ID)
}
} else {
uploadedItems++
cli.Logger.Debugf("Successfully processing %s", r.ID)
Expand Down

0 comments on commit 6762fc0

Please sign in to comment.