[esErrors/isTermSizeZeroError] always return true/false

When the error coming from the elasticsearch.js client is not as expected, isTermSizeZeroError throws a TypeError. This seems incorrect, the function should just return true or false in just about any scenario.
This commit is contained in:
spalger 2016-10-01 00:48:21 +02:00
parent a23cd39bd9
commit cdaab8d902
3 changed files with 17 additions and 3 deletions

View file

@ -30,4 +30,8 @@ describe('isTermSizeZeroError', () => {
};
expect(isTermSizeZeroError(error)).to.be(false);
});
it ('returns false for non-elasticsearch error input', () => {
expect(isTermSizeZeroError({ foo: 'bar' })).to.be(false);
});
});

View file

@ -15,6 +15,17 @@ export default class ElasticsearchError {
}
}
static hasRootCause(error, cause) {
try {
const esError = new ElasticsearchError(error);
return esError.hasRootCause(cause);
} catch (err) {
// we assume that any failure represents a validation error
// in the ElasticsearchError constructor
return false;
}
}
getRootCauses() {
const rootCauses = _.get(this.error, 'resp.error.root_cause');
return _.pluck(rootCauses, 'reason');

View file

@ -1,6 +1,5 @@
import ElasticsearchError from './elasticsearch_error';
import { hasRootCause } from './elasticsearch_error';
export default function isTermSizeZeroError(error) {
const esError = new ElasticsearchError(error);
return esError.hasRootCause('size must be positive, got 0');
return hasRootCause(error, 'size must be positive, got 0');
}