diff --git a/x-pack/filebeat/input/httpjson/encoding.go b/x-pack/filebeat/input/httpjson/encoding.go index 2867c5c8cd5d..6ae0a9da4f72 100644 --- a/x-pack/filebeat/input/httpjson/encoding.go +++ b/x-pack/filebeat/input/httpjson/encoding.go @@ -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 } @@ -102,7 +102,7 @@ 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) } @@ -110,28 +110,6 @@ func decodeAsNdjson(p []byte, dst *response) error { return nil } -type jsonError struct { - error - body []byte -} - -func (e jsonError) Error() string { - switch err := e.error.(type) { - case nil: - return "" - 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{} @@ -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} } } @@ -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 "" - 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{} @@ -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) } @@ -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) { case nil: return "" + 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. @@ -277,7 +243,7 @@ func (e xmlError) Error() string { } } -func (e xmlError) Unwrap() error { +func (e textContextError) Unwrap() error { return e.error }