go.mercari.io/datastore@v1.8.2/errors.go (about) 1 // Copyright 2014 Google Inc. All Rights Reserved. 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 17 package datastore 18 19 import ( 20 "errors" 21 "fmt" 22 "reflect" 23 ) 24 25 var ( 26 // ErrInvalidEntityType is returned when functions like Get or Next are 27 // passed a dst or src argument of invalid type. 28 ErrInvalidEntityType = errors.New("datastore: invalid entity type") 29 // ErrInvalidKey is returned when an invalid key is presented. 30 ErrInvalidKey = errors.New("datastore: invalid key") 31 // ErrNoSuchEntity is returned when no entity was found for a given key. 32 ErrNoSuchEntity = errors.New("datastore: no such entity") 33 ) 34 35 // ErrFieldMismatch is returned when a field is to be loaded into a different 36 // type than the one it was stored from, or when a field is missing or 37 // unexported in the destination struct. 38 // StructType is the type of the struct pointed to by the destination argument 39 // passed to Get or to Iterator.Next. 40 type ErrFieldMismatch struct { 41 StructType reflect.Type 42 FieldName string 43 Reason string 44 } 45 46 func (e *ErrFieldMismatch) Error() string { 47 return fmt.Sprintf("datastore: cannot load field %q into a %q: %s", 48 e.FieldName, e.StructType, e.Reason) 49 } 50 51 // ErrConcurrentTransaction is returned when a transaction is rolled back due 52 // to a conflict with a concurrent transaction. 53 var ErrConcurrentTransaction = errors.New("datastore: concurrent transaction") 54 55 // MultiError is returned by batch operations when there are errors with 56 // particular elements. Errors will be in a one-to-one correspondence with 57 // the input elements; successful elements will have a nil entry. 58 type MultiError []error 59 60 func (m MultiError) Error() string { 61 s, n := "", 0 62 for _, e := range m { 63 if e != nil { 64 if n == 0 { 65 s = e.Error() 66 } 67 n++ 68 } 69 } 70 switch n { 71 case 0: 72 return "(0 errors)" 73 case 1: 74 return s 75 case 2: 76 return s + " (and 1 other error)" 77 } 78 return fmt.Sprintf("%s (and %d other errors)", s, n-1) 79 }