M o r e a t r u b y o n r a i l s . o r g :
M o r e R u b y o n R a i l s
● B l o g
● G u i d e s
● A P I
● F o r u m
● C o n t r i b u t e o n G i t H u b
G u i d e s
V e r s i o n :
Edge
7.1
7.0
6.1
6.0
5.2
5.1
5.0
4.2
4.1
4.0
3.2
3.1
3.0
2.3
● H o m e
●
Models
Active Record Basics
Active Record Migrations
Active Record Validations
Active Record Callbacks
Active Record Associations
Active Record Query Interface
Active Model Basics
Views
Action View Overview
Layouts and Rendering in Rails
Action View Helpers
Action View Form Helpers
Controllers
Action Controller Overview
Rails Routing from the Outside In
Other Components
Active Support Core Extensions
Action Mailer Basics
Action Mailbox Basics
Action Text Overview
Active Job Basics
Active Storage Overview
Action Cable Overview
Digging Deeper
Rails Internationalization (I18n) API
Testing Rails Applications
Securing Rails Applications
Error Reporting in Rails Applications
Debugging Rails Applications
Configuring Rails Applications
The Rails Command Line
The Asset Pipeline
Working with JavaScript in Rails
Autoloading and Reloading
Caching with Rails: An Overview
Using Rails for API-only Applications
Tuning Performance for Deployment
Advanced Active Record
Multiple Databases
Composite Primary Keys
Extending Rails
Rails on Rack
Creating and Customizing Rails Generators & Templates
Contributing
Contributing to Ruby on Rails
API Documentation Guidelines
Guides Guidelines
Installing Rails Core Development Dependencies
Policies
Maintenance Policy
Release Notes
Upgrading Ruby on Rails
Version 7.2 - ?
Version 7.1 - October 2023
Version 7.0 - December 2021
Version 6.1 - December 2020
Version 6.0 - August 2019
Version 5.2 - April 2018
Version 5.1 - April 2017
Version 5.0 - June 2016
Version 4.2 - December 2014
Version 4.1 - April 2014
Version 4.0 - June 2013
Version 3.2 - January 2012
Version 3.1 - August 2011
Version 3.0 - August 2010
Version 2.3 - March 2009
Version 2.2 - November 2008
● C o n t r i b u t e
●
Guides Index
Getting Started with Rails
Active Record Basics
Active Record Migrations
Active Record Validations
Active Record Callbacks
Active Record Associations
Active Record Query Interface
Active Model Basics
Action View Overview
Layouts and Rendering in Rails
Action View Helpers
Action View Form Helpers
Action Controller Overview
Rails Routing from the Outside In
Active Support Core Extensions
Action Mailer Basics
Action Mailbox Basics
Action Text Overview
Active Job Basics
Active Storage Overview
Action Cable Overview
Rails Internationalization (I18n) API
Testing Rails Applications
Securing Rails Applications
Error Reporting in Rails Applications
Debugging Rails Applications
Configuring Rails Applications
The Rails Command Line
The Asset Pipeline
Working with JavaScript in Rails
Autoloading and Reloading
Caching with Rails: An Overview
Using Rails for API-only Applications
Tuning Performance for Deployment
Multiple Databases
Composite Primary Keys
Rails on Rack
Creating and Customizing Rails Generators & Templates
Contributing to Ruby on Rails
API Documentation Guidelines
Guides Guidelines
Installing Rails Core Development Dependencies
Maintenance Policy
Upgrading Ruby on Rails
Version 7.2 - ?
Version 7.1 - October 2023
Version 7.0 - December 2021
Version 6.1 - December 2020
Version 6.0 - August 2019
Version 5.2 - April 2018
Version 5.1 - April 2017
Version 5.0 - June 2016
Version 4.2 - December 2014
Version 4.1 - April 2014
Version 4.0 - June 2013
Version 3.2 - January 2012
Version 3.1 - August 2011
Version 3.0 - August 2010
Version 2.3 - March 2009
Version 2.2 - November 2008
E r r o r R e p o r t i n g i n R a i l s A p p l i c a t i o n s
T h i s g u i d e i n t r o d u c e s w a y s t o m a n a g e e x c e p t i o n s t h a t o c c u r i n R u b y o n R a i l s a p p l i c a t i o n s .
A f t e r r e a d i n g t h i s g u i d e , y o u w i l l k n o w :
● H o w t o u s e R a i l s ' e r r o r r e p o r t e r t o c a p t u r e a n d r e p o r t e r r o r s .
● H o w t o c r e a t e c u s t o m s u b s c r i b e r s f o r y o u r e r r o r - r e p o r t i n g s e r v i c e .
C h a p t e r s
(一) E r r o r R e p o r t i n g
● S u b s c r i b i n g t o t h e R e p o r t e r
● U s i n g t h e E r r o r R e p o r t e r
● E r r o r - r e p o r t i n g O p t i o n s
● F i l t e r i n g b y E r r o r C l a s s e s
● S e t t i n g C o n t e x t G l o b a l l y
● F o r L i b r a r i e s
T h e R a i l s e r r o r r e p o r t e r p r o v i d e s a s t a n d a r d w a y t o c o l l e c t e x c e p t i o n s t h a t o c c u r i n y o u r a p p l i c a t i o n a n d r e p o r t t h e m t o y o u r p r e f e r r e d s e r v i c e o r l o c a t i o n .
T h e e r r o r r e p o r t e r a i m s t o r e p l a c e b o i l e r p l a t e e r r o r - h a n d l i n g c o d e l i k e t h i s :
begin
do_something
rescue SomethingIsBroken => error
MyErrorReportingService . notify ( error )
end
e r r o r
M y E r r o r R e p o r t i n g S e r v i c e . n o t i f y ( e r r o r )
e n d
" > C o p y
w i t h a c o n s i s t e n t i n t e r f a c e :
Rails . error . handle ( SomethingIsBroken ) do
do_something
end
C o p y
R a i l s w r a p s a l l e x e c u t i o n s ( s u c h a s H T T P r e q u e s t s , j o b s , a n d r a i
l s r u n n e r
i n v o c a t i o n s ) i n t h e e r r o r r e p o r t e r , s o a n y u n h a n d l e d e r r o r s r a i s e d i n y o u r a p p w i l l a u t o m a t i c a l l y b e r e p o r t e d t o y o u r e r r o r - r e p o r t i n g s e r v i c e v i a t h e i r s u b s c r i b e r s .
T h i s m e a n s t h a t t h i r d - p a r t y e r r o r - r e p o r t i n g l i b r a r i e s n o l o n g e r n e e d t o i n s e r t a R a c k m i d d l e w a r e o r d o a n y m o n k e y - p a t c h i n g t o c a p t u r e u n h a n d l e d e x c e p t i o n s . L i b r a r i e s t h a t u s e A c t i v e S u p p o r t c a n a l s o u s e t h i s t o n o n - i n t r u s i v e l y r e p o r t w a r n i n g s t h a t w o u l d p r e v i o u s l y h a v e b e e n l o s t i n l o g s .
U s i n g t h e R a i l s ' e r r o r r e p o r t e r i s n o t r e q u i r e d . A l l o t h e r m e a n s o f c a p t u r i n g e r r o r s s t i l l w o r k .
T o u s e t h e e r r o r r e p o r t e r , y o u n e e d a s u b s c r i b e r . A s u b s c r i b e r i s a n y o b j e c t w i t h a r e p o r t
m e t h o d . W h e n a n e r r o r o c c u r s i n y o u r a p p l i c a t i o n o r i s m a n u a l l y r e p o r t e d , t h e R a i l s e r r o r r e p o r t e r w i l l c a l l t h i s m e t h o d w i t h t h e e r r o r o b j e c t a n d s o m e o p t i o n s .
S o m e e r r o r - r e p o r t i n g l i b r a r i e s , s u c h a s S e n t r y ' s a n d H o n e y b a d g e r ' s , a u t o m a t i c a l l y r e g i s t e r a s u b s c r i b e r f o r y o u . C o n s u l t y o u r p r o v i d e r ' s d o c u m e n t a t i o n f o r m o r e d e t a i l s .
Y o u m a y a l s o c r e a t e a c u s t o m s u b s c r i b e r . F o r e x a m p l e :
# config/initializers/error_subscriber.rb
class ErrorSubscriber
def report ( error , handled :, severity :, context :, source: nil )
MyErrorReportingService . report_error ( error , context: context , handled: handled , level: severity )
end
end
C o p y
A f t e r d e f i n i n g t h e s u b s c r i b e r c l a s s , r e g i s t e r i t b y c a l l i n g R a i l
s . e r r o r . s u b s c r i b e
m e t h o d :
Rails . error . subscribe ( ErrorSubscriber . new )
C o p y
Y o u c a n r e g i s t e r a s m a n y s u b s c r i b e r s a s y o u w i s h . R a i l s w i l l c a l l t h e m i n t u r n , i n t h e o r d e r i n w h i c h t h e y w e r e r e g i s t e r e d .
T h e R a i l s e r r o r - r e p o r t e r w i l l a l w a y s c a l l r e g i s t e r e d s u b s c r i b e r s , r e g a r d l e s s o f y o u r e n v i r o n m e n t . H o w e v e r , m a n y e r r o r - r e p o r t i n g s e r v i c e s o n l y r e p o r t e r r o r s i n p r o d u c t i o n b y d e f a u l t . Y o u s h o u l d c o n f i g u r e a n d t e s t y o u r s e t u p a c r o s s e n v i r o n m e n t s a s n e e d e d .
T h e r e a r e t h r e e w a y s y o u c a n u s e t h e e r r o r r e p o r t e r :
R a i l s . e r r o r . h a n d l e
w i l l r e p o r t a n y e r r o r r a i s e d w i t h i n t h e b l o c k . I t w i l l t h e n s w a l l o w t h e e r r o r , a n d t h e r e s t o f y o u r c o d e o u t s i d e t h e b l o c k w i l l c o n t i n u e a s n o r m a l .
result = Rails . error . handle do
1 + '1' # raises TypeError
end
result # => nil
1 + 1 # This will be executed
n i l
1 + 1 # T h i s w i l l b e e x e c u t e d
" > C o p y
I f n o e r r o r i s r a i s e d i n t h e b l o c k , R a i l s . e r r o r . h a n d l e
w i l l r e t u r n t h e r e s u l t o f t h e b l o c k , o t h e r w i s e i t w i l l r e t u r n n i l
. Y o u c a n o v e r r i d e t h i s b y p r o v i d i n g a f
a l l b a c k
:
user = Rails . error . handle ( fallback: -> { User . anonymous }) do
User . find_by ( params [ :id ])
end
{ U s e r . a n o n y m o u s } ) d o
U s e r . f i n d _ b y ( p a r a m s [ : i d ] )
e n d
" > C o p y
R a i l s . e r r o r . r e c o r d
w i l l r e p o r t e r r o r s t o a l l r e g i s t e r e d s u b s c r i b e r s a n d t h e n r e - r a i s e t h e e r r o r , m e a n i n g t h a t t h e r e s t o f y o u r c o d e w o n ' t e x e c u t e .
Rails . error . record do
1 + '1' # raises TypeError
end
1 + 1 # This won't be executed
C o p y
I f n o e r r o r i s r a i s e d i n t h e b l o c k , R a i l s . e r r o r . r e c o r d
w i l l r e t u r n t h e r e s u l t o f t h e b l o c k .
Y o u c a n a l s o m a n u a l l y r e p o r t e r r o r s b y c a l l i n g R a i l s . e r r o r . r e p o r
t
:
begin
# code
rescue StandardError => e
Rails . error . report ( e )
end
e
R a i l s . e r r o r . r e p o r t ( e )
e n d
" > C o p y
A n y o p t i o n s y o u p a s s w i l l b e p a s s e d o n t h e e r r o r s u b s c r i b e r s .
A l l 3 r e p o r t i n g A P I s ( # h a n d l e
, #
r e c o r d
, a n d # r e p o r t
) s u p p o r t t h e f o l l o w i n g o p t i o n s , w h i c h a r e t h e n p a s s e d a l o n g t o a l l r e g i s t e r e d s u b s c r i b e r s :
● h a n d l e d
: a B o o l e a n
t o i n d i c a t e i f t h e e r r o r w a s h a n d l e d . T h i s i s s e t t o t r u e
b y d e f a u l t . # r e c
o r d
s e t s t h i s t o f a l s e
.
● s e v e r i t y
: a S y m b o l
d e s c r i b i n g t h e s e v e r i t y o f t h e e r r o r . E x p e c t e d v a l u e s a r e : : e r r o r
, : w a r n i n
g
, a n d : i n f o
. # h a n d l e
s e t s t h i s t o : w a r n i n g
, w h i l e # r e c o r d
s e t s i t t o : e r r o r
.
● c o n t e x t
: a H a s h
t o p r o v i d e m o r e c o n t e x t a b o u t t h e e r r o r , l i k e r e q u e s t o r u s e r d e t a i l s
● s o u r c e
: a S t r i n g
a b o u t t h e s o u r c e o f t h e e r r o r . T h e d e f a u l t s o u r c e i s " a p p l i c a t i o n "
. E r r o r s r e p o r t e d b y i n t e r n a l l i b r a r i e s m a y s e t o t h e r s o u r c e s ; t h e R e d i s c a c h e l i b r a r y m a y u s e " r e d i s _ c
a c h e _ s t o r e . a c t i v e _ s u p p o r t "
, f o r i n s t a n c e . Y o u r s u b s c r i b e r c a n u s e t h e s o u r c e t o i g n o r e e r r o r s y o u a r e n ' t i n t e r e s t e d i n .
Rails . error . handle ( context: { user_id: user . id }, severity: :info ) do
# ...
end
C o p y
W i t h R a i l s . e r r o r . h a n d l e
a n d R a i l
s . e r r o r . r e c o r d
, y o u c a n a l s o c h o o s e t o o n l y r e p o r t e r r o r s o f c e r t a i n c l a s s e s . F o r e x a m p l e :
Rails . error . handle ( IOError ) do
1 + '1' # raises TypeError
end
1 + 1 # TypeErrors are not IOErrors, so this will *not* be executed
C o p y
H e r e , t h e T y p e E r r o r
w i l l n o t b e c a p t u r e d b y t h e R a i l s e r r o r r e p o r t e r . O n l y i n s t a n c e s o f I O E r r o r
a n d i t s d e s c e n d a n t s w i l l b e r e p o r t e d . A n y o t h e r e r r o r s w i l l b e r a i s e d a s n o r m a l .
I n a d d i t i o n t o s e t t i n g c o n t e x t t h r o u g h t h e c o n t e x t
o p t i o n , y o u c a n u s e t h e # s e t _ c o n t e x t
A P I . F o r e x a m p l e :
Rails . error . set_context ( section: "checkout" , user_id: @user . id )
C o p y
A n y c o n t e x t s e t t h i s w a y w i l l b e m e r g e d w i t h t h e c o n t e x t
o p t i o n
Rails . error . set_context ( a: 1 )
Rails . error . handle ( context: { b: 2 }) { raise }
# The reported context will be: {:a=>1, :b=>2}
Rails . error . handle ( context: { b: 3 }) { raise }
# The reported context will be: {:a=>1, :b=>3}
1 , : b = > 2 }
R a i l s . e r r o r . h a n d l e ( c o n t e x t : { b : 3 } ) { r a i s e }
# T h e r e p o r t e d c o n t e x t w i l l b e : { : a = > 1 , : b = > 3 }
" > C o p y
E r r o r - r e p o r t i n g l i b r a r i e s c a n r e g i s t e r t h e i r s u b s c r i b e r s i n a R a
i l t i e
:
module MySdk
class Railtie < :: Rails :: Railtie
initializer "my_sdk.error_subscribe" do
Rails . error . subscribe ( MyErrorSubscriber . new )
end
end
end
C o p y
I f y o u r e g i s t e r a n e r r o r s u b s c r i b e r , b u t s t i l l h a v e o t h e r e r r o r m e c h a n i s m s l i k e a R a c k m i d d l e w a r e , y o u m a y e n d u p w i t h e r r o r s r e p o r t e d m u l t i p l e t i m e s . Y o u s h o u l d e i t h e r r e m o v e y o u r o t h e r m e c h a n i s m s o r a d j u s t y o u r r e p o r t f u n c t i o n a l i t y s o i t s k i p s r e p o r t i n g a n e x c e p t i o n i t h a s s e e n b e f o r e .
F e e d b a c k
Y o u ' r e e n c o u r a g e d t o h e l p i m p r o v e t h e q u a l i t y o f t h i s g u i d e .
P l e a s e c o n t r i b u t e i f y o u s e e a n y t y p o s o r f a c t u a l e r r o r s .
T o g e t s t a r t e d , y o u c a n r e a d o u r d o c u m e n t a t i o n c o n t r i b u t i o n s s e c t i o n .
Y o u m a y a l s o f i n d i n c o m p l e t e c o n t e n t o r s t u f f t h a t i s n o t u p t o d a t e .
P l e a s e d o a d d a n y m i s s i n g d o c u m e n t a t i o n f o r m a i n . M a k e s u r e t o c h e c k
E d g e G u i d e s f i r s t t o v e r i f y
i f t h e i s s u e s a r e a l r e a d y f i x e d o r n o t o n t h e m a i n b r a n c h .
C h e c k t h e R u b y o n R a i l s G u i d e s G u i d e l i n e s
f o r s t y l e a n d c o n v e n t i o n s .
I f f o r w h a t e v e r r e a s o n y o u s p o t s o m e t h i n g t o f i x b u t c a n n o t p a t c h i t y o u r s e l f , p l e a s e
o p e n a n i s s u e .
A n d l a s t b u t n o t l e a s t , a n y k i n d o f d i s c u s s i o n r e g a r d i n g R u b y o n R a i l s
d o c u m e n t a t i o n i s v e r y w e l c o m e o n t h e o f f i c i a l R u b y o n R a i l s F o r u m .
T h i s w o r k i s l i c e n s e d u n d e r a C r e a t i v e C o m m o n s A t t r i b u t i o n - S h a r e A l i k e 4 . 0 I n t e r n a t i o n a l L i c e n s e
" R a i l s " , " R u b y o n R a i l s " , a n d t h e R a i l s l o g o a r e t r a d e m a r k s o f D a v i d H e i n e m e i e r H a n s s o n . A l l r i g h t s r e s e r v e d .