Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libbeat: Refactor error handling in schema.Apply() #7335

Merged
merged 1 commit into from
Jul 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-developer.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ The list below covers the major changes between 6.3.0 and master only.
==== Added

- Libbeat provides a global registry for beats developer that allow to register and retrieve plugin. {pull}7392[7392]
- Added more options to control required and optional fields in schema.Apply(), error returned is a plain nil if no error happened {pull}7335[7335]
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ https://github.com/elastic/beats/compare/v6.2.3...master[Check the HEAD diff]
- Add ability to define input configuration as stringified JSON for autodiscover. {pull}7372[7372]
- Add processor definition support for hints builder {pull}7386[7386]
- Add support to disable html escaping in outputs. {pull}7445[7445]
- Refactor error handing in schema.Apply(). {pull}7335[7335]
- Add additional types to kubernetes metadata {pull}7457[7457]

*Auditbeat*
Expand Down
18 changes: 11 additions & 7 deletions libbeat/autodiscover/providers/jolokia/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ import (
// Message contains the information of a Jolokia Discovery message
var messageSchema = s.Schema{
"agent": s.Object{
"id": c.Str("agent_id"),
"version": c.Str("agent_version", s.Optional),
"id": c.Str("agent_id", s.Required),
"version": c.Str("agent_version"),
},
"secured": c.Bool("secured", s.Optional),
"secured": c.Bool("secured"),
"server": s.Object{
"product": c.Str("server_product", s.Optional),
"vendor": c.Str("server_vendor", s.Optional),
"version": c.Str("server_version", s.Optional),
"product": c.Str("server_product"),
"vendor": c.Str("server_vendor"),
"version": c.Str("server_version"),
},
"url": c.Str("url"),
}
Expand Down Expand Up @@ -245,7 +245,11 @@ func (d *Discovery) sendProbe(config InterfaceConfig) {
logp.Err(err.Error())
continue
}
message, _ := messageSchema.Apply(m)
message, err := messageSchema.Apply(m, s.FailOnRequired)
if err != nil {
logp.Err(err.Error())
continue
}
d.update(config, message)
}
}()
Expand Down
81 changes: 60 additions & 21 deletions libbeat/common/schema/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,76 @@

package schema

import "fmt"

const (
RequiredType ErrorType = iota
OptionalType ErrorType = iota
import (
"fmt"
)

type ErrorType int
// KeyError is an error with a field key
type KeyError interface {
Key() string
SetKey(k string)
}

type errorKey struct {
key string
}

// Key returns the value of the field key
func (k *errorKey) Key() string {
return k.key
}

// SetKey sets the value of the field key
func (k *errorKey) SetKey(v string) {
k.key = v
}

// KeyNotFoundError is an error happening when a field key is not found
type KeyNotFoundError struct {
errorKey

Err error
Optional bool
Required bool
}

type Error struct {
key string
message string
errorType ErrorType
// NewKeyNotFoundError builds a KeyNotFoundError
func NewKeyNotFoundError(key string) *KeyNotFoundError {
var e KeyNotFoundError
e.SetKey(key)
return &e
}

func NewError(key string, message string) *Error {
return &Error{
key: key,
message: message,
errorType: RequiredType,
// Error returns the error message of a KeyNotFoundError
func (err *KeyNotFoundError) Error() string {
msg := fmt.Sprintf("key `%s` not found", err.Key())
if err.Err != nil {
msg += ": " + err.Err.Error()
}
return msg
}

func (err *Error) SetType(errorType ErrorType) {
err.errorType = errorType
// WrongFormatError is an error happening when a field format is incorrect
type WrongFormatError struct {
errorKey

Msg string
}

func (err *Error) IsType(errorType ErrorType) bool {
return err.errorType == errorType
// NewWrongFormatError builds a new WrongFormatError
func NewWrongFormatError(key string, msg string) *WrongFormatError {
e := WrongFormatError{
Msg: msg,
}
e.SetKey(key)
return &e
}

func (err *Error) Error() string {
return fmt.Sprintf("Missing field: %s, Error: %s", err.key, err.message)
// Error returns the error message of a WrongFormatError
func (err *WrongFormatError) Error() string {
msg := fmt.Sprintf("wrong format in `%s`", err.Key())
if err.Msg != "" {
msg += ": " + err.Msg
}
return msg
}
37 changes: 0 additions & 37 deletions libbeat/common/schema/error_test.go

This file was deleted.

87 changes: 0 additions & 87 deletions libbeat/common/schema/errors.go

This file was deleted.

32 changes: 0 additions & 32 deletions libbeat/common/schema/errors_test.go

This file was deleted.

Loading