CatalystX/Errors.pm untested

filter: Statementstmt 0.0% 0 / 2 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 2 SCAR -
1 package CatalystX::Errors;
2  
3 our $VERSION = '0.001009';
4 $VERSION = eval $VERSION;
5
6 1;
7  
8 =head1 NAME
9  
10 CatalystX::Errors - Standard error handling with content negotiation and utilities
11  
12 =head1 SYNOPSIS
13  
14 Use in your application class
15  
16 package Example;
17  
18 use Catalyst;
19  
20 __PACKAGE__->setup_plugins([qw/Errors/]);
21 __PACKAGE__->setup();
22 __PACKAGE__->meta->make_immutable();
23  
24 And then you can use it in a controller (or anyplace where you have C<$c> context).
25  
26 package Example::Controller::Root;
27  
28 use Moose;
29 use MooseX::MethodAttributes;
30  
31 extends 'Catalyst::Controller';
32  
33 sub root :Chained(/) PathPart('') CaptureArgs(0) {}
34  
35 sub not_found :Chained(root) PathPart('') Args {
36 my ($self, $c, @args) = @_;
37 $c->detach_error(404);
38 }
39  
40 sub end :Does(RenderErrors) { }
41  
42 __PACKAGE__->config(namespace=>'');
43 __PACKAGE__->meta->make_immutable;
44  
45 =head1 DESCRIPTION
46  
47 A set of L<Catalyst> plugins, views and action roles to streamline handling of HTTP
48 error responses. Creates a standard way to return 4xx and 5xx HTTP errors using
49 proper content negotiation. Out of the box it support returning errors in HTML,
50 JSON and Text, with errors in a number of common languages. Patches to support more
51 languages and media types welcomed.
52  
53 I wrote this to encapsulate a common pattern I noted emerging in many of my applications.
54 Hopefully this will reduce boilerplate setup and speed your initial work. Also its never
55 bad to have rational ways to do common things.
56  
57 There is a very basic example application in C</example> for your review. This distribution
58 doesn't have a ton of test cases and I reserve the right to make breaking changes should a
59 better paradigm for this use case emerge. You should review the docs for each class in
60 this distribution for more info.
61  
62 You should see the plugin for API method documentation: L<Catalyst::Plugin::Errors>.
63  
64 B<NOTE>: The use case this is aimed at is proper handling of HTTP exception conditions resulting
65 from the actual HTTP routing and negotiation (page not found, not authorized, etc) and for
66 wrapping up any server side exceptions that get generated unexpectedly (such as data issues).
67 I don't really intend this to return error responses for something like invalid HTML forms for
68 example (althought you can use it for something like returning a generic 'bad request' in
69 response to an invalidly formed POST (such as an invalid CSRF token)).
70  
71 =head1 EXCEPTION CLASSES
72  
73 There are times when you need to throw an exception that should be properly interpreted at
74 the L<Catalyst> level. For example you have exception conditions being throw from code outside your
75 main web framework code (such as in L<DBIx::Class>). You can take two approaches. One is to capture and test $c->errors
76 in you final end action. Or you can consume the role L<CatalystX::Utils::DoesHttpException> and annotate
77 how you want L<Catalyst::ActionRole::RenderErrors> to convert it to a Catalyst response. This
78 is not either or BTW, and both approaches may be needed (for example you might be using a distribution
79 like L<DBIx::Class> that doesn't let you control the errors object). For example:
80  
81 package MyApp::Exception::NoCoffee;
82  
83 use Moose;
84 extends 'CatalystX::Utils::DoesHttpException';
85  
86 sub status_code { 418 }
87 sub error { 'Coffee not allowed' }
88 sub additional_headers { [ 'X-Error-Code' => '200101' ] }
89  
90 See L<CatalystX::Utils::DoesHttpException> and L<CatalystX::Utils::HttpException> for more details
91 and examples.
92  
93 B<Note>: It's best of the action role L<Catalyst::ActionRole::RenderErrors> is the last role added
94 to the action so that your error handling happens first.
95  
96 =head1 SEE ALSO
97
98 L<Catalyst>, L<Catalyst::Plugin::Errors>, L<Catalyst::View::Errors::HTML>,
99 L<Catalyst::View::Errors::Text>, L<Catalyst::View::Errors::JSON>,
100 L<Catalyst::ActionRole::RenderErrors>.
101  
102 =head1 AUTHOR
103
104 John Napiorkowski L<email:jjnapiork@cpan.org>
105
106 =head1 COPYRIGHT & LICENSE
107
108 Copyright 2022, John Napiorkowski L<email:jjnapiork@cpan.org>
109
110 This library is free software; you can redistribute it and/or modify it under
111 the same terms as Perl itself.
112  
113 =cut