in reply to Re: Error handling in Mojolicious
in thread Error handling in Mojolicious
If one takes the DBI as an example typically RaiseError is used in conjunction with eval, or a module like Try::Tiny or TryCatch, to catch the exception that's been thrown and handle it.
Let's look at the example that marto gives: (Note he states "Feel free to add error checking etc")
#!/usr/bin/perl use strict; use warnings; use Mojo::UserAgent; my $url = 'https://en.wikipedia.org/wiki/File:ENIAC-changing_a_tube.jp +g'; my $ua = Mojo::UserAgent->new; my $dom = $ua->get( $url )->res->dom; my $target = $dom->at('#file > a:nth-child(1) > img:nth-child(1)')->at +tr('src'); $ua->get('https:' . $target)->res->save_to('target.jpg') ;
Is there any way to "raise error" so I can utilize something like a try/catch?
Added:
In the code above the $url (in the first ->get) might be bad; the css selector (used in the ->at) might be invalid for the html returned; the argument to the second ->get could be bad and the ->save_to could fail due to some IO error. Adding checks for all these possibilities would be rather painful.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Error handling in Mojolicious
by AnomalousMonk (Archbishop) on Jul 08, 2021 at 17:39 UTC | |
by davido (Cardinal) on Jul 08, 2021 at 19:00 UTC | |
Re^3: Error handling in Mojolicious
by Fletch (Bishop) on Jul 08, 2021 at 19:05 UTC |