-
Notifications
You must be signed in to change notification settings - Fork 9
/
errors.go
146 lines (119 loc) · 4.33 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
144
145
146
// Copyright 2020 Red Hat, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package types
// Documentation in literate-programming-style is available at:
// https://redhatinsights.github.io/insights-operator-utils/packages/types/errors.html
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/rs/zerolog/log"
"github.com/RedHatInsights/insights-operator-utils/responses"
)
// responseDataError is used as the error message when the responses functions return an error
const responseDataError = "Unexpected error during response data encoding"
// RouterMissingParamError missing parameter in request
type RouterMissingParamError struct {
ParamName string
}
func (e *RouterMissingParamError) Error() string {
return fmt.Sprintf("Missing required param from request: %v", e.ParamName)
}
// RouterParsingError parsing error, for example string when we expected integer
type RouterParsingError struct {
ParamName string
ParamValue interface{}
ErrString string
}
func (e *RouterParsingError) Error() string {
return fmt.Sprintf(
"Error during parsing param '%v' with value '%v'. Error: '%v'",
e.ParamName, e.ParamValue, e.ErrString,
)
}
// NoContentError means the client request is correct but the server doesn't
// have any data to return. For example, if a feature is disabled for a given
// cluster.
type NoContentError struct {
ErrString string
}
func (e *NoContentError) Error() string {
return e.ErrString
}
// UnauthorizedError means server can't authorize you, for example the token is missing or malformed
type UnauthorizedError struct {
ErrString string
}
func (e *UnauthorizedError) Error() string {
return e.ErrString
}
// ForbiddenError means you don't have permission to do a particular action,
// for example your account belongs to a different organization
type ForbiddenError struct {
ErrString string
}
func (e *ForbiddenError) Error() string {
return e.ErrString
}
// NoBodyError error meaning that client didn't provide body when it's required
type NoBodyError struct{}
func (*NoBodyError) Error() string {
return "client didn't provide request body"
}
// ValidationError validation error, for example when string is longer then expected
type ValidationError struct {
ParamName string
ParamValue interface{}
ErrString string
}
func (e *ValidationError) Error() string {
return fmt.Sprintf(
"Error during validating param '%v' with value '%v'. Error: '%v'",
e.ParamName, e.ParamValue, e.ErrString,
)
}
// HandleServerError handles separate server errors and sends appropriate responses
func HandleServerError(writer http.ResponseWriter, err error) {
log.Error().Err(err).Msg("handleServerError()")
var respErr error
switch err := err.(type) {
case *RouterMissingParamError, *RouterParsingError, *json.SyntaxError, *NoBodyError, *ValidationError:
respErr = responses.SendBadRequest(writer, err.Error())
case *json.UnmarshalTypeError:
respErr = responses.SendBadRequest(writer, "bad type in json data")
case *ItemNotFoundError:
respErr = responses.SendNotFound(writer, err.Error())
case *UnauthorizedError:
respErr = responses.SendUnauthorized(writer, err.Error())
case *ForbiddenError:
respErr = responses.SendForbidden(writer, err.Error())
default:
respErr = responses.SendInternalServerError(writer, "Internal Server Error")
}
if respErr != nil {
log.Error().Err(respErr).Msg(responseDataError)
}
}
// ErrOldReport is an error returned if a more recent already
// exists on the storage while attempting to write a report for a cluster.
var ErrOldReport = errors.New("More recent report already exists in storage")
// ItemNotFoundError shows that item with id ItemID wasn't found in the storage
type ItemNotFoundError struct {
ItemID interface{}
}
// Error returns error string
func (e *ItemNotFoundError) Error() string {
return fmt.Sprintf("Item with ID %+v was not found in the storage", e.ItemID)
}