How to use the elasticsearch.errors function in elasticsearch

To help you get started, we’ve selected a few elasticsearch examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github elastic / kibana / src / core / server / elasticsearch / retry_call_cluster.ts View on Github external
* Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import { retryWhen, concatMap } from 'rxjs/operators';
import { defer, throwError, iif, timer } from 'rxjs';
import * as legacyElasticsearch from 'elasticsearch';

import { CallAPIOptions } from '.';
import { Logger } from '../logging';

const esErrors = legacyElasticsearch.errors;

/**
 * Retries the provided Elasticsearch API call when an error such as
 * `AuthenticationException` `NoConnections`, `ConnectionFault`,
 * `ServiceUnavailable` or `RequestTimeout` are encountered. The API call will
 * be retried once a second, indefinitely, until a successful response or a
 * different error is received.
 *
 * @param apiCaller
 */

// TODO: Replace with APICaller from './scoped_cluster_client' once #46668 is merged
export function migrationsRetryCallCluster(
  apiCaller: (
    endpoint: string,
    clientParams: Record,
github pelias / api / test / unit / middleware / sendJSON.js View on Github external
test('generic elasticsearch error', function(t) {
    var res = { body: { geocoding: {
      errors: [ new es.errors.Generic('an error') ]
    }}};

    res.status = function( code ){
      return { json: function( body ){
        t.equal( code, 500, 'Internal Server Error' );
        t.deepEqual( body, res.body, 'body set' );
        t.end();
      }};
    };

    middleware(null, res);
  });
};
github pelias / api / test / unit / middleware / sendJSON.js View on Github external
test('request timeout', function(t) {
    var res = { body: { geocoding: {
      errors: [ new es.errors.RequestTimeout('an error') ]
    }}};

    res.status = function( code ){
      return { json: function( body ){
        t.equal( code, 502, 'Bad Gateway' );
        t.deepEqual( body, res.body, 'body set' );
        t.end();
      }};
    };

    middleware(null, res);
  });
};
github elastic / esqueue / test / fixtures / elasticsearch.js View on Github external
Client.prototype.get = function (params = {}, source = {}) {
  if (params === elasticsearch.errors.NotFound) return elasticsearch.errors.NotFound;

  const _source = Object.assign({
    jobtype: 'jobtype',
    created_by: false,
    payload: {
      id: 'sample-job-1',
      now: 'Mon Apr 25 2016 14:13:04 GMT-0700 (MST)'
    },
    priority: 10,
    timeout: 10000,
    created_at: '2016-04-25T21:13:04.738Z',
    attempts: 0,
    max_attempts: 3,
    status: 'pending'
  }, source);
github elastic / kibana / src / server / saved_objects / client / __tests__ / saved_objects_client_mappings.js View on Github external
import elasticsearch from 'elasticsearch';
import expect from 'expect.js';
import sinon from 'sinon';

import { SavedObjectsClient } from '../saved_objects_client';
const { BadRequest } = elasticsearch.errors;

describe('SavedObjectsClient', () => {
  let callAdminCluster;
  let savedObjectsClient;
  const illegalArgumentException = { type: 'type_missing_exception' };

  describe('mapping', () => {
    beforeEach(() => {
      callAdminCluster = sinon.stub();
      savedObjectsClient = new SavedObjectsClient('.kibana-test', {}, callAdminCluster);
    });

    afterEach(() => {
      callAdminCluster.reset();
    });
github elastic / kibana / src / legacy / core_plugins / elasticsearch / lib / __tests__ / health_check.js View on Github external
*
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import Promise from 'bluebird';
import sinon from 'sinon';
import expect from '@kbn/expect';

const NoConnections = require('elasticsearch').errors.NoConnections;

import healthCheck from '../health_check';
import kibanaVersion from '../kibana_version';

const esPort = 9220;

describe('plugins/elasticsearch', () => {
  describe('lib/health_check', function () {
    let health;
    let plugin;
    let cluster;
    let server;
    const sandbox = sinon.createSandbox();

    function getTimerCount() {
      return Object.keys(sandbox.clock.timers || {}).length;
github elastic / kibana / x-pack / legacy / plugins / reporting / server / lib / esqueue / __tests__ / fixtures / legacy_elasticsearch.js View on Github external
}

    if (endpoint === 'index') {
      const shardCount = 2;
      return Promise.resolve({
        _index: params.index || 'index',
        _id: params.id || uniqueId('testDoc'),
        _seq_no: 1,
        _primary_term: 1,
        _shards: { total: shardCount, successful: shardCount, failed: 0 },
        created: true
      });
    }

    if (endpoint === 'get') {
      if (params === legacyElasticsearch.errors.NotFound) return legacyElasticsearch.errors.NotFound;

      const _source = {
        jobtype: 'jobtype',
        created_by: false,

        payload: {
          id: 'sample-job-1',
          now: 'Mon Apr 25 2016 14:13:04 GMT-0700 (MST)'
        },

        priority: 10,
        timeout: 10000,
        created_at: '2016-04-25T21:13:04.738Z',
        attempts: 0,
        max_attempts: 3,
        status: 'pending',
github pelias / api / middleware / sendJSON.js View on Github external
function isTimeoutError(error) {
  return error instanceof es.errors.RequestTimeout;
}
github kuzzleio / kuzzle / lib / util / esWrapper.js View on Github external
formatESError(error) {
    let
      kuzzleError,
      messageReplaced,
      message = error.message || '';

    if (error instanceof KuzzleError) {
      return error;
    }

    if (error instanceof es.errors.NoConnections) {
      return new ServiceUnavailableError('Elasticsearch service is not connected');
    }

    messageReplaced = errorMessagesMapping.some(mapping => {
      message = message.replace(mapping.regex, mapping.replacement);
      return message !== error.message;
    });

    switch (error.displayName) {
      case 'BadRequest':
        if (!messageReplaced) {
          if (error.body && error.body.error) {
            message = error.body.error.root_cause ? error.body.error.root_cause[0].reason : error.body.error.reason;
          }

          debug('unhandled "BadRequest" elasticsearch error: %a', error);
github elastic / kibana / src / server / plugins / elasticsearch / lib / health_check.js View on Github external
var Promise = require('bluebird');
var elasticsearch = require('elasticsearch');
var exposeClient = require('./expose_client');
var migrateConfig = require('./migrate_config');
var createKibanaIndex = require('./create_kibana_index');
var checkEsVersion = require('./check_es_version');
var NoConnections = elasticsearch.errors.NoConnections;
var util = require('util');
var format = util.format;
module.exports = function (plugin, server) {
  var config = server.config();
  var client = server.plugins.elasticsearch.client;

  plugin.status.yellow('Waiting for Elasticsearch');


  function waitForPong() {
    return client.ping({ requestTimeout: 1500 }).catch(function (err) {
      if (!(err instanceof NoConnections)) throw err;

      plugin.status.red(format('Unable to connect to Elasticsearch at %s. Retrying in 2.5 seconds.', config.get('elasticsearch.url')));

      return Promise.delay(2500).then(waitForPong);