-
Notifications
You must be signed in to change notification settings - Fork 1
/
error.go
41 lines (33 loc) · 1.11 KB
/
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
// Package aiven provides a client for interacting with the Aiven API.
package aiven
import (
"errors"
"fmt"
"net/http"
"strings"
)
// Error represents an Aiven API Error.
type Error struct {
Message string `json:"message"`
MoreInfo string `json:"more_info"`
Status int `json:"status"`
}
// Error concatenates the Status, Message and MoreInfo values.
func (e Error) Error() string {
return fmt.Sprintf("%d: %s - %s", e.Status, e.Message, e.MoreInfo)
}
// IsNotFound returns true if the specified error has status 404
func IsNotFound(err error) bool {
var e Error
return errors.As(err, &e) && e.Status == http.StatusNotFound
}
// IsAlreadyExists returns true if the error message and error code that indicates that entity already exists
func IsAlreadyExists(err error) bool {
var e Error
return errors.As(err, &e) && strings.Contains(e.Message, "already exists") && e.Status == http.StatusConflict
}
// IsNilOrNotFound returns true for nil and 404 error.
// This check is quite often used for resource deletion when 404 is not an issue.
func IsNilOrNotFound(err error) bool {
return err == nil || IsNotFound(err)
}