summaryrefslogtreecommitdiff
path: root/lib/error.rb
blob: b72ce7e0ddf9d8866563871ee70800568890355d (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61

# adding additional fields to Exception class to format errors according to OT-API
class Exception
  attr_accessor :creator, :errorCause, :id
end

module OpenTox
  
  class NotAuthorizedError < Exception
  end
  
  class NotFoundError < Exception
  end
  
  class BadRequestError < Exception
  end
  
  class RestCallError < Exception
    attr_accessor :code, :body, :uri, :payload, :headers
  end

  class ErrorReport
    
    # formats error according to accept-header, yaml is default
    # ( sets content-type in response accordingly )
    # @param [Exception] error
    # @param |Sinatra::Request, optional] request
    # @param [Sinatra::Response, optiona,] response, optional to set content-type
    # @return [String]  formated error
    def self.format(error, request=nil, response=nil)
      # sets current uri
      error.creator = "#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}#{request.env['REQUEST_URI']}" if request
      accept = request.env['HTTP_ACCEPT'].to_s if request
      case accept
      # when /rdf/
      # TODO add error to rdf
      when /html/
        response['Content-Type'] = 'text/html' if response
        OpenTox.text_to_html error.to_yaml
      else
        response['Content-Type'] = 'application/x-yaml' if response
        error.to_yaml
      end
    end
    
    # trys to parse error from text
    # @return [Exception] Exception if parsing sucessfull, nil otherwise
    def self.parse( body )
      begin
        err = YAML.load(body)
        if err and err.is_a?(Exception)
          return err
        else
          return nil
        end
      rescue
        return nil
      end
    end
  end
end