NAME

Ouch - Exceptions that don't hurt.

SYNOPSIS

 use Ouch;

 eval { ouch 404, 'File not found.'; };

 if (kiss 404) {
   check_elsewhere();
 }

 say $@;           # These two lines do the
 say $@->message;  # same thing.

DESCRIPTION

Ouch provides a class for exception handling that doesn't require a lot of boilerplate, nor any up front definition. If Exception::Class is working for you, great! But if you want something that is faster, easier to use, requires less typing, and has no prereqs, but still gives you much of that same functionality, then Ouch is for you.

Why another exception handling module?

It really comes down to Carp isn't enough for me, and Exception::Class does what I want but makes me type way too much. Also, I tend to work on a lot of protocol-based systems that use error codes (HTTP, FTP, SMTP, JSON-RPC) rather than error classes, so that feels more natural to me. Consider the difference between these:

Ouch

 use Ouch;
 ouch 404, 'File not found.', 'file';

Exception::Class

 use Exception::Class (
    'FileNotFound' => {
        fields  => [ 'code', 'field' ],
    },
 );
 FileNotFound->throw( error => 'File not found.', code => 404, field => 'file' );

And if you want to catch the exception you're looking at:

Ouch

 if (kiss 404) {
   # do something
 }

Exception::Class

 my $e;
 if ($e = Exception::Class->caught('FileNotFound')) {
   # do something
 }

Those differences may not seem like a lot, but over any substantial program with lots of exceptions it can become a big deal.

Usage

Most of the time, all you need to do is:

 ouch $code, $message, $data;
 ouch -32700, 'Parse error.', $request; # JSON-RPC 2.0 error
 ouch 441, 'You need to specify an email address.', 'email'; # form processing error

You can also go long form if you prefer:

 die Ouch->new($code, $message, $data);

Functional Interface

ouch

Some nice sugar instead of using the object oriented interface.

 ouch 2121, 'Did not do the big thing.';
code

An error code. An integer representing error type. Try to stick to codes used in whatever domain you happen to be working in. HTTP Status codes. JSON-RPC error codes, etc.

message

A human readable error message.

data

Optional. Anything you want to attach to the exception to help a developer catching it decide what to do. For example, if you're doing form processing, you might want this to be the name of the field that caused the exception.

kiss

Some nice sugar to trap an Ouch.

 if (kiss $code) {
    # make it go
 }
code

The code you're looking for.

exception

Optional. If you like you can pass the exception into kiss. If not, it will just use whatever is in $@. You might want to do this if you've saved the exception before running another eval, for example.

bleep

A little sugar to make exceptions human friendly. Returns a clean error message from any exception, including an Ouch.

 File not found.

Rather than:

 File not found. at /Some/File.pm line 63.
exception

Optional. If you like you can pass the exception into bleep. If not, it will just use whatever is in $@.

Object-Oriented Interface

new

Constructor for the object-oriented interface. Takes the same parameters as ouch.

 Ouch->new($code, $message, $data);

scalar

Returns the scalar form of the error message:

 Crap! at /Some/File.pm line 43.

Just as if you had done:

 die 'Crap!';

Rather than:

 ouch $code, 'Crap!'; 

trace

Call this if you want the full stack trace that lead up to the ouch.

hashref

Returns a formatted hash reference of the exception, which can be useful for handing off to a serializer like JSON.

 {
   code     => $code,
   message  => $message,
   data     => $data,
 }

code

Returns the code passed into the constructor.

message

Returns the messsage passed into the constructor.

data

Returns the data passed into the constructor.

SUPPORT

Repository

http://github.com/rizen/Ouch

Bug Reports

http://github.com/rizen/Ouch/issues

SEE ALSO

If you're looking for something lighter, check out Carp that ships with Perl. Or if you're looking for something heavier check out Exception::Class.

AUTHOR

JT Smith <jt_at_plainblack_dot_com>

LEGAL

Ouch is Copyright 2011 Plain Black Corporation (http://www.plainblack.com) and is licensed under the same terms as Perl itself.