...
1
9
10 package openapi
11
12 import (
13 "errors"
14 "fmt"
15 "net/http"
16 )
17
18 var (
19
20 ErrTypeAssertionError = errors.New("unable to assert type")
21 )
22
23
24 type ParsingError struct {
25 Err error
26 }
27
28 func (e *ParsingError) Unwrap() error {
29 return e.Err
30 }
31
32 func (e *ParsingError) Error() string {
33 return e.Err.Error()
34 }
35
36
37 type RequiredError struct {
38 Field string
39 }
40
41 func (e *RequiredError) Error() string {
42 return fmt.Sprintf("required field '%s' is zero value.", e.Field)
43 }
44
45
46
47 type ErrorHandler func(w http.ResponseWriter, r *http.Request, err error, result *ImplResponse)
48
49
50
51 func DefaultErrorHandler(w http.ResponseWriter, r *http.Request, err error, result *ImplResponse) {
52 if _, ok := err.(*ParsingError); ok {
53
54 EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusBadRequest), w)
55 } else if _, ok := err.(*RequiredError); ok {
56
57 EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusUnprocessableEntity), w)
58 } else {
59
60 EncodeJSONResponse(err.Error(), &result.Code, w)
61 }
62 }
63
View as plain text