forked from fullstorydev/emulators
/
errors.go
42 lines (35 loc) · 813 Bytes
/
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
package gcsemu
import (
"fmt"
"google.golang.org/api/googleapi"
)
func httpStatusCodeOf(err error) int {
if gapiErr, ok := err.(*googleapi.Error); ok {
return gapiErr.Code
}
if httpErr, ok := err.(*httpError); ok {
if httpErr.code != 0 {
return httpErr.code
}
return httpStatusCodeOf(httpErr.cause)
}
return 0
}
func fmtErrorfCode(httpCode int, f string, args ...interface{}) error {
return &httpError{
cause: fmt.Errorf(f, args...),
code: httpCode,
}
}
// httpError is a custom error type that decorates with an HTTP error code
type httpError struct {
cause error
code int
}
// Error returns a string describing the entire causal chain.
func (err *httpError) Error() string {
if err == nil {
return "<nil>"
}
return fmt.Sprintf("http error %d: %s", err.code, err.cause)
}