Class: CoreLibrary::ErrorCase
- Inherits:
-
Object
- Object
- CoreLibrary::ErrorCase
- 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
-
#_get_resolved_error_message_template(response) ⇒ String
Updates all placeholders in the given message template with provided value.
-
#error_message(error_message) ⇒ ErrorCase
The setter for the description of the error message.
-
#error_message_template(error_message_template) ⇒ ErrorCase
The setter for the description of the error message.
-
#exception_type(exception_type) ⇒ ErrorCase
The setter for the type of the exception to be thrown.
-
#get_error_message(response) ⇒ String
Getter for the error message for the exception case.
-
#initialize ⇒ ErrorCase
constructor
Initializes a new instance of ErrorCase.
-
#raise_exception(response) ⇒ Object
Raises the exception for the current error case type.
Constructor Details
#initialize ⇒ ErrorCase
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.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/apimatic-core/types/error_case.rb', line 54 def (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 = ApiHelper.resolve_template_placeholders(status_code_placeholder, response.status_code.to_s, @error_message_template) # Handling response header placeholder = ApiHelper.resolve_template_placeholders(header_placeholders, response.headers, ) # Handling response body placeholder begin response_payload = ApiHelper.json_deserialize(response.raw_body, true) unless response.raw_body.nil? rescue TypeError # This statement execution means the received response body is not a JSON but a simple string response_payload = response.raw_body end = ApiHelper.resolve_template_placeholders_using_json_pointer(body_placeholders, response_payload, ) end |
#error_message(error_message) ⇒ ErrorCase
The setter for the description of the error message.
14 15 16 17 |
# File 'lib/apimatic-core/types/error_case.rb', line 14 def () @error_message = self end |
#error_message_template(error_message_template) ⇒ ErrorCase
The setter for the description of the error message.
22 23 24 25 |
# File 'lib/apimatic-core/types/error_case.rb', line 22 def () @error_message_template = self end |
#exception_type(exception_type) ⇒ ErrorCase
The setter for the type of the exception to be thrown.
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.
39 40 41 42 43 |
# File 'lib/apimatic-core/types/error_case.rb', line 39 def (response) return (response) unless @error_message_template.nil? @error_message end |
#raise_exception(response) ⇒ Object
Raises the exception for the current error case type.
47 48 49 |
# File 'lib/apimatic-core/types/error_case.rb', line 47 def raise_exception(response) raise @exception_type.new (response), response end |