forked from coinbase/waas-client-library-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
41 lines (33 loc) · 1.04 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
package clients
import (
"net/http"
"github.com/googleapis/gax-go/v2/apierror"
"google.golang.org/api/googleapi"
)
// HTTPCode attempts to return the HTTP status code for the given error.
// If it cannot be determined, it returns a 500 (Internal Server Error).
func HTTPCode(err error) int {
if err == nil {
return http.StatusOK
}
if googleapiErr, ok := err.(*googleapi.Error); ok {
return googleapiErr.Code
}
return http.StatusInternalServerError
}
// UnwrapError attempts to unwrap the passed error and return a googleapi error.
// It is expected that the passed error is of type apierror.APIError.
// If it is not, it returns the passed error unchanged.
func UnwrapError(err error) error {
if err == nil {
return nil
}
if apiErr, ok := err.(*apierror.APIError); ok { //nolint:errorlint
// Unwrap the the `apierror` to extract the HTTP status code and original error message.
unwrappedErr := apiErr.Unwrap()
if googleapiErr, ok := unwrappedErr.(*googleapi.Error); ok { //nolint:errorlint
return googleapiErr
}
}
return err
}