Sha256: c2df9514ba09232d9144af291729f2e73a6a44b1052b1695daf809bc9aad7d50
Contents?: true
Size: 1.58 KB
Versions: 1
Compression:
Stored size: 1.58 KB
Contents
require "active_record/validations.rb" module Exceptionally module Controller extend ActiveSupport::Concern included do rescue_from Exception, :with => :exceptionally_handler rescue_from ArgumentError, :with => :exceptionally_handler rescue_from Exceptionally::Error, :with => :exceptionally_handler rescue_from ActiveRecord::RecordNotFound, :with => :missing_record_handler rescue_from ActiveRecord::RecordInvalid, :with => :record_invalid_handler rescue_from ActiveRecord::RecordNotSaved, :with => :record_not_saved rescue_from ActionController::ParameterMissing, :with => :missing_param_handler end # Raise custom error def exceptionally_handler(error) pass_to_error_handler(error) end # Raise 400 error def missing_param_handler(error) pass_to_error_handler(error, 400) end # Raise 404 error def missing_record_handler(error) pass_to_error_handler(error, 404) end # Raise 409 error def record_invalid_handler(error) pass_to_error_handler(error, 409) end # Raise 422 error def record_not_saved(error) pass_to_error_handler(error, 422, {validations: error.record.try(:errors)}) end def pass_to_error_handler(error, status = nil, extra = {}) status ||= error.try(:status) || 500 Exceptionally::Handler.new(error.message, status, error, params) render_error(error, status, extra) end def render_error(error, status = 500, extra = {}) render json: {error: error.message}.merge(extra || {}), status: status || 500 end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
exceptionally-1.5.0 | lib/exceptionally/controller.rb |