| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package libyaml |
| 8 |
|
| 9 |
import ( |
| 10 |
"errors" |
| 11 |
"fmt" |
| 12 |
"strings" |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
type Stage string |
| 18 |
|
| 19 |
const ( |
| 20 |
|
| 21 |
ReaderStage Stage = "reader" |
| 22 |
ScannerStage Stage = "scanner" |
| 23 |
ParserStage Stage = "parser" |
| 24 |
ComposerStage Stage = "composer" |
| 25 |
ResolverStage Stage = "resolver" |
| 26 |
ConstructorStage Stage = "constructor" |
| 27 |
|
| 28 |
|
| 29 |
RepresenterStage Stage = "representer" |
| 30 |
SerializerStage Stage = "serializer" |
| 31 |
EmitterStage Stage = "emitter" |
| 32 |
WriterStage Stage = "writer" |
| 33 |
) |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
type LoadError struct { |
| 40 |
Stage Stage |
| 41 |
Message string |
| 42 |
|
| 43 |
|
| 44 |
Mark Mark |
| 45 |
ContextMark Mark |
| 46 |
ContextMsg string |
| 47 |
|
| 48 |
|
| 49 |
err error |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
func (e *LoadError) Error() string { |
| 56 |
if len(e.ContextMsg) > 0 { |
| 57 |
return fmt.Sprintf("go-yaml load error in %s (%s) at %s: %s", |
| 58 |
e.Stage, e.ContextMsg, e.ContextMark.rangeString(e.Mark), e.Message) |
| 59 |
} |
| 60 |
return fmt.Sprintf("go-yaml load error in %s at %s: %s", |
| 61 |
e.Stage, e.Mark.shortString(), e.Message) |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
func (e *LoadError) simpleError() string { |
| 68 |
var builder strings.Builder |
| 69 |
if len(e.ContextMsg) > 0 { |
| 70 |
fmt.Fprintf(&builder, "%s at %s: ", e.ContextMsg, e.ContextMark) |
| 71 |
} |
| 72 |
if len(e.ContextMsg) == 0 || e.ContextMark != e.Mark { |
| 73 |
if e.Mark.Line > 0 { |
| 74 |
fmt.Fprintf(&builder, "line %d: ", e.Mark.Line) |
| 75 |
} else { |
| 76 |
builder.WriteString("<unknown position>: ") |
| 77 |
} |
| 78 |
} |
| 79 |
builder.WriteString(e.Message) |
| 80 |
return builder.String() |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
func (e *LoadError) Unwrap() error { |
| 85 |
return e.err |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
func NewLoadError(stage Stage, message string, mark Mark, cause error) *LoadError { |
| 91 |
return &LoadError{ |
| 92 |
Stage: stage, |
| 93 |
Message: message, |
| 94 |
Mark: mark, |
| 95 |
err: cause, |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
type DumpError struct { |
| 104 |
Stage Stage |
| 105 |
Message string |
| 106 |
|
| 107 |
|
| 108 |
err error |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
func (e *DumpError) Error() string { |
| 114 |
return fmt.Sprintf("go-yaml dump error in %s: %s", e.Stage, e.Message) |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
func (e *DumpError) Unwrap() error { |
| 119 |
return e.err |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
func NewDumpError(stage Stage, message string, cause error) *DumpError { |
| 125 |
return &DumpError{Stage: stage, Message: message, err: cause} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
func failDump(stage Stage, err error) { |
| 134 |
if de, ok := err.(*DumpError); ok { |
| 135 |
panic(&YAMLError{de}) |
| 136 |
} |
| 137 |
panic(&YAMLError{&DumpError{Stage: stage, Message: err.Error(), err: err}}) |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
func failDumpf(stage Stage, format string, args ...any) { |
| 142 |
panic(&YAMLError{&DumpError{Stage: stage, Message: fmt.Sprintf(format, args...)}}) |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
type EmitterError struct { |
| 147 |
Message string |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
func (e EmitterError) Error() string { |
| 152 |
return fmt.Sprintf("yaml: %s", e.Message) |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
type WriterError struct { |
| 157 |
Err error |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
func (e WriterError) Error() string { |
| 162 |
return fmt.Sprintf("yaml: %s", e.Err) |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
func (e WriterError) Unwrap() error { |
| 167 |
return e.Err |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
type LoadErrors struct { |
| 172 |
Errors []*LoadError |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
func (e *LoadErrors) Error() string { |
| 177 |
var b strings.Builder |
| 178 |
b.WriteString("yaml: construct errors: ") |
| 179 |
for i, err := range e.Errors { |
| 180 |
if i > 0 { |
| 181 |
b.WriteString("; ") |
| 182 |
} |
| 183 |
b.WriteString(err.simpleError()) |
| 184 |
} |
| 185 |
return b.String() |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
func (e *LoadErrors) As(target any) bool { |
| 192 |
switch t := target.(type) { |
| 193 |
case **LoadError: |
| 194 |
if len(e.Errors) == 0 { |
| 195 |
return false |
| 196 |
} |
| 197 |
*t = e.Errors[0] |
| 198 |
return true |
| 199 |
case **TypeError: |
| 200 |
var msgs []string |
| 201 |
for _, err := range e.Errors { |
| 202 |
msgs = append(msgs, err.simpleError()) |
| 203 |
} |
| 204 |
*t = &TypeError{Errors: msgs} |
| 205 |
return true |
| 206 |
} |
| 207 |
return false |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
func (e *LoadErrors) Is(target error) bool { |
| 214 |
for _, err := range e.Errors { |
| 215 |
if errors.Is(err, target) { |
| 216 |
return true |
| 217 |
} |
| 218 |
} |
| 219 |
return false |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
type TypeError struct { |
| 231 |
Errors []string |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
func (e *TypeError) Error() string { |
| 236 |
return fmt.Sprintf("yaml: unmarshal errors: %s", strings.Join(e.Errors, "; ")) |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
type YAMLError struct { |
| 241 |
Err error |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
func (e *YAMLError) Error() string { |
| 246 |
return e.Err.Error() |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
func handleErr(err *error) { |
| 252 |
if v := recover(); v != nil { |
| 253 |
if e, ok := v.(*YAMLError); ok { |
| 254 |
*err = e.Err |
| 255 |
} else { |
| 256 |
panic(v) |
| 257 |
} |
| 258 |
} |
| 259 |
} |