Skip to content

Latest commit

 

History

History
53 lines (35 loc) · 1.14 KB

rails_exceptions.md

File metadata and controls

53 lines (35 loc) · 1.14 KB

Add custom Exception

# gem_dir/lib/gem_name/exceptions.rb
module Exceptions
  class AuthenticationError < StandardError; end
  class InvalidUsername < AuthenticationError; end
end

raise Exception::InvalidUsername

if you want default message

class WrongDocumentStatus < StandardError
  def initialize(msg='Unknown status for Document')
    super
  end
end

sources:

Rails: 3.2.13

Published: 19.09.201

Rake task to list all exceptions

# lib/tasks/exceptions.rake
namespace :exceptions do
  task :list => :environment do
    exceptions = []

    ObjectSpace.each_object(Class) do |k|
      exceptions << k if k.ancestors.include?(Exception)
    end

    puts exceptions#.sort { |a,b| a.name.to_s <=> b.name.to_s }.join("\n")
  end
end

sources:

Rails: 3.2.13

Published: 19.09.2013