die if Catalyst app object is missing in push_error and get_errors
[catagits/Catalyst-Controller-DBIC-API.git] / lib / Catalyst / Controller / DBIC / API.pm
index 537bec2..99e11b7 100644 (file)
@@ -6,7 +6,7 @@ BEGIN { extends 'Catalyst::Controller::ActionRole'; }
 
 use CGI::Expand ();
 use DBIx::Class::ResultClass::HashRefInflator;
-use JSON;
+use JSON ();
 use Test::Deep::NoTest('eq_deeply');
 use MooseX::Types::Moose(':all');
 use Moose::Util;
@@ -932,6 +932,8 @@ push_error stores an error message into the stash to be later retrieved by L</en
 sub push_error
 {
     my ( $self, $c, $params ) = @_;
+    die 'Catalyst app object missing'
+        unless defined $c;
     my $error = 'unknown error';
     if (exists $params->{message}) {
         $error = $params->{message};
@@ -951,9 +953,24 @@ get_errors returns all of the errors stored in the stash
 sub get_errors
 {
     my ( $self, $c ) = @_;
+    die 'Catalyst app object missing'
+        unless defined $c;
     return $c->stash->{_dbic_crud_errors};
 }
 
+=method_protected has_errors
+
+returns returns true if errors are stored in the stash
+
+=cut
+
+sub has_errors {
+    my ( $self, $c ) = @_;
+    die 'Catalyst app object missing'
+        unless defined $c;
+    return exists $c->stash->{_dbic_crud_errors};
+}
+
 =head1 DESCRIPTION
 
 Easily provide common API endpoints based on your L<DBIx::Class> schema classes. Module provides both RPC and REST interfaces to base functionality. Uses L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize> to serialise response and/or deserialise request.