Skip to content

Commit

Permalink
Merge pull request #227 from slemrmartin/fix-error-document-conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
lindgrenj6 authored May 24, 2021
2 parents 18a68e3 + b7d215a commit 0319dd8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/insights/api/common/error_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module Common
class ErrorDocument
def add(status = 400, message)
@status = status
errors << {"status" => status, "detail" => message}
safe_message = message.to_s.encode('UTF-8', :invalid => :replace, :undef => :replace)
errors << {"status" => status, "detail" => safe_message}
self
end

Expand Down
4 changes: 4 additions & 0 deletions spec/dummy/app/controllers/api/v1x0.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def error_nested
raise SomethingHappened, "something else happened"
end

def error_utf8
raise StandardError, "something \xAE happened"
end

def http_error
raise ActionCable::Connection::Authorization::UnauthorizedError
end
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
namespace :v1x0, :path => "v1.0" do
get "/error", :to => "errors#error"
get "/error_nested", :to => "errors#error_nested"
get "/error_utf8", :to => "errors#error_utf8"
get "/http_error", :to => "errors#http_error"
get "/api_client_error", :to => "errors#api_client_error"
get "/pundit_error", :to => "errors#pundit_error"
Expand Down
8 changes: 8 additions & 0 deletions spec/requests/exception_handling_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
end
end

context "utf-8 conversion error" do
it "returns the error message" do
get "/api/v1.0/error_utf8", :headers => headers
expect(response.status).to eq(400)
expect(error.first["detail"]).to match(/StandardError/)
end
end

context "api_client_error" do
context "with response body" do
let(:response_header) { { 'Content-Type' => 'application/json' } }
Expand Down

0 comments on commit 0319dd8

Please sign in to comment.