Class: CoreLibrary::ErrorCase

Inherits:
Object
  • Object
show all
Defined in:
lib/apimatic-core/types/error_case.rb

Overview

This data class represents the expected errors to be handled after the API call.

Instance Method Summary collapse

Constructor Details

#initializeErrorCase

Initializes a new instance of ErrorCase.



5
6
7
8
9
# File 'lib/apimatic-core/types/error_case.rb', line 5

def initialize
  @error_message = nil
  @error_message_template = nil
  @exception_type = nil
end

Instance Method Details

#_get_resolved_error_message_template(response) ⇒ String

Updates all placeholders in the given message template with provided value.

Parameters:

  • response

    The received http response.

Returns:

  • (String)

    The resolved template message.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/apimatic-core/types/error_case.rb', line 54

def _get_resolved_error_message_template(response)
  placeholders = @error_message_template.scan(/{\$.*?\}/)

  status_code_placeholder = placeholders.select { |element| element == '{$statusCode}' }.uniq
  header_placeholders = placeholders.select { |element| element.start_with?('{$response.header') }.uniq
  body_placeholders = placeholders.select { |element| element.start_with?('{$response.body') }.uniq

  # Handling response code placeholder
  error_message_template = ApiHelper.resolve_template_placeholders(status_code_placeholder,
                                                                   response.status_code.to_s,
                                                                   @error_message_template)

  # Handling response header placeholder
  error_message_template = ApiHelper.resolve_template_placeholders(header_placeholders, response.headers,
                                                                   error_message_template)

  # Handling response body placeholder
  response_payload = ApiHelper.json_deserialize(response.raw_body, true)
  error_message_template = ApiHelper.resolve_template_placeholders_using_json_pointer(body_placeholders,
                                                                                      response_payload,
                                                                                      error_message_template)

  error_message_template
end

#error_message(error_message) ⇒ ErrorCase

The setter for the description of the error message.

Parameters:

  • error_message (String)

    The error message.

Returns:

  • (ErrorCase)

    An updated instance of ErrorCase.



14
15
16
17
# File 'lib/apimatic-core/types/error_case.rb', line 14

def error_message(error_message)
  @error_message = error_message
  self
end

#error_message_template(error_message_template) ⇒ ErrorCase

The setter for the description of the error message.

Parameters:

  • error_message_template (String)

    The error message template.

Returns:

  • (ErrorCase)

    An updated instance of ErrorCase.



22
23
24
25
# File 'lib/apimatic-core/types/error_case.rb', line 22

def error_message_template(error_message_template)
  @error_message_template = error_message_template
  self
end

#exception_type(exception_type) ⇒ ErrorCase

The setter for the type of the exception to be thrown.

Parameters:

  • exception_type (Object)

    The type of the exception to be thrown.

Returns:

  • (ErrorCase)

    An updated instance of ErrorCase.



30
31
32
33
# File 'lib/apimatic-core/types/error_case.rb', line 30

def exception_type(exception_type)
  @exception_type = exception_type
  self
end

#get_error_message(response) ⇒ String

Getter for the error message for the exception case. This considers both error message and error template message. Error message template has the higher precedence over an error message.

Parameters:

  • response

    The received http response.

Returns:

  • (String)

    The resolved exception message.



39
40
41
42
43
# File 'lib/apimatic-core/types/error_case.rb', line 39

def get_error_message(response)
  return _get_resolved_error_message_template(response) unless @error_message_template.nil?

  @error_message
end

#raise_exception(response) ⇒ Object

Raises the exception for the current error case type.

Parameters:

  • response

    The received response.



47
48
49
# File 'lib/apimatic-core/types/error_case.rb', line 47

def raise_exception(response)
  raise @exception_type.new get_error_message(response), response
end