Skip to content

Commit

Permalink
Use custom error
Browse files Browse the repository at this point in the history
  • Loading branch information
ycombinator committed Jan 13, 2019
1 parent e837254 commit 27c3e97
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
21 changes: 20 additions & 1 deletion filebeat/fileset/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ type PipelineLoader interface {
GetVersion() common.Version
}

// MultiplePipelineUnsupportedError is an error returned when a fileset uses multiple pipelines but is
// running against a version of Elasticsearch that doesn't support this feature.
type MultiplePipelineUnsupportedError struct {
module string
fileset string
esVersion common.Version
minESVersionRequired common.Version
}

func (m MultiplePipelineUnsupportedError) Error() string {
return fmt.Sprintf(
"the %s/%s fileset has multiple pipelines, which are only supported with Elasticsearch >= %s. Currently running with Elasticsearch version %s",
m.module,
m.fileset,
m.minESVersionRequired.String(),
m.esVersion.String(),
)
}

// LoadPipelines loads the pipelines for each configured fileset.
func (reg *ModuleRegistry) LoadPipelines(esClient PipelineLoader, overwrite bool) error {
for module, filesets := range reg.registry {
Expand All @@ -62,7 +81,7 @@ func (reg *ModuleRegistry) LoadPipelines(esClient PipelineLoader, overwrite bool
esVersion := esClient.GetVersion()
minESVersionRequired := common.MustNewVersion("6.5.0")
if len(pipelines) > 1 && esVersion.LessThan(minESVersionRequired) {
return fmt.Errorf("the %s/%s fileset has multiple pipelines, which are only supported with Elasticsearch >= %s. Currently running with Elasticsearch version %s", module, name, minESVersionRequired.String(), esVersion.String())
return MultiplePipelineUnsupportedError{module, name, esVersion, *minESVersionRequired}
}

var pipelineIDsLoaded []string
Expand Down
30 changes: 15 additions & 15 deletions filebeat/fileset/pipelines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@ import (

func TestLoadPipelinesWithMultiPipelineFileset(t *testing.T) {
cases := []struct {
name string
esVersion string
expectedErr string
name string
esVersion string
isErrExpected bool
}{
{
name: "ES < 6.5.0",
esVersion: "6.4.1",
expectedErr: "the mod/fls fileset has multiple pipelines, which are only supported with Elasticsearch >= 6.5.0. Currently running with Elasticsearch version 6.4.1",
name: "ES < 6.5.0",
esVersion: "6.4.1",
isErrExpected: true,
},
{
name: "ES == 6.5.0",
esVersion: "6.5.0",
expectedErr: "",
name: "ES == 6.5.0",
esVersion: "6.5.0",
isErrExpected: false,
},
{
name: "ES > 6.5.0",
esVersion: "6.6.0",
expectedErr: "",
name: "ES > 6.5.0",
esVersion: "6.6.0",
isErrExpected: false,
},
}

Expand Down Expand Up @@ -95,10 +95,10 @@ func TestLoadPipelinesWithMultiPipelineFileset(t *testing.T) {
assert.NoError(t, err)

err = testRegistry.LoadPipelines(testESClient, false)
if test.expectedErr == "" {
assert.NoError(t, err)
if test.isErrExpected {
assert.IsType(t, MultiplePipelineUnsupportedError{}, err)
} else {
assert.Error(t, err, test.expectedErr)
assert.NoError(t, err)
}
})
}
Expand Down

0 comments on commit 27c3e97

Please sign in to comment.