diff --git a/lib/mindee/v2/parsing/error_response.rb b/lib/mindee/v2/parsing/error_response.rb index cec15610..56753078 100644 --- a/lib/mindee/v2/parsing/error_response.rb +++ b/lib/mindee/v2/parsing/error_response.rb @@ -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 diff --git a/spec/v2/parsing/error_response_spec.rb b/spec/v2/parsing/error_response_spec.rb new file mode 100644 index 00000000..604778b5 --- /dev/null +++ b/spec/v2/parsing/error_response_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require 'mindee/v2/parsing' +require 'mindee/v2/parsing/error_item' +require 'mindee/input/local_response' +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