Repository navigation
Expand file tree
/
Copy patherrors.go
More file actions
126 lines (114 loc) · 3.98 KB
/
Copy patherrors.go
File metadata and controls
126 lines (114 loc) · 3.98 KB
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
package avro
import (
"errors"
"fmt"
"reflect"
"strings"
)
// SemanticError indicates a Go type is incompatible with an Avro schema type
// during encoding or decoding.
type SemanticError struct {
// GoType is the Go type involved, if applicable.
GoType reflect.Type
// AvroType is the Avro schema type (e.g. "int", "record", "boolean").
AvroType string
// Field is the dotted path to the record field (e.g. "address.zip"),
// if the error occurred within a record.
Field string
// Err is the underlying error.
Err error
}
func (e *SemanticError) Error() string {
gt := formatGoType(e.GoType)
var s string
switch {
case e.Field != "" && gt != "":
// Field is built from schema field names, which have no length cap
// (WithLaxNames admits any non-empty string), so we truncate it here
// rather than let a hostile name amplify into an equally large error
// on every mismatched datum. The public e.Field keeps the full value.
s = fmt.Sprintf("avro: field %s: cannot use %s with Avro type %s", truncForError(e.Field), gt, e.AvroType)
case gt != "" && e.AvroType != "":
s = fmt.Sprintf("avro: cannot use %s with Avro type %s", gt, e.AvroType)
case gt != "":
s = fmt.Sprintf("avro: unsupported type %s", gt)
case e.AvroType != "":
s = fmt.Sprintf("avro: unsupported Avro type %s", e.AvroType)
default:
s = "avro: semantic error"
}
if e.Err != nil {
s += ": " + e.Err.Error()
}
return s
}
func formatGoType(t reflect.Type) string {
if t == nil {
return ""
}
return strings.ReplaceAll(t.String(), "interface {}", "any")
}
func (e *SemanticError) Unwrap() error { return e.Err }
func semErr(v reflect.Value, avroType string) error {
return &SemanticError{GoType: v.Type(), AvroType: avroType}
}
func semErrW(v reflect.Value, avroType string, err error) error {
return &SemanticError{GoType: v.Type(), AvroType: avroType, Err: err}
}
// recordFieldError builds the dotted path for nested records. For an inner
// SemanticError we prepend the field name to the path and keep the inner
// error's type information, so the chain does not fill with misleading
// intermediate "record" types.
func recordFieldError(goType reflect.Type, fieldName string, err error) error {
var inner *SemanticError
if errors.As(err, &inner) {
field := fieldName
if inner.Field != "" {
field = fieldName + "." + inner.Field
}
return &SemanticError{
GoType: inner.GoType,
AvroType: inner.AvroType,
Field: field,
Err: inner.Err,
}
}
return &SemanticError{GoType: goType, AvroType: "record", Field: fieldName, Err: err}
}
// ShortBufferError indicates the input buffer is too short for the value
// being decoded.
type ShortBufferError struct {
// Type is what was being read (e.g. "boolean", "string", "uint32").
Type string
// Need is the number of bytes required (0 if unknown).
Need int
// Have is the number of bytes available.
Have int
}
func (e *ShortBufferError) Error() string {
if e.Need > 0 {
return fmt.Sprintf("avro: short buffer for %s: need %d, have %d", e.Type, e.Need, e.Have)
}
return fmt.Sprintf("avro: short buffer for %s", e.Type)
}
// CompatibilityError describes an incompatibility between a reader and writer
// schema, as returned by [CheckCompatibility] and [Resolve].
type CompatibilityError struct {
// Path is the dotted path to the incompatible element, e.g.
// "User.address.zip".
Path string
// ReaderType is the Avro type in the reader schema.
ReaderType string
// WriterType is the Avro type in the writer schema.
WriterType string
// Detail describes the specific incompatibility.
Detail string
}
func (e *CompatibilityError) Error() string {
// Path, ReaderType, and WriterType carry user-controlled names with no
// length cap at parse, so we truncate them here; the public fields keep
// their full values. Detail truncates its own embedded names when it is
// built.
return fmt.Sprintf("avro: incompatible at %s: reader %s vs writer %s: %s",
truncForError(e.Path), truncForError(e.ReaderType), truncForError(e.WriterType), e.Detail)
}