Exception: MeiliSearch::ApiError

Inherits:
Error
  • 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.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/meilisearch/error.rb', line 23

def initialize(http_code, http_message, http_body)
  @http_code = http_code
  @http_message = http_message
  @http_body = parse_body(http_body)
  @ms_code = @http_body['code']
  @ms_type = @http_body['type']
  @ms_message = @http_body.fetch('message', 'MeiliSearch API has not returned any error message')
  @ms_link = @http_body.fetch('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



17
18
19
# File 'lib/meilisearch/error.rb', line 17

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



17
18
19
# File 'lib/meilisearch/error.rb', line 17

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



17
18
19
# File 'lib/meilisearch/error.rb', line 17

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



17
18
19
# File 'lib/meilisearch/error.rb', line 17

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



17
18
19
# File 'lib/meilisearch/error.rb', line 17

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



17
18
19
# File 'lib/meilisearch/error.rb', line 17

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



17
18
19
# File 'lib/meilisearch/error.rb', line 17

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



17
18
19
# File 'lib/meilisearch/error.rb', line 17

def ms_type
  @ms_type
end

Instance Method Details

#detailsObject



50
51
52
# File 'lib/meilisearch/error.rb', line 50

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

#parse_body(http_body) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/meilisearch/error.rb', line 35

def parse_body(http_body)
  if http_body.respond_to?(:to_hash)
    http_body.to_hash
  elsif http_body.respond_to?(:to_str)
    JSON.parse(http_body.to_str)
  else
    {}
  end
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
  { 'message' => "The server has not returned a valid JSON HTTP body: #{http_body}" }
end