This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
forked from PaloAltoNetworks/prisma-cloud-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
68 lines (55 loc) · 1.37 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package prismacloud
import (
"errors"
"fmt"
"strings"
)
var InvalidCredentialsError = errors.New("invalid credentials")
var ObjectNotFoundError = errors.New("object not found")
var AlreadyExistsError = errors.New("object already exists")
type PrismaCloudErrorList struct {
Errors []PrismaCloudError
Method string
StatusCode int
Path string
}
func (e PrismaCloudErrorList) Error() string {
var buf strings.Builder
buf.Grow(100)
fmt.Fprintf(&buf, "%d/%s ", e.StatusCode, e.Path)
for i := range e.Errors {
if i != 0 {
buf.WriteString(" ")
}
buf.WriteString(e.Errors[i].Error())
}
return buf.String()
}
func (e PrismaCloudErrorList) GenericError() error {
for i := range e.Errors {
if e.Errors[i].ObjectNotFound() {
return ObjectNotFoundError
} else if e.Errors[i].AlreadyExists() {
return AlreadyExistsError
}
}
return nil
}
type PrismaCloudError struct {
Message string `json:"i18nKey"`
Severity string `json:"severity"`
Subject string `json:"subject"`
}
func (e PrismaCloudError) ObjectNotFound() bool {
switch e.Message {
case "invalid_id", "not_found":
return true
}
return false
}
func (e PrismaCloudError) AlreadyExists() bool {
return strings.HasSuffix(e.Message, "_already_exists")
}
func (e PrismaCloudError) Error() string {
return fmt.Sprintf("Error(msg:%s severity:%s subject:%v)", e.Message, e.Severity, e.Subject)
}