summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorChristoph Helma <ch@toxbank-ch.in-silico.ch>2012-02-15 17:20:24 +0000
committerChristoph Helma <ch@toxbank-ch.in-silico.ch>2012-02-15 17:20:24 +0000
commit7ba9df992e8a0db4f3ff79754e48118e7dba1e0b (patch)
treec9bd8ec8d29e8662231266741e405b306108318d /lib
parent76e2c84c766fd37d702c45e76dc666230afcd136 (diff)
parent9ed209b262e0b540af967e24e9b9845600a0669c (diff)
Merge branch 'development' of github.com:opentox/opentox-client into development
Conflicts: lib/spork.rb lib/task.rb
Diffstat (limited to 'lib')
-rw-r--r--lib/error.rb99
-rw-r--r--lib/spork.rb1
-rw-r--r--lib/task.rb6
3 files changed, 102 insertions, 4 deletions
diff --git a/lib/error.rb b/lib/error.rb
new file mode 100644
index 0000000..b92f2a4
--- /dev/null
+++ b/lib/error.rb
@@ -0,0 +1,99 @@
+
+# adding additional fields to Exception class to format errors according to OT-API
+class Exception
+ attr_accessor :errorCause
+ def http_code; 500; end
+end
+
+module OpenTox
+
+ class BadRequestError < RuntimeError
+ def http_code; 400; end
+ end
+
+ class NotAuthorizedError < RuntimeError
+ def http_code; 401; end
+ end
+
+ class NotFoundError < RuntimeError
+ def http_code; 404; end
+ end
+
+ class ServiceUnavailableError < RuntimeError
+ def http_code; 503; end
+ end
+
+ class RestCallError < RuntimeError
+ attr_accessor :rest_params
+ def http_code; 502; end
+ end
+
+ class ErrorReport
+
+ # TODO replace params with URIs (errorCause -> OT.errorCause)
+ attr_reader :message, :actor, :errorCause, :http_code, :errorDetails, :errorType
+
+ private
+ def initialize( http_code, erroType, message, actor, errorCause, rest_params=nil, backtrace=nil )
+ @http_code = http_code
+ @errorType = erroType
+ @message = message
+ @actor = actor
+ @errorCause = errorCause
+ @rest_params = rest_params
+ @backtrace = backtrace
+ end
+
+ public
+ # creates a error report object, from an ruby-exception object
+ # @param [Exception] error
+ # @param [String] actor, URI of the call that cause the error
+ def self.create( error, actor )
+ rest_params = error.rest_params if error.is_a?(OpenTox::RestCallError) and error.rest_params
+ backtrace = error.backtrace.short_backtrace if CONFIG[:backtrace]
+ ErrorReport.new( error.http_code, error.class.to_s, error.message, actor, error.errorCause, rest_params, backtrace )
+ end
+
+ def self.from_rdf(rdf)
+ metadata = OpenTox::Parser::Owl.from_rdf( rdf, OT.ErrorReport ).metadata
+ ErrorReport.new(metadata[OT.statusCode], metadata[OT.errorCode], metadata[OT.message], metadata[OT.actor], metadata[OT.errorCause])
+ end
+
+ # overwrite sorting to make easier readable
+ def to_yaml_properties
+ p = super
+ p = ( p - ["@backtrace"]) + ["@backtrace"] if @backtrace
+ p = ( p - ["@errorCause"]) + ["@errorCause"] if @errorCause
+ p
+ end
+
+ def rdf_content()
+ c = {
+ RDF.type => [OT.ErrorReport],
+ OT.statusCode => @http_code,
+ OT.message => @message,
+ OT.actor => @actor,
+ OT.errorCode => @errorType,
+ }
+ c[OT.errorCause] = @errorCause.rdf_content if @errorCause
+ c
+ end
+
+ def to_rdfxml
+ s = Serializer::Owl.new
+ s.add_resource(CONFIG[:services]["opentox-task"]+"/tmpId/ErrorReport/tmpId", OT.errorReport, rdf_content)
+ s.to_rdfxml
+ end
+ end
+end
+
+class Array
+ def short_backtrace
+ short = []
+ each do |c|
+ break if c =~ /sinatra\/base/
+ short << c
+ end
+ short.join("\n")
+ end
+end
diff --git a/lib/spork.rb b/lib/spork.rb
index 40c458b..c77b5b5 100644
--- a/lib/spork.rb
+++ b/lib/spork.rb
@@ -81,4 +81,3 @@ if defined? Passenger::Rack::RequestHandler
end
end
end
-
diff --git a/lib/task.rb b/lib/task.rb
index cd5aa79..aee6c62 100644
--- a/lib/task.rb
+++ b/lib/task.rb
@@ -5,7 +5,7 @@ module OpenTox
# Class for handling asynchronous tasks
class Task
- def self.create service_uri, params
+ def self.create service_uri, params={}
task = Task.new RestClient.post(service_uri,params).chomp
pid = Spork.spork { yield }
task.pid = pid
@@ -45,10 +45,10 @@ module OpenTox
when /=/
res = RestClient.put(File.join(@uri,method.sub(/=/,'')),{})
super unless res.code == 200
- when /?/
+ when /\?/
return metadata[RDF::OT.hasStatus] == method.sub(/\?/,'').capitalize
else
- return metadata[RDF::OT[method]]
+ return metadata[RDF::OT[method]].to_s
end
rescue
super