summaryrefslogtreecommitdiff
path: root/lib/rest-client-wrapper.rb
diff options
context:
space:
mode:
authorChristoph Helma <helma@in-silico.ch>2012-08-02 23:33:11 +0200
committerChristoph Helma <helma@in-silico.ch>2012-08-02 23:33:11 +0200
commit75b1d2a98c17d8ef86c3a7a974e1be5444c9fb20 (patch)
tree650b0c673387ad54f4f6083a79adc3f435e9ae56 /lib/rest-client-wrapper.rb
parent32ad3c8f6e1e16cfe9fd59a47df6b560ffb13ddd (diff)
error handling improved
Diffstat (limited to 'lib/rest-client-wrapper.rb')
-rw-r--r--lib/rest-client-wrapper.rb43
1 files changed, 20 insertions, 23 deletions
diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb
index 387a01b..67a6264 100644
--- a/lib/rest-client-wrapper.rb
+++ b/lib/rest-client-wrapper.rb
@@ -18,13 +18,13 @@ module OpenTox
# check input
@subjectid = headers[:subjectid] ? headers[:subjectid] : nil
bad_request_error "Invalid URI: '#{uri}'", uri unless URI.valid? uri
- #not_found_error "URI '#{uri}' not found.", uri unless URI.accessible?(uri, @subjectid) unless URI.ssl?(uri)
+ #resource_not_found_error "URI '#{uri}' not found.", uri unless URI.accessible?(uri, @subjectid) unless URI.ssl?(uri)
bad_request_error "Headers are not a hash: #{headers.inspect}", uri unless headers==nil or headers.is_a?(Hash)
# make sure that no header parameters are set in the payload
[:accept,:content_type,:subjectid].each do |header|
if defined? $aa || URI(uri).host == URI($aa[:uri]).host
else
- bad_request_error "#{header} should be submitted in the headers" if payload and payload.is_a?(Hash) and payload[header]
+ bad_request_error "#{header} should be submitted in the headers", uri if payload and payload.is_a?(Hash) and payload[header]
end
end
@@ -44,27 +44,11 @@ module OpenTox
response.follow_redirection(request, result)
elsif response.code >= 400 and !URI.task?(uri)
message = response.to_s
- message += "\nREST paramenters:\n#{request.args.inspect}"
- case response.code
- when 400
- bad_request_error message, uri
- when 401
- not_authorized_error message, uri
- when 404
- not_found_error message, uri
- when 433
- locked_error message, uri
- when 500
- internal_server_error message, uri
- when 501
- not_implemented_error message, uri
- when 503
- service_unavailable_error message, uri
- when 504
- time_out_error message, uri
- else
- rest_call_error message, uri
- end
+ parameters = request.args
+ parameters[:headers][:subjectid] = "REMOVED" if parameters[:headers] and parameters[:headers][:subjectid]
+ message += "\nREST paramenters:\n#{parameters.inspect}"
+ error = known_errors.collect{|e| e if e[:code] == response.code}.compact.first
+ Object.method(error[:method]).call message, uri # call error method
else
response
end
@@ -72,5 +56,18 @@ module OpenTox
end
end
+ def self.known_errors
+ errors = []
+ RestClient::STATUSES.each do |code,k|
+ if code >= 400
+ method = k.underscore.gsub(/ |'/,'_')
+ method += "_error" unless method.match(/_error$/)
+ klass = method.split("_").collect{|s| s.capitalize}.join("")
+ errors << {:code => code, :method => method.to_sym, :class => klass}
+ end
+ end
+ errors
+ end
+
end
end