@@ -29,15 +29,77 @@ import (
29
29
30
30
const iidEndpoint = "https://console.firebase.google.com/v1"
31
31
32
- var errorCodes = map [int ]string {
33
- http .StatusBadRequest : "malformed instance id argument" ,
34
- http .StatusUnauthorized : "request not authorized" ,
35
- http .StatusForbidden : "project does not match instance ID or the client does not have sufficient privileges" ,
36
- http .StatusNotFound : "failed to find the instance id" ,
37
- http .StatusConflict : "already deleted" ,
38
- http .StatusTooManyRequests : "request throttled out by the backend server" ,
39
- http .StatusInternalServerError : "internal server error" ,
40
- http .StatusServiceUnavailable : "backend servers are over capacity" ,
32
+ const (
33
+ invalidArgument = "invalid-argument"
34
+ unauthorized = "unauthorized"
35
+ insufficientPermission = "insufficient-permission"
36
+ notFound = "instance-id-not-found"
37
+ alreadyDeleted = "instance-id-already-deleted"
38
+ tooManyRequests = "too-many-requests"
39
+ internalError = "internal-error"
40
+ serverUnavailable = "server-unavailable"
41
+ unknown = "unknown-error"
42
+ )
43
+
44
+ var errorCodes = map [int ]struct {
45
+ code , message string
46
+ }{
47
+ http .StatusBadRequest : {invalidArgument , "malformed instance id argument" },
48
+ http .StatusUnauthorized : {insufficientPermission , "request not authorized" },
49
+ http .StatusForbidden : {
50
+ insufficientPermission ,
51
+ "project does not match instance ID or the client does not have sufficient privileges" ,
52
+ },
53
+ http .StatusNotFound : {notFound , "failed to find the instance id" },
54
+ http .StatusConflict : {alreadyDeleted , "already deleted" },
55
+ http .StatusTooManyRequests : {tooManyRequests , "request throttled out by the backend server" },
56
+ http .StatusInternalServerError : {internalError , "internal server error" },
57
+ http .StatusServiceUnavailable : {serverUnavailable , "backend servers are over capacity" },
58
+ }
59
+
60
+ // IsInvalidArgument checks if the given error was due to an invalid instance ID argument.
61
+ func IsInvalidArgument (err error ) bool {
62
+ return internal .HasErrorCode (err , invalidArgument )
63
+ }
64
+
65
+ // IsInsufficientPermission checks if the given error was due to the request not having the
66
+ // required authorization. This could be due to the client not having the required permission
67
+ // or the specified instance ID not matching the target Firebase project.
68
+ func IsInsufficientPermission (err error ) bool {
69
+ return internal .HasErrorCode (err , insufficientPermission )
70
+ }
71
+
72
+ // IsNotFound checks if the given error was due to a non existing instance ID.
73
+ func IsNotFound (err error ) bool {
74
+ return internal .HasErrorCode (err , notFound )
75
+ }
76
+
77
+ // IsAlreadyDeleted checks if the given error was due to the instance ID being already deleted from
78
+ // the project.
79
+ func IsAlreadyDeleted (err error ) bool {
80
+ return internal .HasErrorCode (err , alreadyDeleted )
81
+ }
82
+
83
+ // IsTooManyRequests checks if the given error was due to the client sending too many requests
84
+ // causing a server quota to exceed.
85
+ func IsTooManyRequests (err error ) bool {
86
+ return internal .HasErrorCode (err , tooManyRequests )
87
+ }
88
+
89
+ // IsInternal checks if the given error was due to an internal server error.
90
+ func IsInternal (err error ) bool {
91
+ return internal .HasErrorCode (err , internalError )
92
+ }
93
+
94
+ // IsServerUnavailable checks if the given error was due to the backend server being temporarily
95
+ // unavailable.
96
+ func IsServerUnavailable (err error ) bool {
97
+ return internal .HasErrorCode (err , serverUnavailable )
98
+ }
99
+
100
+ // IsUnknown checks if the given error was due to unknown error returned by the backend server.
101
+ func IsUnknown (err error ) bool {
102
+ return internal .HasErrorCode (err , unknown )
41
103
}
42
104
43
105
// Client is the interface for the Firebase Instance ID service.
@@ -84,8 +146,11 @@ func (c *Client) DeleteInstanceID(ctx context.Context, iid string) error {
84
146
return err
85
147
}
86
148
87
- if msg , ok := errorCodes [resp .Status ]; ok {
88
- return fmt .Errorf ("instance id %q: %s" , iid , msg )
149
+ if info , ok := errorCodes [resp .Status ]; ok {
150
+ return internal .Errorf (info .code , "instance id %q: %s" , iid , info .message )
151
+ }
152
+ if err := resp .CheckStatus (http .StatusOK ); err != nil {
153
+ return internal .Error (unknown , err .Error ())
89
154
}
90
- return resp . CheckStatus ( http . StatusOK )
155
+ return nil
91
156
}
0 commit comments