-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
53 lines (44 loc) · 1.4 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
package oas
import (
"encoding/json"
"fmt"
"github.com/tjbrockmeyer/oasm"
"github.com/xeipuuv/gojsonschema"
"strconv"
"strings"
)
func newMalformedJSONError(err error) malformedJSONError {
return malformedJSONError(fmt.Sprint("request contains malformed JSON: ", err.Error()))
}
type malformedJSONError string
func (err malformedJSONError) Error() string {
return string(err)
}
func newJSONValidationError(result *gojsonschema.Result) jsonValidationError {
errorList := make([]string, 0, len(result.Errors()))
for _, e := range result.Errors() {
errorList = append(errorList, fmt.Sprintf("At %s: %s", e.Context().String(), e.Description()))
}
return jsonValidationError{
Type: "JSONValidationError",
Errors: errorList,
}
}
func newParameterTypeError(param oasm.Parameter, expectedType, found string) jsonValidationError {
return jsonValidationError{
Type: "ParameterTypeError",
Errors: []string{
fmt.Sprintf("%s.%s: expected (%s) to be convertible to type %s", param.In, param.Name, found, expectedType),
},
}
}
type jsonValidationError struct {
Type string `json:"type"`
Errors []string `json:"errors"`
}
func (err jsonValidationError) Error() string {
return "JSONValidationError:\n\t" + strings.Join(err.Errors, "\n\t")
}
func errorToJSON(err error) json.RawMessage {
return []byte(fmt.Sprintf(`{"message":"Internal Server Error","details":%s}`, strconv.Quote(err.Error())))
}