0

I've created this code to show specific errors messages to user:

application_controller.rb

class ApplicationController < ActionController::Base
  rescue_from Exception do |exception|
    message = exception.message
    message = "default error message" if exception.message.nil?

    render :text => message
  end
end

room_controller.rb

class RoomController < ApplicationController
  def show
    @room = Room.find(params[:room_id]) # Can throw 'ActiveRecord::RecordNotFound'
  end

  def business_method
    # something
    raise ValidationErros::BusinessException("You cant do this") if something #message "You cant do this" should be shown for user
    #...
  end

  def business_method_2
    Room.find(params[:room_id]).do_something
  end
end

room.rb

class Room < ActiveRecord::Base
  def do_something
    #...
    raise ValidationErrors::BusinessException("Invalid state for room") if something #message "Invalid state for room" should be shown for user
    #...
  end
end

app/models/erros/validation_errors.rb

module ValidationErrors
  class BusinessException < RuntimeError
    attr :message
    def initialize(message = nil)
      @message = message
    end
  end
end

example.js

$.ajax({
        url: '/room/show/' + roomId,
        success: function(data){
            //... do something with data
        },
        error: function(data){
            App.notifyError(data) //show dialog with message
        }
    });

But I can not use the class BusinessException. When BusinessException should be raised, the message

uninitialized constant Room::ValidationErrors

is shown to user.

if I change this code:

raise ValidationErrors::BusinessException("Invalid state for room") if something 

by this:

raise "Invalid state for room" if something 

It works.

What change to this code works with BusinessException with messages. I need this to create specifics rescue_from methods in ApplicationController.

Edit:

Thank you for comments! My error is it doesn't know ValidationErrors Module. How to import this Module to my class?

I've tested add theses lines to lines:

require 'app/models/errors/validation_errors.rb'

require 'app/models/errors/validation_errors'

But then raise the error:

cannot load such file -- app/models/errors/validation_errors

Solution:

https://stackoverflow.com/a/3356843/740394

config.autoload_paths += %W(#{config.root}/app/models/errors)
Community
  • 1
  • 1
Rodrigo
  • 5,435
  • 5
  • 42
  • 78
  • It looks like it doesn't know ValidationErrors and it looks for it inside Room. Where did you put that file? – yoavmatchulsky Jul 05 '12 at 01:23
  • I would say you just need to have a `require 'app/models/erros/validation_errors.rb'` somewhere. It doesn't look like that file is getting loaded. – Josh W Lewis Jul 05 '12 at 01:26
  • Ahhhhh! [**Do not rescue from Exception**](http://stackoverflow.com/q/10048173/211563) unless you're re-raising it or know exactly what you're doing! – Andrew Marshall Jul 05 '12 at 03:46
  • look my edit. The mistake is to import the module ValiationErrors! – Rodrigo Jul 06 '12 at 02:14

1 Answers1

0
raise ::ValidationErrors::BusinessException("Invalid state for room")
SMathew
  • 3,993
  • 1
  • 18
  • 10
  • This doesn't works. raise error `uninitialized constant ValidationErrors` – Rodrigo Jul 06 '12 at 02:15
  • 'models' are already in the autoload path. for autoloader to pick up your file, your folder structure should match the class. You shouldn't manually 'require' within a Rails app. – SMathew Jul 06 '12 at 03:05