Sha256: 8bb1afd9c2b26a3298b8d2cb5c09e2727bf52a7f589d6a97ed5601b16db0e0cf

Contents?: true

Size: 1.24 KB

Versions: 9

Compression:

Stored size: 1.24 KB

Contents

module Hippo
    module API
        module ErrorFormmater
            extend self

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

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

            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

            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

        end
    end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
hippo-fw-0.9.9 lib/hippo/api/error_formatter.rb
hippo-fw-0.9.8 lib/hippo/api/error_formatter.rb
hippo-fw-0.9.7 lib/hippo/api/error_formatter.rb
hippo-fw-0.9.6 lib/hippo/api/error_formatter.rb
hippo-fw-0.9.5 lib/hippo/api/error_formatter.rb
hippo-fw-0.9.4 lib/hippo/api/error_formatter.rb
hippo-fw-0.9.3 lib/hippo/api/error_formatter.rb
hippo-fw-0.9.2 lib/hippo/api/error_formatter.rb
hippo-fw-0.9.1 lib/hippo/api/error_formatter.rb