Giter Site home page Giter Site logo

scaffeinate / modular-error-handling Goto Github PK

View Code? Open in Web Editor NEW
40.0 40.0 14.0 35 KB

Error Handling in Rails - The Modular Way

Home Page: https://medium.com/rails-ember-beyond/error-handling-in-rails-the-modular-way-9afcddd2fe1b#.yvuf06281

License: MIT License

Ruby 77.71% JavaScript 2.09% CSS 2.23% HTML 17.97%

modular-error-handling's People

Contributors

scaffeinate avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

modular-error-handling's Issues

rescue_from CustomError rails 5

Updated:
Hey, thanks for your write up! In my app rescue_from is not catching NotVisibleError.

In my controller

raise Exceptions::NotVisibleError

When the error comes through into the Error handler it gets caught by the StandardError block, not CustomError.. thoughts as to why this might be?

Files, copy pasta, changed Error namespace to Exceptions

module Exceptions
  class CustomError < StandardError
    attr_reader :status, :error, :message

    def initialize(_error=nil, _status=nil, _message=nil)
      @error = _error || 422
      @status = _status || :unprocessable_entity
      @message = _message || 'Something went wrong'
    end

    def fetch_json
      Helpers::Render.json(error, message, status)
    end
  end
end
module Exceptions
  class NotVisibleError < CustomError
    def initialize
      super(:you_cant_see_me, 422, 'You can\'t see me')
    end
  end
end
module Exceptions
  module ErrorHandler
    def self.included(clazz)
      clazz.class_eval do

        rescue_from ActiveRecord::RecordNotFound do |e|
          respond(:record_not_found, 404, e.to_s)
        end
        rescue_from CustomError do |e|
          respond(e.error, e.status, e.message.to_s)
        end
        rescue_from StandardError do |e|
          respond(:standard_error, 500, e.to_s) 
        end
      end
private
    # respond_to below
end

If I byebug into it

   17: 
   18:         rescue_from StandardError do |e|
   19:           byebug
=> 20:           respond(:standard_error, 500, e.to_s) 
   21:         end
   22:       end
   23:     end
   24: 
(byebug) e
#<Exceptions::NotVisibleError: Exceptions::NotVisibleError>

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.