Joao Pereira
Joao Pereira

Reputation: 2534

Convention for the location of error classes in a rails application

Quick question,

I have some ruby classes that extends from StandardError, like:

class NotAuthorized < StandarError
end

I use this class only to raise exceptions in controllers like this

class SomeController < ApplicationController

  before_filter :is_admin!

  protected
  def is_admin!
     raise NotAuthorized if ...
  end
end

Because the NotAuthorized class is not a model, I do not feel that it should be under model folder. Also is not a controller, though it serves only the controllers. Where do you usually put this kind of classes?

Upvotes: 3

Views: 415

Answers (1)

phoet
phoet

Reputation: 18845

as you did not talk about the version of rails that you are using, i will be referring to rails4.

starting with rails 3 (i think) every folder under app will get autoloaded. with this in mind, just put stuff where it belongs app/exceptions app/worker app/whetever.

from my perspective, exceptions are a little different. they usually have some kind of context. that's why i like them to be in some module ie Authorization::NotAuthorized.

so in my case, there would be a concern named Authorization that holds this error class.

Upvotes: 2

Related Questions