-
Notifications
You must be signed in to change notification settings - Fork 38
/
errors.go
180 lines (153 loc) · 5.86 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2014 DoAT. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED “AS IS” WITHOUT ANY WARRANTIES WHATSOEVER.
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF NON INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL DoAT OR CONTRIBUTORS
// BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of
// the authors and should not be interpreted as representing official policies,
// either expressed or implied, of DoAT.
package raml
// This file contains all code related to YAML and RAML errors.
import (
"fmt"
"strings"
yaml "github.com/advance512/yaml"
)
// A RamlError is returned by the ParseFile function when RAML or YAML problems
// are encountered when parsing the RAML document.
type RamlError struct {
Errors []string
}
func (e *RamlError) Error() string {
return fmt.Sprintf("Error parsing RAML:\n %s\n",
strings.Join(e.Errors, "\n "))
}
// Populate the RAML error value with converted YAML error strings (with
// additional context)
func populateRAMLError(ramlError *RamlError,
yamlErrors *yaml.TypeError) {
// Go over the errors
for _, currErr := range yamlErrors.Errors {
// Create the RAML errors
ramlError.Errors =
append(ramlError.Errors, convertYAMLError(currErr))
}
}
// Convert a YAML error string into RAML error string, with more context
func convertYAMLError(yamlError string) string {
if strings.Contains(yamlError, "cannot unmarshal") {
yamlErrorParts := strings.Split(yamlError, " ")
if len(yamlErrorParts) >= 7 {
fmt.Println(yamlError)
var ok bool
var source string
var target string
var targetName string
line := yamlErrorParts[1]
line = line[:len(line)-1]
// TODO: support more complex types:
// map[string]raml.NamedParameter -->
// detect map, format to:
// "mapping of %s to %s", ramlTypeNames["string"], ramlTypeNames["raml.NamedParameter"]
// if "string" is not found, use the key, i.e. "string" in this case.
// so the output would be:
// mapping of string to named parameter
// TODO: instead of having string in the key of some mappings,
// perhaps use a type alias:
// type Name string
// map[Name]NamedParameter
// would output:
// mapping of name string to named parameter
if source, ok = yamlTypeToName[yamlErrorParts[4]]; !ok {
source = yamlErrorParts[4]
}
fmt.Println("source: ", source)
if source == "string" {
source = fmt.Sprintf("string (got %s)", yamlErrorParts[5])
target = yamlErrorParts[7]
} else {
target = yamlErrorParts[6]
}
if targetName, ok = ramlTypeNames[target]; !ok {
targetName = target
}
target, _ = ramlTypes[target]
return fmt.Sprintf("line %s: %s cannot be of "+
"type %s, must be %s", line, targetName, source, target)
}
}
// Otherwise
return fmt.Sprintf("YAML error, %s", yamlError)
}
var yamlTypeToName map[string]string = map[string]string{
"!!seq": "sequence",
"!!map": "mapping",
"!!int": "integer",
"!!str": "string",
"!!null": "null",
"!!bool": "boolean",
"!!float": "float",
"!!timestamp": "timestamp",
"!!binary": "binary",
"!!merge": "merge",
}
var ramlTypeNames map[string]string = map[string]string{
"string": "string value",
"int": "numeric value",
"raml.NamedParameter": "named parameter",
"raml.HTTPCode": "HTTP code",
"raml.HTTPHeader": "HTTP header",
"raml.Header": "header",
"raml.Documentation": "documentation",
"raml.Body": "body",
"raml.Response": "response",
"raml.DefinitionParameters": "definition parameters",
"raml.DefinitionChoice": "definition choice",
"raml.Trait": "trait",
"raml.ResourceTypeMethod": "resource type method",
"raml.ResourceType": "resource type",
"raml.SecuritySchemeMethod": "security scheme method",
"raml.SecurityScheme": "security scheme",
"raml.Method": "method",
"raml.Resource": "resource",
"raml.APIDefinition": "API definition",
}
var ramlTypes map[string]string = map[string]string{
"string": "string",
"int": "integer",
"raml.NamedParameter": "mapping",
"raml.HTTPCode": "integer",
"raml.HTTPHeader": "string",
"raml.Header": "mapping",
"raml.Documentation": "mapping",
"raml.Body": "mapping",
"raml.Response": "mapping",
"raml.DefinitionParameters": "mapping",
"raml.DefinitionChoice": "string or mapping",
"raml.Trait": "mapping",
"raml.ResourceTypeMethod": "mapping",
"raml.ResourceType": "mapping",
"raml.SecuritySchemeMethod": "mapping",
"raml.SecurityScheme": "mapping",
"raml.Method": "mapping",
"raml.Resource": "mapping",
"raml.APIDefinition": "mapping",
}