forked from grafana/grafana-api-golang-client
/
error.go
47 lines (40 loc) · 776 Bytes
/
error.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
package gapi
import (
"errors"
"fmt"
)
type GApiError struct {
statusCode int
message string
}
// Error implements the error interface.
func (e *GApiError) Error() string {
return fmt.Sprintf("status: %d, body: %v", e.statusCode, e.message)
}
// IsNotFound returns a boolean indicating whether the error is a not found error.
func IsNotFound(err error) bool {
if err == nil {
return false
}
var e *GApiError
if errors.As(err, &e) && e.statusCode == 404 {
return true
}
return false
}
func GetGApiError(err error) *GApiError {
if err == nil {
return nil
}
var e *GApiError
if errors.As(err, &e) {
return e
}
return nil
}
func (e *GApiError) StatusCode() int {
return e.statusCode
}
func (e *GApiError) Message() string {
return e.message
}