GithubHelp home page GithubHelp logo

Comments (4)

richmolj avatar richmolj commented on May 28, 2024

Also relevant to the Railtie vs Mixin issue. One use case: imagine globally rescuing a NotAuthorized error with 403 and generic message. But if you're in Admin::PostsController, you want a custom error message for this internal functionality, maybe with details on who to contact to get privileges. This is another advantage to registering errors on the controller instead of globally.

from graphiti-rails.

wagenet avatar wagenet commented on May 28, 2024

@richmolj I think we may still have a slight disconnect. We're not really registering handlers globally from a developer perspective. The fact that we are using GraphitiErrors.register_exception behind the scenes is an implementation detail that the developer should not have to be concerned about. My concern is to have a default handler for JSON:API (and other types that a developer may opt-in to) and it so happens that a good way to do that is to register a handler with GraphitiErrors.

The Rails approach is to directly use rescue_from for exceptions that you want to handle. I'd like to do the same and not also require the developer to call a second API (e.g. register_exception for this to work correctly).

from graphiti-rails.

wagenet avatar wagenet commented on May 28, 2024

To rewrite the code in the graphiti_errors_spec.rb, something like this:

  def render_graphiti_exception(exception, handler: nil,  **options)
    raise ArgumentError if handler && !options.empty?
    handler ||= GraphitiErrors::ExceptionHandler.new(options)
    handler.log(exception)
    json   = handler.error_payload(exception)
    status = handler.status_code(exception)
    render json: json, status: status
  end

  rescue_from(CustomStatusError) { |e| render_graphiti_exception(e, status: 301) }
  rescue_from(CustomTitleError) { |e| render_graphiti_exception(e, title: "My Title") }
  rescue_from(MessageTrueError) { |e| render_graphiti_exception(e, message: true) }
  rescue_from(MessageProcError) do |e|
    render_graphiti_exception(e, message: ->(e) { e.class.name.upcase })
  end
  rescue_from(MetaProcError) do |e|
    render_graphiti_exception(e, meta: ->(e) { {class_name: e.class.name.upcase} })
  end
  rescue_from(LogFalseError) { |e| render_graphiti_exception(e, log: false) }
  rescue_from(CustomHandlerError) do |e|
    render_graphiti_exception(e, handler: CustomErrorHandler)
  end

This seems to me to be the Rails approach, so the thing to do would basically be to make render_graphiti_exception built-in.

from graphiti-rails.

wagenet avatar wagenet commented on May 28, 2024

Addressed by #22

from graphiti-rails.

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.