errors.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2011 Google Inc. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. // This file provides error functions for common API failure modes.
  5. package appengine
  6. import (
  7. "fmt"
  8. "google.golang.org/appengine/internal"
  9. )
  10. // IsOverQuota reports whether err represents an API call failure
  11. // due to insufficient available quota.
  12. func IsOverQuota(err error) bool {
  13. callErr, ok := err.(*internal.CallError)
  14. return ok && callErr.Code == 4
  15. }
  16. // MultiError is returned by batch operations when there are errors with
  17. // particular elements. Errors will be in a one-to-one correspondence with
  18. // the input elements; successful elements will have a nil entry.
  19. type MultiError []error
  20. func (m MultiError) Error() string {
  21. s, n := "", 0
  22. for _, e := range m {
  23. if e != nil {
  24. if n == 0 {
  25. s = e.Error()
  26. }
  27. n++
  28. }
  29. }
  30. switch n {
  31. case 0:
  32. return "(0 errors)"
  33. case 1:
  34. return s
  35. case 2:
  36. return s + " (and 1 other error)"
  37. }
  38. return fmt.Sprintf("%s (and %d other errors)", s, n-1)
  39. }