Pundit gem error class overriding my custom error class in ruby

206 views Asked by At

my application has custom error classes defined lib/error/*

example

#lib/error/custom_error.rb

module Error
      class CustomError < StandardError
        attr_accessor :error, :status, :message
    
        def initialize error, status, message
          @error    = error || :unprocessable_entity
          @status   = status || 422 
          @message  = message  || 'Something went wrong'
        end

        # this is just an example
        def raise_unauth_error
          raise "un auth"
        end
    
      end 
    end

this has been used in a ton of controller just by including it like

class Api::UsersController < Api::ApiBaseController
   include Error::CustomError

   def set_user
     .
     .
     .
     raise Error::CustomError.new('sample message', 404, ""?) unless @user
   end

   def user_check
     raise_unauth_error unless @user.admin?
   end
end

I recently added pundit gem to my application

class Api::ApiBaseController < ActionController::Api
  include Pundit
end

now I am getting errors saying Pundit::Error::CustomError (NameError), from everywhere. it goes away if I included the error class like this ::Error::CustomError but if I have to do this there are a ton of places where I have to make this edit

is there a way to include pundit gem without overriding the custom error class?

1

There are 1 answers

0
Cassandra S. On

It's a known issue in the Pundit repository, and as noted in the discussion there, the only way around it (currently) is through using ::Error::CustomError for now.