Class: Esse::Transport

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
InstanceMethods
Defined in:
lib/esse/errors.rb,
lib/esse/transport.rb,
lib/esse/transport/health.rb,
lib/esse/transport/search.rb,
lib/esse/transport/aliases.rb,
lib/esse/transport/indices.rb,
lib/esse/transport/documents.rb

Overview

TODO:

Rename this

Defined Under Namespace

Modules: InstanceMethods Classes: BulkResponseError, ReadonlyClusterError, ServerError

Constant Summary collapse

ES_TRANSPORT_ERRORS =
{
  'MultipleChoices' => 'MultipleChoicesError', # 300
  'MovedPermanently' => 'MovedPermanentlyError', # 301
  'Found' => 'FoundError', # 302
  'SeeOther' => 'SeeOtherError', # 303
  'NotModified' => 'NotModifiedError', # 304
  'UseProxy' => 'UseProxyError', # 305
  'TemporaryRedirect' => 'TemporaryRedirectError', # 307
  'PermanentRedirect' => 'PermanentRedirectError', # 308
  'BadRequest' => 'BadRequestError', # 400
  'Unauthorized' => 'UnauthorizedError', # 401
  'PaymentRequired' => 'PaymentRequiredError', # 402
  'Forbidden' => 'ForbiddenError', # 403
  'NotFound' => 'NotFoundError', # 404
  'MethodNotAllowed' => 'MethodNotAllowedError', # 405
  'NotAcceptable' => 'NotAcceptableError', # 406
  'ProxyAuthenticationRequired' => 'ProxyAuthenticationRequiredError', # 407
  'RequestTimeout' => 'RequestTimeoutError', # 408
  'Conflict' => 'ConflictError', # 409
  'Gone' => 'GoneError', # 410
  'LengthRequired' => 'LengthRequiredError', # 411
  'PreconditionFailed' => 'PreconditionFailedError', # 412
  'RequestEntityTooLarge' => 'RequestEntityTooLargeError', # 413
  'RequestURITooLong' => 'RequestURITooLongError', # 414
  'UnsupportedMediaType' => 'UnsupportedMediaTypeError', # 415
  'RequestedRangeNotSatisfiable' => 'RequestedRangeNotSatisfiableError', # 416
  'ExpectationFailed' => 'ExpectationFailedError', # 417
  'ImATeapot' => 'ImATeapotError', # 418
  'TooManyConnectionsFromThisIP' => 'TooManyConnectionsFromThisIPError', # 421
  'UpgradeRequired' => 'UpgradeRequiredError', # 426
  'BlockedByWindowsParentalControls' => 'BlockedByWindowsParentalControlsError', # 450
  'RequestHeaderTooLarge' => 'RequestHeaderTooLargeError', # 494
  'HTTPToHTTPS' => 'HTTPToHTTPSError', # 497
  'ClientClosedRequest' => 'ClientClosedRequestError', # 499
  'InternalServerError' => 'InternalServerError', # 500
  'NotImplemented' => 'NotImplementedError', # 501
  'BadGateway' => 'BadGatewayError', # 502
  'ServiceUnavailable' => 'ServiceUnavailableError', # 503
  'GatewayTimeout' => 'GatewayTimeoutError', # 504
  'HTTPVersionNotSupported' => 'HTTPVersionNotSupportedError', # 505
  'VariantAlsoNegotiates' => 'VariantAlsoNegotiatesError', # 506
  'NotExtended' => 'NotExtendedError', # 510
}
ERRORS =
ES_TRANSPORT_ERRORS.each_with_object({}) do |(transport_name, esse_name), memo|
  memo[transport_name] = const_set esse_name, Class.new(ServerError)
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from InstanceMethods

#aliases, #bulk, #clear_scroll, #close, #count, #create_index, #delete, #delete_index, #exist?, #get, #health, #index, #index_exist?, #open, #refresh, #reindex, #scroll, #search, #update, #update_aliases, #update_by_query, #update_mapping, #update_settings

Constructor Details

#initialize(cluster) ⇒ Transport

Returns a new instance of Transport.



17
18
19
# File 'lib/esse/transport.rb', line 17

def initialize(cluster)
  @cluster = cluster
end

Instance Attribute Details

#clusterObject (readonly)

Returns the value of attribute cluster.



15
16
17
# File 'lib/esse/transport.rb', line 15

def cluster
  @cluster
end

Instance Method Details

#coerce_exceptionObject

Elasticsearch::Transport was renamed to Elastic::Transport in 8.0 This lib should support both versions that’s why we are wrapping up the transport errors to local errors.

We are not only coercing exceptions but also the response body. Elasticsearch-ruby >= 8.0 returns the response wrapped in a Elasticsearch::API::Response::Response object. We are unwrapping it to keep the same interface. But we may want to coerce it to some internal object in the future.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/esse/transport.rb', line 28

def coerce_exception
  resp = yield
  if resp.class.name.start_with?('Elasticsearch::API::Response')
    resp = resp.body
  end
  resp
rescue => exception
  name = Hstring.new(exception.class.name)
  if /^(Elasticsearch|Elastic|OpenSearch)?::Transport::Transport::Errors/.match?(name.value) && \
      (exception_class = Esse::Transport::ERRORS[name.demodulize.value])
    raise exception_class.new(exception.message)
  else
    raise exception
  end
end