-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
67 lines (59 loc) · 1.88 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2011 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// This file provides error functions for common API failure modes.
package appengine
import (
"fmt"
"appengine_internal"
)
// IsOverQuota reports whether err represents an API call failure
// due to insufficient available quota.
func IsOverQuota(err error) bool {
callErr, ok := err.(*appengine_internal.CallError)
return ok && callErr.Code == 4 // 4 == APIResponse_OVER_QUOTA
}
// IsCapabilityDisabled reports whether err represents an API call failure
// due to the API being disabled. See the appengine/capability package for
// a way to detect this condition in advance.
func IsCapabilityDisabled(err error) bool {
// Note, we are using plain integers rather than the constants
// generated from the proto files to avoid dragging in
// possibly problematic dependencies into the Go toolchain.
switch e := err.(type) {
case *appengine_internal.CallError:
return e.Code == 6 // 6 == APIResponse_CAPABILITY_DISABLED
case *appengine_internal.APIError:
// Special-case mapping for memcache to ensure strict
// backward compatibility even for undocumented
// behaviors.
// 9 == MemcacheServiceError_UNAVAILABLE
return e.Code == 9 && e.Service == "memcache"
default:
return false
}
}
// MultiError is returned by batch operations when there are errors with
// particular elements. Errors will be in a one-to-one correspondence with
// the input elements; successful elements will have a nil entry.
type MultiError []error
func (m MultiError) Error() string {
s, n := "", 0
for _, e := range m {
if e != nil {
if n == 0 {
s = e.Error()
}
n++
}
}
switch n {
case 0:
return "(0 errors)"
case 1:
return s
case 2:
return s + " (and 1 other error)"
}
return fmt.Sprintf("%s (and %d other errors)", s, n-1)
}