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
1 change: 1 addition & 0 deletions lib/flex_commerce_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "flex_commerce_api/config"
require "flex_commerce"
require "flex_commerce_api/errors"
require "flex_commerce_api/json_api_client_extension/parse_json"

module FlexCommerceApi
def self.config
Expand Down
8 changes: 8 additions & 0 deletions lib/flex_commerce_api/error/internal_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ def message
end
end

def raven_context
{
extra: {
body: response_env[:body]
}
}
end

private

def extract_error(body)
Expand Down
23 changes: 23 additions & 0 deletions lib/flex_commerce_api/json_api_client_extension/parse_json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This module extends json_api_client's ParseJson service
# to make sure malformed repsonse bodies is always included as context in
# the thrown error message when json parsing fails.
module FlexCommerceApi
module JsonApiClientExtension
module ParseJson
def call(environment)
super
rescue JSON::ParserError => error
error.define_singleton_method(:raven_context) do
{
extra: {
body: environment.body
}
}
end
raise
end
end
end
end

JsonApiClient::Middleware::ParseJson.send :prepend, FlexCommerceApi::JsonApiClientExtension::ParseJson
2 changes: 1 addition & 1 deletion lib/flex_commerce_api/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module FlexCommerceApi
VERSION = '0.6.49'
VERSION = '0.6.50'
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'spec_helper'

describe FlexCommerceApi::JsonApiClientExtension::ParseJson do
# Global context for all specs - defines things you dont see defined in here
# such as flex_root_url, api_root, default_headers and page_size
# see api_globals.rb in spec/support for the source code
include_context "global context"

it "includes malformed json in the thrown error when parsing fails" do
stub_request(:get, "#{api_root}/addresses/1.json_api")
.with(headers: default_request_headers)
.to_return(
body: {"key": "some_random_value"}.to_json,
status: response_status,
headers: default_headers
)

allow(JSON).to receive(:parse)
.and_raise(
JSON::ParserError,
"quoted string not terminated at line 1, column 13937 [parse.c:337]"
)

expect{ FlexCommerce::Address.find(1) }.to raise_error do |error|
expect(error).to be_a(JSON::ParserError)
expect(error.raven_context).to eq({
extra: {
body: {"key": "some_random_value"}.to_json,
}
})
end
end
end