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 15// This file provides error functions for common API failure modes. 16 17package datastore 18 19import ( 20 "fmt" 21) 22 23// MultiError is returned by batch operations when there are errors with 24// particular elements. Errors will be in a one-to-one correspondence with 25// the input elements; successful elements will have a nil entry. 26type MultiError []error 27 28func (m MultiError) Error() string { 29 s, n := "", 0 30 for _, e := range m { 31 if e != nil { 32 if n == 0 { 33 s = e.Error() 34 } 35 n++ 36 } 37 } 38 switch n { 39 case 0: 40 return "(0 errors)" 41 case 1: 42 return s 43 case 2: 44 return s + " (and 1 other error)" 45 } 46 return fmt.Sprintf("%s (and %d other errors)", s, n-1) 47} 48