Code Monkey home page Code Monkey logo

Comments (4)

thomasklemm avatar thomasklemm commented on May 14, 2024

Thanks for this idea, @nixpulvis.

I think we'll probably stay with the rescue_from for the Pundit::NotAuthorizedError currently. Attempted authorization violations are important enough to merit this kind of special treatment IMO.

from pundit.

nixpulvis avatar nixpulvis commented on May 14, 2024

Just for context, the reason I wanted this functionality was to allow something along these lines.

# ExceptionsController
# This controller handles the errors of the application.
#
class ExceptionsController < ApplicationController
  layout 'application'

  def show
    exception = env['action_dispatch.exception']
    class_name = exception.class.name

    @code = ActionDispatch::ExceptionWrapper.new(env, exception).status_code
    @status = ActionDispatch::ExceptionWrapper.rescue_responses[class_name]

    respond_to do |format|
      format.html { render @status, status: @code, layout: !request.xhr? }
      format.json do
        render json: { code: @code, status: @status }, status: @code
      end
    end
  end
end

A nice exception handling controller.

from pundit.

thomasklemm avatar thomasklemm commented on May 14, 2024

@nixpulvis I've never implemented a custom ExceptionsController, thanks for the idea.

from pundit.

nixpulvis avatar nixpulvis commented on May 14, 2024

Yea this was the first time I thought to do it. What's nice about it is rails makes it pretty easy to use.

# ./config/application.rb

# Handle errors internally.
config.exceptions_app = -> (env) do
  ExceptionsController.action(:show).call(env)
end

from pundit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.