summaryrefslogtreecommitdiff
path: root/lib/opentox.rb
blob: 1ba77dc63903ce94012d90f95f89157824eb2c78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
require 'sinatra/base'
# Error handling
# Errors are logged as error and formated according to acccept-header
# Non OpenTox::Errors (defined in error.rb) are handled as internal error (500), stacktrace is logged
# IMPT: set sinatra settings :show_exceptions + :raise_errors to false in config.ru, otherwise Rack::Showexceptions takes over

module OpenTox
  class Service < Sinatra::Base
    set :raise_errors, false
    set :show_exceptions, false
    error do
      #TODO: add actor to error report
      #actor = "#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}#{request.env['REQUEST_URI']}"
      error = request.env['sinatra.error']
      case request.env['HTTP_ACCEPT']
      when 'application/rdf+xml'
        content_type 'application/rdf+xml'
      when /html/
        content_type 'text/html'
      when "text/n3"
        content_type "text/n3"
      else
        content_type "text/n3"
      end
      if error.respond_to? :report
        code = error.report.http_code
        case request.env['HTTP_ACCEPT']
        when 'application/rdf+xml'
          body = error.report.to_rdfxml
        when /html/
          body = error.report.to_yaml
        when "text/n3"
          body = error.report.to_ntriples
        else
          body = error.report.to_ntriples
        end
      else
        content_type "text/plain"
        body = error.message
        body += "\n#{error.backtrace}"
      end

      halt code, body
    end
  end
end