Module: OpenToxError

Included in:
OpenTox::Error, RuntimeError
Defined in:
lib/error.rb

Overview

add additional fields to Exception class to format errors according to OT-API

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#error_causeObject

Returns the value of attribute error_cause


5
6
7
# File 'lib/error.rb', line 5

def error_cause
  @error_cause
end

#http_codeObject

Returns the value of attribute http_code


5
6
7
# File 'lib/error.rb', line 5

def http_code
  @http_code
end

#uriObject

Returns the value of attribute uri


5
6
7
# File 'lib/error.rb', line 5

def uri
  @uri
end

Class Method Details

.cut_backtrace(trace) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/error.rb', line 33

def self.cut_backtrace(trace)
  if trace.is_a?(Array)
    cut_index = trace.find_index{|line| line.match(/sinatra|minitest/)}
    cut_index ||= trace.size
    cut_index -= 1
    cut_index = trace.size-1 if cut_index < 0
    trace[0..cut_index].join("\n")
  else
    trace
  end
end

Instance Method Details

#encode_with(coder) ⇒ Object

this method defines what is used for to_yaml (override to skip large @rdf graph)


27
28
29
30
31
# File 'lib/error.rb', line 27

def encode_with coder
  @rdf.each do |statement|
    coder[statement.predicate.fragment.to_s] = statement.object.to_s
  end
end

#initialize(message = nil, uri = nil, cause = nil) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/error.rb', line 6

def initialize(message=nil, uri=nil, cause=nil)
  message = message.to_s.gsub(/\A"|"\Z/, '') if message # remove quotes
  @error_cause = cause ? OpenToxError::cut_backtrace(cause) : short_backtrace
  
  super message
  #unless self.is_a? Errno::EAGAIN # avoid "Resource temporarily unavailable" errors
    @uri = uri.to_s.sub(%r{//.*:.*@},'//') # remove credentials from uri
    @http_code ||= 500
    @rdf = RDF::Graph.new
    subject = RDF::Node.new
    @rdf << [subject, RDF.type, RDF::OT.ErrorReport]
    @rdf << [subject, RDF::OT.actor, @uri]
    @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, @error_cause]
    $logger.error("\n"+self.to_yaml) 
  #end
end

#short_backtraceObject


45
46
47
48
# File 'lib/error.rb', line 45

def short_backtrace
  backtrace = caller.collect{|line| line unless line =~ /#{File.dirname(__FILE__)}/}.compact
  OpenToxError::cut_backtrace(backtrace)
end

#to_turtleObject

redefine to use prefixes (not supported by RDF::Writer)


59
60
61
62
63
64
65
# File 'lib/error.rb', line 59

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::Turtle::Writer.for(:turtle).buffer(:prefixes => prefixes)  do |writer|
    @rdf.each{|statement| writer << statement} if @rdf
  end
end