This repository has been archived by the owner on Nov 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
errors.go
143 lines (118 loc) · 3.52 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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
///////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
///////////////////////////////////////////////////////////////////////
package client
import (
"fmt"
"github.com/vmware/dispatch/pkg/api/v1"
)
// Error is an interface implemented by all errors declared here.
type Error interface {
Error() string
Message() string
Code() int
}
type baseError struct {
code int
message string
}
func (b baseError) Error() string {
return fmt.Sprintf("[Code: %d] %s", b.code, b.message)
}
func (b baseError) Message() string {
return b.message
}
func (b baseError) Code() int {
return b.code
}
// ErrorServerUnknownError represents unknown server error
type ErrorServerUnknownError struct {
baseError
}
// NewErrorServerUnknownError creates new instance of ErrorServerUnknownError based on Error Model
func NewErrorServerUnknownError(apiError *v1.Error) *ErrorServerUnknownError {
return &ErrorServerUnknownError{
baseError: baseErrFromModel(apiError),
}
}
// ErrorBadRequest represents client-side request error
type ErrorBadRequest struct {
baseError
}
// NewErrorBadRequest creates new instance of ErrorBadRequest based on Error Model
func NewErrorBadRequest(apiError *v1.Error) *ErrorBadRequest {
return &ErrorBadRequest{
baseError: baseErrFromModel(apiError),
}
}
// ErrorAlreadyExists represents error when resource already exists on the server
type ErrorAlreadyExists struct {
baseError
}
// NewErrorAlreadyExists creates new instance of ErrorAlreadyExists based on Error Model
func NewErrorAlreadyExists(apiError *v1.Error) *ErrorAlreadyExists {
return &ErrorAlreadyExists{
baseError: baseErrFromModel(apiError),
}
}
// ErrorNotFound represents error of missing resource
type ErrorNotFound struct {
baseError
}
// NewErrorNotFound creates new instance of ErrorNotFound based on Error Model
func NewErrorNotFound(apiError *v1.Error) *ErrorNotFound {
return &ErrorNotFound{
baseError: baseErrFromModel(apiError),
}
}
// ErrorForbidden represents authz error
type ErrorForbidden struct {
baseError
}
// NewErrorForbidden creates new instance of ErrorForbidden based on Error Model
func NewErrorForbidden(apiError *v1.Error) *ErrorForbidden {
return &ErrorForbidden{
baseError: baseErrFromModel(apiError),
}
}
// ErrorUnauthorized represents authn error
type ErrorUnauthorized struct {
baseError
}
// NewErrorUnauthorized creates new instance of ErrorUnauthorized based on Error Model
func NewErrorUnauthorized(apiError *v1.Error) *ErrorUnauthorized {
return &ErrorUnauthorized{
baseError: baseErrFromModel(apiError),
}
}
// ErrorInvalidInput represents error of request input being invalid
type ErrorInvalidInput struct {
baseError
}
// NewErrorInvalidInput creates new instance of ErrorInvalidInput based on Error Model
func NewErrorInvalidInput(apiError *v1.Error) *ErrorInvalidInput {
return &ErrorInvalidInput{
baseError: baseErrFromModel(apiError),
}
}
// ErrorFunctionError represents error that happened when executing the function
type ErrorFunctionError struct {
baseError
}
// NewErrorFunctionError creates new instance of ErrorFunctionError based on Error Model
func NewErrorFunctionError(apiError *v1.Error) *ErrorFunctionError {
return &ErrorFunctionError{
baseError: baseErrFromModel(apiError),
}
}
func baseErrFromModel(apiError *v1.Error) baseError {
message := ""
if apiError.Message != nil {
message = *apiError.Message
}
return baseError{
code: int(apiError.Code),
message: message,
}
}