scaffeinate / modular-error-handling Goto Github PK
View Code? Open in Web Editor NEWError Handling in Rails - The Modular Way
License: MIT License
Error Handling in Rails - The Modular Way
License: MIT License
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>
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.