ที่ AppSignal เรา Provide Error Tracking สำหรับ Ruby Applications เพื่อทำเช่นนั้น เรา Capture All Exceptions Applications Throw at Us และ Notify Developers As They Happen
ยากได้ Exception Handling Right ในบทความนี้เราจะ Explain How It Works Problems Bad Handling Can Cause และ How to Rescue Exceptions Properly
Rescuing Exceptions (การจับ Exception)
ด้วยการ Rescue Exceptions ใน Ruby คุณ Prevent Application From Crashing Moment Something Goes Wrong ด้วย Block begin .. rescue คุณ Specify Alternative Path for Application When Error Occurs
Also Possible Specify Which Exceptions Should Be Rescued When Specifying Exception Class All Subclasses of This Exception Also Captured
ใน Example ด้านบนเห็น Exception Errno::ENOENT Caught When Parent SystemCallError Being Rescued
Rescuing Too High Up in the Exception Chain (จับสูงเกินไปใน Exception Chain)
Important Not Rescue Exceptions Too High Up Exception Chain When You Do All Subclassed Exceptions Also Caught Making Rescue Block's Capture Too Generic
Program นี้ Read Config File Based on Argument Passed to Program
Error Message Says Couldn't Read Config File But Real Problem Was Typo in Code
Default Exception Class Caught by begin .. rescue Block คือ StandardError If Don't Pass Specific Class Ruby Rescue StandardError และ All Subclassed Errors NoMethodError เป็นหนึ่งใน Errors นี้
Rescue Specific Exception Class Help Prevent Unrelated Errors Accidentally Prompt Failure State Also Allows More Specific Custom Error Messages Helpful for End User
Rescuing Exception (การจับ Exception Class)
อาจยัง Tempting Rescue High Up Exception Chain Rescue All Errors App Can Raise Prevent Crashing (100% Uptime Here We Come!) แต Cause Lot of Problems
Exception Class คือ Main Exception Class in Ruby All Other Exceptions Subclasses of This Class If Exception Rescued All Errors Caught
Two Exceptions Most Apps Won't Want Rescue คือ SignalException และ SystemExit
SignalException Used When Outside Source Telling Application Stop Can Be OS When Wants Shut Down หรือ System Admin Wants Stop Application Example
SystemExit Used When exit Called from Ruby Application When Raised Developer Wants Application Stop Example
If Rescue Exception และ These Exceptions Raised While Application Currently Running begin ... rescue ... end Block Cannot Exit
Generally Bad Idea Rescue Exception Normal Situations When Rescuing Exception You'll Prevent SignalException และ SystemExit Function But Also LoadError, SyntaxError และ NoMemoryError To Name a Few Better Rescue More Specific Exceptions Instead
Failures in Tests (ความล้มเหลวใน Tests)
When Exception Rescued Using rescue Exception => e Other Things Beside Your Application Could Break Test Suite Could Actually Be Hiding Some Errors
In Minitest และ RSpec Assertions That Fail Raise Exception Inform You About Failed Assertion Failing Test When They Do Raise Own Custom Exceptions Subclassed from Exception
If Exception Rescued in Test or Application Code Could Be Silencing Assertion Failure
Expecting Exceptions (คาดหวัง Exceptions)
Some Code Meant to Raise Exceptions In Test Suite Possible Simply Silence Exception Order Test Not Fail When Raised
However This Doesn't Test If Exception Was Raised or Not When Exception Not Raised Your Test Won't Tell If Behavior Still Correct
Possible Assert If Exception Raised And If Not Which Exception Was
Re-raise Exception (Re-raise Exception)
Application Should Only Capture Exceptions As High Up Chain as Exception Class When Very Good Reason เช่น When Cleanup Involved Before Exiting Block Code Like Removing Temporary Files That Really Need Removed
One Recommendation When Absolutely Have to Rescue Exception Re-raise It After Done Handling Error This Way Ruby Exception Handling Decide Fate of Process Afterward
Unsure What to Rescue? (ไม่แน่ใจ Rescue อะไร)
As Mentioned Earlier Good to Be Specific What Errors to Rescue
When Unsure What Exceptions Operation Can Raise Rescuing StandardError Can Be Good Place to Start Run Code Different Scenarios See What Exceptions It Raises
Every Time Come Across New Exception Add Specific Rescue Cases for Those Exceptions or Its Relevant Parent Class Better to Be Specific What to Rescue Than Rescue Too Many Exceptions
This Concludes Primer on Exceptions Handling in Ruby Let Us Know @AppSignal If Want Know More Or Have Specific Question If Want Better Insight Where And How Often Exceptions Raised in Your App Give AppSignal a Try