-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patherror.go
53 lines (44 loc) · 1.47 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
package http
import (
"fmt"
"net/http"
httpinternal "github.com/lunarway/release-manager/internal/http"
"github.com/lunarway/release-manager/internal/try"
"github.com/pkg/errors"
"go.uber.org/multierr"
)
func unknownError(w http.ResponseWriter) {
httpinternal.Error(w, "unknown error", http.StatusInternalServerError)
}
func invalidBodyError(w http.ResponseWriter) {
httpinternal.Error(w, "invalid body", http.StatusBadRequest)
}
func cancelled(w http.ResponseWriter) {
httpinternal.Error(w, "request cancelled", http.StatusBadRequest)
}
func requiredFieldError(w http.ResponseWriter, field string) {
httpinternal.Error(w, fmt.Sprintf("field %s required but was empty", field), http.StatusBadRequest)
}
func requiredQueryError(w http.ResponseWriter, field string) {
httpinternal.Error(w, fmt.Sprintf("query param %s required but was empty", field), http.StatusBadRequest)
}
func notFound(w http.ResponseWriter) {
httpinternal.Error(w, "not found", http.StatusNotFound)
}
// errorCause unwraps err from pkg/errors messages and if err contains a
// multierr, it will return the last err, again unwrapped if wrapped.
func errorCause(err error) error {
// get cause before and after multierr unwrap to handle wrapped multierrs and
// multierrs with wrapped errors
errs := multierr.Errors(errors.Cause(err))
if len(errs) == 0 {
return nil
}
for i := len(errs) - 1; i >= 0; i-- {
err := errs[i]
if err != try.ErrTooManyRetries {
return errors.Cause(err)
}
}
return err
}