Skip to content

Commit

Permalink
Merge pull request #20 from frantisekrokusek/fr/improve-construct-err…
Browse files Browse the repository at this point in the history
…or-message

rework exceptions handling
  • Loading branch information
Franciscorodr authored Jun 20, 2022
2 parents b0a1176 + 9053517 commit 8783f19
Showing 1 changed file with 11 additions and 38 deletions.
49 changes: 11 additions & 38 deletions lib/fintecture/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,61 +12,34 @@ class << self
def error(res)
body = JSON.parse res.body

# Errors array
if body['code'] && body['log_id'] && body['errors']
raise _construct_message_errors(res, body)
# Single error
else
raise _construct_message_error(res, body)
end
raise construct_message_errors(res.status, body)
end

private

def _construct_message_errors(res, body)
status = res.status
code = body['code']
log_id = body['log_id']
errors_array = body['errors']
def construct_message_errors(status, body)
code = body['code'].presence
log_id = body['log_id'].presence
errors_array = body['errors'].presence || body['meta'].presence || []

error_string = "\nFintecture server errors : "
error_string += "\n status: #{status} "
error_string += "\n code: #{code}"
error_string += "\n id : #{log_id}"
error_string += "\n status: #{status} " if status
error_string += "\n code: #{code}" if code
error_string += "\n id : #{log_id}" if log_id

errors_array.compact.each do |error|
errors_array.compact!
errors_array.each do |error|
formated_error = error
formated_error = error.map { |key, value| " #{key}: #{value}" }.join("\n") if error.is_a?(Hash)
error_string += "\n\n#{formated_error}"
end
error_string += "\n\n"

{
type: 'Fintecture api',
status: status,
errors: errors_array,
error_string: error_string
}.to_json
end

def _construct_message_error(res, body)
status = res.status
error = body['meta']

error_string = "\nFintecture server errors : "
error_string += "\n status: #{status} "

formated_error = error
formated_error = error.map { |key, value| " #{key}: #{value}" }.join("\n") if error.is_a?(Hash)

error_string += "\n\n#{formated_error}"

error_string += "\n\n"

{
type: 'Fintecture api',
status: status,
errors: [error],
errors: errors_array,
error_string: error_string
}.to_json
end
Expand Down

0 comments on commit 8783f19

Please sign in to comment.