Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions lib/mindee/v2/parsing/error_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,32 @@ def initialize(server_response)
end
end

# String representation.
# To make the error prettier to display.
# @return [String]
def to_s
"HTTP #{@status} - #{@title} :: #{@code} - #{@detail}"
result = [
'Error Details',
'=============',
":HTTP Status: #{@status}",
":Title: #{@title}",
":Code: #{@code}",
":Detail: #{@detail}",
]

if @errors && !@errors.empty?
result << ''
result << 'Error Items'
result << '-----------'

@errors.each_with_index do |error, i|
result << "**Error #{i + 1}:**"
result << " :Pointer: #{error.pointer}"
result << " :Detail: #{error.detail}"
result << '' if i < @errors.length - 1
end
end

"#{result.join("\n")}\n"
end

# Hash representation
Expand Down
22 changes: 22 additions & 0 deletions spec/v2/parsing/error_response_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require 'mindee/v2/parsing'
require 'mindee/v2/parsing/error_item'
require 'mindee/input/local_response'
Comment thread
Copilot marked this conversation as resolved.
Resolved
require_relative '../../data'

describe Mindee::V2::Parsing::ErrorResponse do
it 'should load and pretty print an error response' do
local_response = Mindee::Input::LocalResponse.new(
File.join(V2_DATA_DIR, 'errors', 'error_422_invalid_fields.json')
)
response = local_response.deserialize_response(described_class)

rst_output = File.read(
File.join(V2_DATA_DIR, 'errors', 'error_422_invalid_fields.rst')
)

expect(response).not_to be_nil
expect(response.to_s).to eq(rst_output)
end
end
Loading