ruby - How can I create a custom exception in my Rails application? -
i trying create custom exceptions in rails, 've problem designed solution.
here i've done far:
-create in app/
folder folder named errors/
file exceptions.rb
in it.
app/errors/exceptions.rb
:
module exceptions class apperror < standarderror; end end
- in 1 of controllers, tried raise it:
raise exceptions::apperror.new("user not authorized")
but when call controller's action, here get:
nameerror (uninitialized constant exceptions::apperror did mean? typeerror keyerror ioerror eoferror did mean? typeerror keyerror ioerror eoferror ):
i think don't have understood how create new directories , files, , use them.
i've read created in app
dir, eager loaded, can't understand problem.
short version: rails' automated code loading - fact in case files contain exceptions doesn't matter (see guide on subject more details)
rails try load exceptions/app_error.rb
, in of files on auto load path. because file naming doesn't match this, can't find definition , nameerror.
if don't care code reloading (and might not sort of content) can keep files require them in initialiser (ensure app/errors in load path):
require 'exceptions'
if not you'll have rearrange files match. if add app/errors rails' autoload path , keep files is, should work. if don't want change autoload path you'd have mode somewhere in autoload path , ensure nesting of modules reflects organization on disk.
personally i'd stick these in lib , require them initialiser
Comments
Post a Comment