Return NonKayveeError if log contians invalid or empty json
This commit is contained in:
parent
eb230b94f7
commit
de04a27799
2 changed files with 14 additions and 3 deletions
|
|
@ -83,8 +83,14 @@ func FieldsFromKayvee(line string) (map[string]interface{}, error) {
|
|||
possibleJSON := line[firstIdx : lastIdx+1]
|
||||
var fields map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(possibleJSON), &fields); err != nil {
|
||||
return map[string]interface{}{}, err
|
||||
return map[string]interface{}{}, &NonKayveeError{}
|
||||
}
|
||||
|
||||
if len(fields) == 0 { // Some logs superfluous "{}" in them. They're not kayvee.
|
||||
return map[string]interface{}{}, &NonKayveeError{}
|
||||
}
|
||||
// TODO: consider also filter if they have source and title
|
||||
|
||||
for k, v := range fields {
|
||||
if !stringInSlice(k, reservedFields) {
|
||||
m[k] = v
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package decode
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
|
@ -75,7 +74,13 @@ func TestKayveeDecoding(t *testing.T) {
|
|||
Title: "errors on invalid JSON (missing a quote)",
|
||||
Input: `prefix {"a:"b"} postfix`,
|
||||
ExpectedOutput: map[string]interface{}{},
|
||||
ExpectedError: &json.SyntaxError{},
|
||||
ExpectedError: &NonKayveeError{},
|
||||
},
|
||||
Spec{
|
||||
Title: "errors on empty JSON: {}",
|
||||
Input: `prefix {} postfix`,
|
||||
ExpectedOutput: map[string]interface{}{},
|
||||
ExpectedError: &NonKayveeError{},
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue