Skip to content

Commit c93eea5

Browse files
hinokaCommit Bot
authored andcommitted
[datastore] Add IsErrNoSuchEntity in datastore/errors.go
This was an unsurprisingly common thing to do. Also datastore already imports errors, so this was the most logical place to put this. Change-Id: I11f5ef777733de85cbd2a9616beab6500a47bf06 Reviewed-on: https://chromium-review.googlesource.com/1043331 Commit-Queue: Ryan Tseng <[email protected]> Reviewed-by: Andrii Shyshkalov <[email protected]>
1 parent 8511a0d commit c93eea5

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

service/datastore/errors.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ func MakeErrInvalidKey(reason string, args ...interface{}) *errors.Annotator {
4848
// error.
4949
func IsErrInvalidKey(err error) bool { return errors.Unwrap(err) == datastore.ErrInvalidKey }
5050

51+
// IsErrNoSuchEntity tests if an error is ErrNoSuchEntity,
52+
// or is a MultiError that contains ErrNoSuchEntity and no other errors.
53+
func IsErrNoSuchEntity(err error) (found bool) {
54+
errors.Walk(err, func(err error) bool {
55+
found = err == ErrNoSuchEntity
56+
// If we found an ErrNoSuchEntity, continue walking.
57+
// If we found a different type of error, signal Walk to stop walking by returning false.
58+
// Walk does not walk nil errors.
59+
return found
60+
})
61+
return
62+
}
63+
5164
// ErrFieldMismatch is returned when a field is to be loaded into a different
5265
// type than the one it was stored from, or when a field is missing or
5366
// unexported in the destination struct.

0 commit comments

Comments
 (0)