Module: Hippo::API::ErrorFormmater

Extended by:
ErrorFormmater
Included in:
ErrorFormmater
Defined in:
lib/hippo/api/error_formatter.rb

Instance Method Summary collapse

Instance Method Details

#call(message, backtrace = nil, options = nil, env = nil) ⇒ Object



6
7
8
9
10
11
# File 'lib/hippo/api/error_formatter.rb', line 6

def call(message, backtrace=nil, options=nil, env=nil)
    {
     success: false, message: message,
     errors: { base: message }
    }.to_json
end

#handle_exception(message, code, e) ⇒ Object



13
14
15
16
17
18
# File 'lib/hippo/api/error_formatter.rb', line 13

def handle_exception(message, code, e)
    Rack::Response.new([self.call(message)],
                       code, {"Content-type" => "application/json"}
                      ).finish
    Hippo.logger.error e
end

#handle_fk_exception(e, model) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/hippo/api/error_formatter.rb', line 20

def handle_fk_exception(e,model)
    ( target, depends ) = e.message.scan(/on table \"(.*?)\"/).flatten.map do |table|
        table.gsub(/^hip_/, '').capitalize.singularize
    end
    Hippo::API::ErrorFormmater.handle_exception(
      "Unable to delete #{target}, still in use by #{depends}", 400, e
    )
end

#handle_not_found_exception(e, model) ⇒ Object



29
30
31
32
33
# File 'lib/hippo/api/error_formatter.rb', line 29

def handle_not_found_exception(e,model)
    Hippo::API::ErrorFormmater.handle_exception(
      model.to_s.demodulize + " (or one of it's dependencies) was not found", 404, e
    )
end