Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Getting useful information from a thrown error

by talexb (Chancellor)
on Aug 23, 2022 at 21:12 UTC ( [id://11146339]=perlquestion: print w/replies, xml ) Need Help??

talexb has asked for the wisdom of the Perl Monks concerning the following question:

I'm using the excellent throws_ok test from Test::Exception, and it's doing a fine job in reporting that I got the right exception. The problem is, I'd like to see more information about the exception that's being reported. The code does

if ( $condition ) { throw 'BAD_THING_HAPPENED' => 'useful detailed information'; }
and my test (using throws_ok) looks for a thrown error with the pattern BAD_THING_HAPPENED. Great. But I'd love to be able to report the 'useful detailed information'. Is there any way to get that?

I'd even be happy to get the specific BAD_THING_HAPPENED error, because I can add the __LINE__ value to that, and the regexp will still work.

Alex / talexb / Toronto

Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Replies are listed 'Best First'.
Re: Getting useful information from a thrown error
by stevieb (Canon) on Aug 23, 2022 at 21:39 UTC

    I misread the OP. The below can be ignored as it doesn't equate to the actual problem.

    You just want to add your own custom fail report? Here's what I do at work:

    throws_ok { $Keys->add_domain_mapping } qr/requires a.*domain/, "add_domain_mapping() croaks if no domain sent in ok"; throws_ok { $Keys->add_domain_mapping('blah.com') } qr/requires a.*selector/, "add_domain_mapping() croaks if no selector sent in ok";
Re: Getting useful information from a thrown error
by Anonymous Monk on Aug 23, 2022 at 21:23 UTC
    What is throw exactly?

      The throw I'm using causes an exception; so typically you use it in code like this:

      try { something_that_might_blow_up(); } catch { /POSSIBLE_ERROR/ && print "FATAL:A bad thing happened\n"; };
      The module Try::Tiny is where this comes from.

      Alex / talexb / Toronto

      Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11146339]
Approved by johngg
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-26 02:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found