Sha256: 0869b64c125f21c91fe3541a123231a51e9cec01e83d23f187ed248e263d79bf

Contents?: true

Size: 1.24 KB

Versions: 25

Compression:

Stored size: 1.24 KB

Contents

module Lanes
    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
                Lanes.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
                Lanes::API::ErrorFormmater.handle_exception(
                  "Unable to delete #{target}, still in use by #{depends}", 400, e
                )
            end

            def handle_not_found_exception(e,model)
                Lanes::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

25 entries across 25 versions & 1 rubygems

Version Path
lanes-0.8.3 lib/lanes/api/error_formatter.rb
lanes-0.8.2 lib/lanes/api/error_formatter.rb
lanes-0.8.1 lib/lanes/api/error_formatter.rb
lanes-0.8.0 lib/lanes/api/error_formatter.rb
lanes-0.7.0 lib/lanes/api/error_formatter.rb
lanes-0.6.1 lib/lanes/api/error_formatter.rb
lanes-0.6.0 lib/lanes/api/error_formatter.rb
lanes-0.5.6 lib/lanes/api/error_formatter.rb
lanes-0.5.5 lib/lanes/api/error_formatter.rb
lanes-0.5.0 lib/lanes/api/error_formatter.rb
lanes-0.4.0 lib/lanes/api/error_formatter.rb
lanes-0.3.0 lib/lanes/api/error_formatter.rb
lanes-0.1.9.5 lib/lanes/api/error_formatter.rb
lanes-0.1.9 lib/lanes/api/error_formatter.rb
lanes-0.1.8 lib/lanes/api/error_formatter.rb
lanes-0.1.7 lib/lanes/api/error_formatter.rb
lanes-0.1.6 lib/lanes/api/error_formatter.rb
lanes-0.1.5 lib/lanes/api/error_formatter.rb
lanes-0.1.2 lib/lanes/api/error_formatter.rb
lanes-0.1.0 lib/lanes/api/error_formatter.rb