| 1 | package WebService::MyGengo::Exception; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | |||
| 5 | with 'Throwable'; | ||
| 6 | |||
| 7 | use Carp; | ||
| 8 | use Scalar::Util qw(blessed); | ||
| 9 | |||
| 10 | use overload( '""', sub { shift->message } ); | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | WebService::MyGengo::Exception - Exception class for the WebService::MyGengo library. | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | eval { my $job = $client->get_job( "ARGH!" ) }; | ||
| 19 | if ( blessed($@) eq 'WebService::MyGengo::Exception' ) { | ||
| 20 | printf "Exception: %s\n", $@->message; | ||
| 21 | } | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | All exceptions thrown by operations in the WebService::MyGengo library will be | ||
| 26 | instances of this object. | ||
| 27 | |||
| 28 | B<Note:> Unless specified otherwise, errors from the transport layer | ||
| 29 | are not caught and wrapped with this class. | ||
| 30 | |||
| 31 | =head1 ATTRIBUTES | ||
| 32 | |||
| 33 | =head2 message (Str) | ||
| 34 | |||
| 35 | The error message. | ||
| 36 | |||
| 37 | Object stringification is overloaded to return this attribute. | ||
| 38 | |||
| 39 | eval { <something that throws an Exception> }; | ||
| 40 | print "Oops: '$@'\n"; # Prints the Exception message | ||
| 41 | |||
| 42 | =cut | ||
| 43 | has message => ( | ||
| 44 | is => 'ro' | ||
| 45 | , isa => 'Str' | ||
| 46 | , lazy => 1 | ||
| 47 | , default => "No message provided." | ||
| 48 | ); | ||
| 49 | |||
| 50 | =head2 stacktrace (Str) | ||
| 51 | |||
| 52 | A stacktrace from the point where the Exception was thrown. | ||
| 53 | |||
| 54 | The previous_exception attribute holds any previous exceptions. | ||
| 55 | |||
| 56 | See: L<Throwable> | ||
| 57 | |||
| 58 | =cut | ||
| 59 | has stacktrace => ( | ||
| 60 | is => 'ro' | ||
| 61 | , isa => 'Str' | ||
| 62 | , default => sub { Carp::longmess() } | ||
| 63 | ); | ||
| 64 | |||
| 65 | |||
| 66 | no Moose; | ||
| 67 | __PACKAGE__->meta->make_immutable; | ||
| 68 | |||
| 69 | 1; | ||
| 70 | |||
| 71 | =head1 AUTHOR | ||
| 72 | |||
| 73 | Nathaniel Heinrichs | ||
| 74 | |||
| 75 | =head1 LICENSE | ||
| 76 | |||
| 77 | Copyright (c) 2011, Nathaniel Heinrichs <nheinric-at-cpan.org>. | ||
| 78 | All rights reserved. | ||
| 79 | |||
| 80 | This library is free software. You can redistribute it and/or modify | ||
| 81 | it under the same terms as Perl itself. | ||
| 82 | |||
| 83 | =cut |