Skip to content

Commit

Permalink
Merge pull request #72 from filestack/fix/response
Browse files Browse the repository at this point in the history
Add rescue JSON::ParserError and raises body message on upload
  • Loading branch information
Gabi Papier authored Oct 2, 2020
2 parents 1bae6ed + ad5a642 commit 1385cb6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/filestack/utils/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def make_call(url, action, parameters: nil, headers: nil)
end

def build_store_task(options = {})
return 'store' if options.empty?
return 'store' if options.nil? || options.empty?
tasks = []
options.each do |key, value|
value = case key
Expand Down Expand Up @@ -100,9 +100,13 @@ def send_upload(apikey, external_url = nil, security = nil, options = nil)
response = Typhoeus.post("#{base}/#{external_url}", headers: FilestackConfig::HEADERS)

if response.code == 200
response_body = JSON.parse(response.body)
handle = response_body['url'].split('/').last
return { 'handle' => handle }
begin
response_body = JSON.parse(response.body)
handle = response_body['url'].split('/').last
return { 'handle' => handle }
rescue
raise response.body
end
end
raise response.body
end
Expand Down
18 changes: 18 additions & 0 deletions spec/filestack/ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,24 @@ def code
expect(UploadUtils.build_store_task).to eq("store")
end

it 'handles non json response' do
class UploadResponse
def code
200
end

def body
"docs provider error: conversion was taking too long (idx 0)"
end
end

allow(Typhoeus).to receive(:post)
.and_return(UploadResponse.new)
expect {
lamdba UploadUtils.send_upload("fakekey")
}.to raise_error(RuntimeError, "docs provider error: conversion was taking too long (idx 0)")
end

###################
## TRANFORM TESTS #
###################
Expand Down

0 comments on commit 1385cb6

Please sign in to comment.