extract and refactor api errors
This commit is contained in:
parent
46c355beb2
commit
b187244a29
18 changed files with 89 additions and 61 deletions
|
@ -9,7 +9,7 @@ class ApiEntreprise::Adapter
|
|||
def data_source
|
||||
begin
|
||||
@data_source ||= get_resource
|
||||
rescue ApiEntreprise::API::ResourceNotFound
|
||||
rescue ApiEntreprise::API::Error::ResourceNotFound
|
||||
@data_source = nil
|
||||
end
|
||||
end
|
||||
|
|
|
@ -12,24 +12,6 @@ class ApiEntreprise::API
|
|||
|
||||
TIMEOUT = 20
|
||||
|
||||
class ResourceNotFound < StandardError
|
||||
end
|
||||
|
||||
class RequestFailed < StandardError
|
||||
end
|
||||
|
||||
class BadFormatRequest < StandardError
|
||||
end
|
||||
|
||||
class BadGateway < StandardError
|
||||
end
|
||||
|
||||
class ServiceUnavailable < StandardError
|
||||
end
|
||||
|
||||
class TimedOut < StandardError
|
||||
end
|
||||
|
||||
def self.entreprise(siren, procedure_id)
|
||||
call_with_siret(ENTREPRISE_RESOURCE_NAME, siren, procedure_id)
|
||||
end
|
||||
|
@ -84,7 +66,7 @@ class ApiEntreprise::API
|
|||
if response.success?
|
||||
JSON.parse(response.body, symbolize_names: true)
|
||||
else
|
||||
raise RequestFailed, "HTTP Error Code: #{response.code} for #{url}\nheaders: #{response.headers}\nbody: #{response.body}"
|
||||
raise RequestFailed.new(response)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -100,23 +82,17 @@ class ApiEntreprise::API
|
|||
if response.success?
|
||||
JSON.parse(response.body, symbolize_names: true)
|
||||
elsif response.code&.between?(401, 499)
|
||||
raise ResourceNotFound, "url: #{url}"
|
||||
raise Error::ResourceNotFound.new(response)
|
||||
elsif response.code == 400
|
||||
raise BadFormatRequest, "url: #{url}"
|
||||
raise Error::BadFormatRequest.new(response)
|
||||
elsif response.code == 502
|
||||
raise BadGateway, "url: #{url}"
|
||||
raise Error::BadGateway.new(response)
|
||||
elsif response.code == 503
|
||||
raise ServiceUnavailable, "url: #{url}"
|
||||
raise Error::ServiceUnavailable.new(response)
|
||||
elsif response.timed_out?
|
||||
raise TimedOut, "url: #{url}"
|
||||
raise Error::TimedOut.new(response)
|
||||
else
|
||||
raise RequestFailed,
|
||||
<<~TEXT
|
||||
HTTP Error Code: #{response.code} for #{url}
|
||||
headers: #{response.headers}
|
||||
body: #{response.body}
|
||||
curl message: #{response.return_message}
|
||||
TEXT
|
||||
raise Error::RequestFailed.new(response)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
18
app/lib/api_entreprise/api/error.rb
Normal file
18
app/lib/api_entreprise/api/error.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
class ApiEntreprise::API::Error < ::StandardError
|
||||
def initialize(response)
|
||||
# use uri to avoid sending token
|
||||
uri = URI.parse(response.effective_url)
|
||||
|
||||
msg = <<~TEXT
|
||||
url: #{uri.host}#{uri.path}
|
||||
HTTP error code: #{response.code}
|
||||
body: #{CGI.escape(response.body)}
|
||||
curl message: #{response.return_message}
|
||||
total time: #{response.total_time}
|
||||
connect time: #{response.connect_time}
|
||||
response headers: #{response.headers}
|
||||
TEXT
|
||||
|
||||
super(msg)
|
||||
end
|
||||
end
|
4
app/lib/api_entreprise/api/error/bad_format_request.rb
Normal file
4
app/lib/api_entreprise/api/error/bad_format_request.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
class ApiEntreprise::API::Error
|
||||
class BadFormatRequest < ApiEntreprise::API::Error
|
||||
end
|
||||
end
|
4
app/lib/api_entreprise/api/error/bad_gateway.rb
Normal file
4
app/lib/api_entreprise/api/error/bad_gateway.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
class ApiEntreprise::API::Error
|
||||
class BadGateway < ApiEntreprise::API::Error
|
||||
end
|
||||
end
|
4
app/lib/api_entreprise/api/error/request_failed.rb
Normal file
4
app/lib/api_entreprise/api/error/request_failed.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
class ApiEntreprise::API::Error
|
||||
class RequestFailed < ApiEntreprise::API::Error
|
||||
end
|
||||
end
|
4
app/lib/api_entreprise/api/error/resource_not_found.rb
Normal file
4
app/lib/api_entreprise/api/error/resource_not_found.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
class ApiEntreprise::API::Error
|
||||
class ResourceNotFound < ApiEntreprise::API::Error
|
||||
end
|
||||
end
|
4
app/lib/api_entreprise/api/error/service_unavailable.rb
Normal file
4
app/lib/api_entreprise/api/error/service_unavailable.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
class ApiEntreprise::API::Error
|
||||
class ServiceUnavailable < ApiEntreprise::API::Error
|
||||
end
|
||||
end
|
4
app/lib/api_entreprise/api/error/timed_out.rb
Normal file
4
app/lib/api_entreprise/api/error/timed_out.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
class ApiEntreprise::API::Error
|
||||
class TimedOut < ApiEntreprise::API::Error
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue