Skip to content

Commit

Permalink
roll all the error types into one
Browse files Browse the repository at this point in the history
  • Loading branch information
efd6 committed Aug 31, 2023
1 parent 3917f92 commit 1c855ef
Showing 1 changed file with 21 additions and 55 deletions.
76 changes: 21 additions & 55 deletions x-pack/filebeat/input/httpjson/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func encodeAsJSON(trReq transformable) ([]byte, error) {
func decodeAsJSON(p []byte, dst *response) error {
err := json.Unmarshal(p, &dst.body)
if err != nil {
return jsonError{error: err, body: p}
return textContextError{error: err, body: p}
}
return nil
}
Expand All @@ -102,36 +102,14 @@ func decodeAsNdjson(p []byte, dst *response) error {
for dec.More() {
var o interface{}
if err := dec.Decode(&o); err != nil {
return jsonError{error: err, body: p}
return textContextError{error: err, body: p}
}
results = append(results, o)
}
dst.body = results
return nil
}

type jsonError struct {
error
body []byte
}

func (e jsonError) Error() string {
switch err := e.error.(type) {
case nil:
return "<nil>"
case *json.SyntaxError:
return fmt.Sprintf("%v: text context %q", err, textContext(e.body, err.Offset))
case *json.UnmarshalTypeError:
return fmt.Sprintf("%v: text context %q", err, textContext(e.body, err.Offset))
default:
return err.Error()
}
}

func (e jsonError) Unwrap() error {
return e.error
}

// decodeAsCSV decodes p as a headed CSV document into dst.
func decodeAsCSV(p []byte, dst *response) error {
var results []interface{}

Check failure on line 115 in x-pack/filebeat/input/httpjson/encoding.go

View workflow job for this annotation

GitHub Actions / lint (windows)

Consider pre-allocating `results` (prealloc)
Expand Down Expand Up @@ -164,7 +142,7 @@ func decodeAsCSV(p []byte, dst *response) error {

if err != nil {
if err != io.EOF { //nolint:errorlint // csv.Reader never wraps io.EOF.
return csvError{error: err, body: p}
return textContextError{error: err, body: p}
}
}

Expand All @@ -173,31 +151,6 @@ func decodeAsCSV(p []byte, dst *response) error {
return nil
}

type csvError struct {
error
body []byte
}

func (e csvError) Error() string {
switch err := e.error.(type) {
case nil:
return "<nil>"
case *csv.ParseError:
lines := bytes.Split(e.body, []byte{'\n'})
l := err.Line - 1 // Lines are 1-based.
if uint(l) >= uint(len(lines)) {
return err.Error()
}
return fmt.Sprintf("%v: text context %q", err, textContext(lines[l], int64(err.Column)))
default:
return err.Error()
}
}

func (e csvError) Unwrap() error {
return e.error
}

// decodeAsZip decodes p as a ZIP archive into dst.
func decodeAsZip(p []byte, dst *response) error {
var results []interface{}
Expand All @@ -219,7 +172,7 @@ func decodeAsZip(p []byte, dst *response) error {
var o interface{}
if err := dec.Decode(&o); err != nil {
rc.Close()
return jsonError{error: err, body: p}
return textContextError{error: err, body: p}
}
results = append(results, o)
}
Expand All @@ -239,22 +192,35 @@ func decodeAsZip(p []byte, dst *response) error {
func decodeAsXML(p []byte, dst *response) error {
cdata, body, err := xml.Unmarshal(bytes.NewReader(p), dst.xmlDetails)
if err != nil {
return xmlError{error: err, body: p}
return textContextError{error: err, body: p}
}
dst.body = body
dst.header["XML-CDATA"] = []string{cdata}
return nil
}

type xmlError struct {
// textContextError is an error that can provide the text context for
// a decoding error from the csv, json and xml packages.
type textContextError struct {
error
body []byte
}

func (e xmlError) Error() string {
func (e textContextError) Error() string {
switch err := e.error.(type) {

Check failure on line 210 in x-pack/filebeat/input/httpjson/encoding.go

View workflow job for this annotation

GitHub Actions / lint (windows)

type switch on error will fail on wrapped errors. Use errors.As to check for specific errors (errorlint)
case nil:
return "<nil>"
case *json.SyntaxError:
return fmt.Sprintf("%v: text context %q", err, textContext(e.body, err.Offset))
case *json.UnmarshalTypeError:
return fmt.Sprintf("%v: text context %q", err, textContext(e.body, err.Offset))
case *csv.ParseError:
lines := bytes.Split(e.body, []byte{'\n'})
l := err.Line - 1 // Lines are 1-based.
if uint(l) >= uint(len(lines)) {
return err.Error()
}
return fmt.Sprintf("%v: text context %q", err, textContext(lines[l], int64(err.Column)))
case *stdxml.SyntaxError:
lines := bytes.Split(e.body, []byte{'\n'})
l := err.Line - 1 // Lines are 1-based.
Expand All @@ -277,7 +243,7 @@ func (e xmlError) Error() string {
}
}

func (e xmlError) Unwrap() error {
func (e textContextError) Unwrap() error {
return e.error
}

Expand Down

0 comments on commit 1c855ef

Please sign in to comment.