Exception: MeiliSearch::ApiError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/meilisearch/error.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_code, http_message, http_body) ⇒ ApiError

Returns a new instance of ApiError.



20
21
22
23
24
25
26
27
28
# File 'lib/meilisearch/error.rb', line 20

def initialize(http_code, http_message, http_body)
  get_meilisearch_error_info(http_body) unless http_body.nil? || http_body.empty?
  @http_code = http_code
  @http_message = http_message
  @ms_message ||= 'MeiliSearch API has not returned any error message'
  @ms_link ||= '<no documentation link found>'
  @message = "#{http_code} #{http_message} - #{@ms_message}. See #{ms_link}."
  super(details)
end

Instance Attribute Details

#http_bodyObject (readonly)

:http_code # e.g. 400, 404… :http_message # e.g. Bad Request, Not Found… :http_body # The response body received from the MeiliSearch API :ms_code # The error code given by the MeiliSearch API :ms_type # The error type given by the MeiliSearch API :ms_link # The documentation link given by the MeiliSearch API :ms_message # The error message given by the MeiliSearch API :message # The detailed error message of this error class



14
15
16
# File 'lib/meilisearch/error.rb', line 14

def http_body
  @http_body
end

#http_codeObject (readonly)

:http_code # e.g. 400, 404… :http_message # e.g. Bad Request, Not Found… :http_body # The response body received from the MeiliSearch API :ms_code # The error code given by the MeiliSearch API :ms_type # The error type given by the MeiliSearch API :ms_link # The documentation link given by the MeiliSearch API :ms_message # The error message given by the MeiliSearch API :message # The detailed error message of this error class



14
15
16
# File 'lib/meilisearch/error.rb', line 14

def http_code
  @http_code
end

#http_messageObject (readonly)

:http_code # e.g. 400, 404… :http_message # e.g. Bad Request, Not Found… :http_body # The response body received from the MeiliSearch API :ms_code # The error code given by the MeiliSearch API :ms_type # The error type given by the MeiliSearch API :ms_link # The documentation link given by the MeiliSearch API :ms_message # The error message given by the MeiliSearch API :message # The detailed error message of this error class



14
15
16
# File 'lib/meilisearch/error.rb', line 14

def http_message
  @http_message
end

#messageObject (readonly)

:http_code # e.g. 400, 404… :http_message # e.g. Bad Request, Not Found… :http_body # The response body received from the MeiliSearch API :ms_code # The error code given by the MeiliSearch API :ms_type # The error type given by the MeiliSearch API :ms_link # The documentation link given by the MeiliSearch API :ms_message # The error message given by the MeiliSearch API :message # The detailed error message of this error class



14
15
16
# File 'lib/meilisearch/error.rb', line 14

def message
  @message
end

#ms_codeObject (readonly) Also known as: code

:http_code # e.g. 400, 404… :http_message # e.g. Bad Request, Not Found… :http_body # The response body received from the MeiliSearch API :ms_code # The error code given by the MeiliSearch API :ms_type # The error type given by the MeiliSearch API :ms_link # The documentation link given by the MeiliSearch API :ms_message # The error message given by the MeiliSearch API :message # The detailed error message of this error class



14
15
16
# File 'lib/meilisearch/error.rb', line 14

def ms_code
  @ms_code
end

:http_code # e.g. 400, 404… :http_message # e.g. Bad Request, Not Found… :http_body # The response body received from the MeiliSearch API :ms_code # The error code given by the MeiliSearch API :ms_type # The error type given by the MeiliSearch API :ms_link # The documentation link given by the MeiliSearch API :ms_message # The error message given by the MeiliSearch API :message # The detailed error message of this error class



14
15
16
# File 'lib/meilisearch/error.rb', line 14

def ms_link
  @ms_link
end

#ms_messageObject (readonly)

:http_code # e.g. 400, 404… :http_message # e.g. Bad Request, Not Found… :http_body # The response body received from the MeiliSearch API :ms_code # The error code given by the MeiliSearch API :ms_type # The error type given by the MeiliSearch API :ms_link # The documentation link given by the MeiliSearch API :ms_message # The error message given by the MeiliSearch API :message # The detailed error message of this error class



14
15
16
# File 'lib/meilisearch/error.rb', line 14

def ms_message
  @ms_message
end

#ms_typeObject (readonly) Also known as: type

:http_code # e.g. 400, 404… :http_message # e.g. Bad Request, Not Found… :http_body # The response body received from the MeiliSearch API :ms_code # The error code given by the MeiliSearch API :ms_type # The error type given by the MeiliSearch API :ms_link # The documentation link given by the MeiliSearch API :ms_message # The error message given by the MeiliSearch API :message # The detailed error message of this error class



14
15
16
# File 'lib/meilisearch/error.rb', line 14

def ms_type
  @ms_type
end

Instance Method Details

#detailsObject



43
44
45
# File 'lib/meilisearch/error.rb', line 43

def details
  "MeiliSearch::ApiError - code: #{@ms_code} - type: #{ms_type} - message: #{@ms_message} - link: #{ms_link}"
end

#get_meilisearch_error_info(http_body) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/meilisearch/error.rb', line 30

def get_meilisearch_error_info(http_body)
  @http_body = JSON.parse(http_body)
  @ms_code = @http_body['code']
  @ms_message = @http_body['message']
  @ms_type = @http_body['type']
  @ms_link = @http_body['link']
rescue JSON::ParserError
  # We might receive a JSON::ParserError when, for example, MeiliSearch is running behind
  # some proxy (ELB or Nginx, for example), and the request timeouts, returning us
  # a raw HTML body instead of a JSON as we were expecting
  @ms_message = "The server has not returned a valid JSON HTTP body: #{http_body}"
end