summaryrefslogtreecommitdiff
path: root/lib/error.rb
diff options
context:
space:
mode:
authorChristoph Helma <helma@in-silico.ch>2012-08-01 14:56:08 +0200
committerChristoph Helma <helma@in-silico.ch>2012-08-01 14:56:08 +0200
commit32ad3c8f6e1e16cfe9fd59a47df6b560ffb13ddd (patch)
treea9b36b58e546cb7bbfd2f9f3c4a16de32abbaa9e /lib/error.rb
parentfc0397cd0d51df4fe7c23f601c2f211c2ebfbf77 (diff)
task error handling improved
Diffstat (limited to 'lib/error.rb')
-rw-r--r--lib/error.rb56
1 files changed, 31 insertions, 25 deletions
diff --git a/lib/error.rb b/lib/error.rb
index d1abe52..84918b7 100644
--- a/lib/error.rb
+++ b/lib/error.rb
@@ -7,43 +7,49 @@ class RuntimeError
super message
@uri = uri
@http_code ||= 500
- $logger.error "\n"+self.to_turtle
+ @rdf = RDF::Graph.new
+ subject = RDF::Node.new
+ @rdf << [subject, RDF.type, RDF::OT.ErrorReport]
+ @rdf << [subject, RDF::OT.actor, @uri.to_s]
+ @rdf << [subject, RDF::OT.message, message.to_s]
+ @rdf << [subject, RDF::OT.statusCode, @http_code]
+ @rdf << [subject, RDF::OT.errorCode, self.class.to_s]
+ @rdf << [subject, RDF::OT.errorCause, short_backtrace]
+ $logger.error("\n"+self.to_turtle)
end
- # define to_ methods for all RuntimeErrors and various rdf formats
- RDF_FORMATS.each do |format|
+ def short_backtrace
+ backtrace = caller.collect{|line| line unless line =~ /#{File.dirname(__FILE__)}/}.compact
+ cut_index = backtrace.find_index{|line| line.match /sinatra|minitest/}
+ cut_index ||= backtrace.size
+ cut_index -= 1
+ cut_index = backtrace.size-1 if cut_index < 0
+ backtrace[0..cut_index].join("\n")
+ end
+ RDF_FORMATS.each do |format|
+ # rdf serialization methods for all formats e.g. to_rdfxml
send :define_method, "to_#{format}".to_sym do
- rdf = RDF::Writer.for(format).buffer do |writer|
- # TODO: not used for turtle
- # http://rdf.rubyforge.org/RDF/Writer.html#
- writer.prefix :ot, RDF::URI('http://www.opentox.org/api/1.2#')
- writer.prefix :ot1_1, RDF::URI('http://www.opentox.org/api/1.1#')
- subject = RDF::Node.new
- writer << [subject, RDF.type, RDF::OT.ErrorReport]
- writer << [subject, RDF::OT.actor, @uri.to_s]
- writer << [subject, RDF::OT.message, message.to_s]
- writer << [subject, RDF::OT.statusCode, @http_code]
- writer << [subject, RDF::OT.errorCode, self.class.to_s]
-
- # cut backtrace
- backtrace = caller.collect{|line| line unless line =~ /#{File.dirname(__FILE__)}/}.compact
- cut_index = backtrace.find_index{|line| line.match /sinatra|minitest/}
- cut_index ||= backtrace.size
- cut_index -= 1
- cut_index = backtrace.size-1 if cut_index < 0
- details = backtrace[0..cut_index].join("\n")
- writer << [subject, RDF::OT.errorCause, details]
+ RDF::Writer.for(format).buffer do |writer|
+ @rdf.each{|statement| writer << statement}
end
- rdf
end
+ end
+ def to_turtle # redefine to use prefixes (not supported by RDF::Writer)
+ prefixes = {:rdf => "http://www.w3.org/1999/02/22-rdf-syntax-ns#"}
+ ['OT', 'DC', 'XSD', 'OLO'].each{|p| prefixes[p.downcase.to_sym] = eval("RDF::#{p}.to_s") }
+ RDF::N3::Writer.for(:turtle).buffer(:prefixes => prefixes) do |writer|
+ @rdf.each{|statement| writer << statement}
+ end
end
+
end
module OpenTox
class Error < RuntimeError
+
def initialize code, message, uri=nil
@http_code = code
super message, uri
@@ -72,7 +78,7 @@ module OpenTox
# define global methods for raising errors, eg. bad_request_error
Object.send(:define_method, klass.underscore.to_sym) do |message,uri=nil|
- raise c, message, uri
+ raise c.new(message, uri)
end
end