error.go raw

   1  // Copyright 2022 The Go Authors. All rights reserved.
   2  // Use of this source code is governed by a BSD-style
   3  // license that can be found in the LICENSE file.
   4  
   5  package google
   6  
   7  import (
   8  	"errors"
   9  
  10  	"golang.org/x/oauth2"
  11  )
  12  
  13  // AuthenticationError indicates there was an error in the authentication flow.
  14  //
  15  // Use (*AuthenticationError).Temporary to check if the error can be retried.
  16  type AuthenticationError struct {
  17  	err *oauth2.RetrieveError
  18  }
  19  
  20  func newAuthenticationError(err error) error {
  21  	re := &oauth2.RetrieveError{}
  22  	if !errors.As(err, &re) {
  23  		return err
  24  	}
  25  	return &AuthenticationError{
  26  		err: re,
  27  	}
  28  }
  29  
  30  // Temporary indicates that the network error has one of the following status codes and may be retried: 500, 503, 408, or 429.
  31  func (e *AuthenticationError) Temporary() bool {
  32  	if e.err.Response == nil {
  33  		return false
  34  	}
  35  	sc := e.err.Response.StatusCode
  36  	return sc == 500 || sc == 503 || sc == 408 || sc == 429
  37  }
  38  
  39  func (e *AuthenticationError) Error() string {
  40  	return e.err.Error()
  41  }
  42  
  43  func (e *AuthenticationError) Unwrap() error {
  44  	return e.err
  45  }
  46  
  47  type errWrappingTokenSource struct {
  48  	src oauth2.TokenSource
  49  }
  50  
  51  func newErrWrappingTokenSource(ts oauth2.TokenSource) oauth2.TokenSource {
  52  	return &errWrappingTokenSource{src: ts}
  53  }
  54  
  55  // Token returns the current token if it's still valid, else will
  56  // refresh the current token (using r.Context for HTTP client
  57  // information) and return the new one.
  58  func (s *errWrappingTokenSource) Token() (*oauth2.Token, error) {
  59  	t, err := s.src.Token()
  60  	if err != nil {
  61  		return nil, newAuthenticationError(err)
  62  	}
  63  	return t, nil
  64  }
  65