You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

48 lines
1.2 KiB

  1. // Copyright 2014 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // This file provides error functions for common API failure modes.
  15. package datastore
  16. import (
  17. "fmt"
  18. )
  19. // MultiError is returned by batch operations when there are errors with
  20. // particular elements. Errors will be in a one-to-one correspondence with
  21. // the input elements; successful elements will have a nil entry.
  22. type MultiError []error
  23. func (m MultiError) Error() string {
  24. s, n := "", 0
  25. for _, e := range m {
  26. if e != nil {
  27. if n == 0 {
  28. s = e.Error()
  29. }
  30. n++
  31. }
  32. }
  33. switch n {
  34. case 0:
  35. return "(0 errors)"
  36. case 1:
  37. return s
  38. case 2:
  39. return s + " (and 1 other error)"
  40. }
  41. return fmt.Sprintf("%s (and %d other errors)", s, n-1)
  42. }