|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | module MeiliSearch |
4 | | - class MeiliSearchError < StandardError; end |
5 | | - class IndexUidError < MeiliSearchError; end |
6 | | - class MeiliSearchTimeoutError < MeiliSearchError |
7 | | - attr_reader :message |
| 4 | + class ApiError < StandardError |
| 5 | + attr_reader :http_code # e.g. 400, 404... |
| 6 | + attr_reader :http_message # e.g. Bad Request, Not Found... |
| 7 | + attr_reader :http_body # The response body received from the MeiliSearch API |
| 8 | + attr_reader :ms_code # The error code given by the MeiliSearch API |
| 9 | + attr_reader :ms_type # The error type given by the MeiliSearch API |
| 10 | + attr_reader :ms_link # The documentation link given by the MeiliSearch API |
| 11 | + attr_reader :ms_message # The error message given by the MeiliSearch API |
| 12 | + attr_reader :message # The detailed error message of this error class |
8 | 13 |
|
9 | | - def initialize |
10 | | - @message = "MeiliSearchTimeoutError: update wasn't processed in the expected time" |
| 14 | + alias code ms_code |
| 15 | + alias type ms_type |
| 16 | + alias link ms_link |
| 17 | + |
| 18 | + def initialize(http_code, http_message, http_body) |
| 19 | + get_meilisearch_error_info(http_body) unless http_body.nil? || http_body.empty? |
| 20 | + @http_code = http_code |
| 21 | + @http_message = http_message |
| 22 | + @ms_message ||= 'MeiliSearch API has not returned any error message' |
| 23 | + @ms_link ||= '<no documentation link found>' |
| 24 | + @message = "#{http_code} #{http_message} - #{@ms_message.capitalize}. See #{ms_link}." |
| 25 | + super(details) |
| 26 | + end |
| 27 | + |
| 28 | + def get_meilisearch_error_info(http_body) |
| 29 | + @http_body = JSON.parse(http_body) |
| 30 | + @ms_code = @http_body['errorCode'] |
| 31 | + @ms_message = @http_body['message'] |
| 32 | + @ms_type = @http_body['errorType'] |
| 33 | + @ms_link = @http_body['errorLink'] |
11 | 34 | end |
12 | 35 |
|
13 | | - def to_s |
14 | | - "#{@message}." |
| 36 | + def details |
| 37 | + "MeiliSearch::ApiError - code: #{@ms_code} - type: #{ms_type} - message: #{@ms_message} - link: #{ms_link}" |
15 | 38 | end |
16 | 39 | end |
17 | 40 |
|
18 | | - class HTTPError < MeiliSearchError |
19 | | - attr_reader :status |
| 41 | + class CommunicationError < StandardError |
20 | 42 | attr_reader :message |
21 | | - attr_reader :http_body |
22 | | - attr_reader :http_body_message |
23 | | - attr_reader :details |
24 | | - |
25 | | - alias code status |
26 | | - alias body http_body |
27 | | - alias body_message http_body_message |
28 | | - |
29 | | - def initialize(status, message, http_body, details = nil) |
30 | | - @status = status |
31 | | - unless http_body.nil? || http_body.empty? |
32 | | - @http_body = JSON.parse(http_body) |
33 | | - @http_body_message = @http_body['message'] |
34 | | - end |
35 | | - @message = message.capitalize |
36 | | - @message = "#{@message} - #{@http_body_message.capitalize}" unless @http_body_message.nil? |
37 | | - @details = details |
| 43 | + |
| 44 | + def initialize(message) |
| 45 | + @message = "An error occurred while trying to connect to the MeiliSearch instance: #{message}" |
| 46 | + super(@message) |
38 | 47 | end |
| 48 | + end |
39 | 49 |
|
40 | | - def to_s |
41 | | - final_message = @details.nil? ? @message : "#{@message}. #{@details}" |
42 | | - "#{@status}: #{final_message}." |
| 50 | + class TimeoutError < StandardError |
| 51 | + attr_reader :message |
| 52 | + |
| 53 | + def initialize |
| 54 | + @message = 'The update was not processed in the expected time' |
| 55 | + super(@message) |
43 | 56 | end |
44 | 57 | end |
45 | 58 | end |
0 commit comments