perlexceptionmojoliciousdie

Mojo $c->reply->exception vs die?


They seem to do the same thing.

$c->reply->exception('foo');
return;

vs

die "foo\n";

Should I chose one over the other?


Solution

  • The latter is easier to read, can be trapped at the appropriate level if needed, and can include stack traces and context in development mode via automatic Mojo::Exception wrapping. It also will act appropriately in a promise handler or async sub, causing the promise to be rejected.

    You can see exactly how it's handled here ($next->() is the call to the dispatch chain): https://metacpan.org/source/SRI/Mojolicious-8.33/lib/Mojolicious.pm#L208-214