Title: [opsview] [10533] Merge from DEV-professional 10532: Error messages placed in $c->error() instead of stash
Modified: trunk/opsview-web/lib/Opsview/Web/Controller/REST/Acknowledge.pm
===================================================================
--- trunk/opsview-web/lib/Opsview/Web/Controller/REST/Acknowledge.pm	2012-10-18 20:46:09 UTC (rev 10532)
+++ trunk/opsview-web/lib/Opsview/Web/Controller/REST/Acknowledge.pm	2012-10-18 20:59:47 UTC (rev 10533)
@@ -68,11 +68,8 @@
 
     if ( $_ = $c->stash->{validation_errors} ) {
         $c->res->status(403);
-        $c->stash(
-            "rest" => {
-                message => "Error validating acknowledge parameters",
-                detail  => join( "; ", @$_ )
-            }
+        $c->error(
+            "Error validating acknowledge parameters: " . join( "; ", @$_ )
         );
         $c->detach;
     }
@@ -83,9 +80,7 @@
     # If no objects, error
     if ( !@{ $c->stash->{hosts} } && !@{ $c->stash->{services} } ) {
         $c->res->status(403);
-        $c->stash(
-            "rest" => { message => "No objects chosen for acknowledgements" }
-        );
+        $c->error( "No objects chosen for acknowledgements" );
         $c->detach;
     }
 
@@ -95,12 +90,9 @@
     my @all_errors = @{ $c->stash->{all_errors} };
     if (@all_errors) {
         $c->res->status(403);
-        $c->stash(
-            "rest" => {
-                message =>
-                  "Errors setting acknowledgements - some objects may have been acknowledged",
-                detail => join( "", @all_errors ),
-            }
+        $c->error(
+            "Errors setting acknowledgements - some objects may have been acknowledged: "
+              . join( "", @all_errors )
         );
         $c->detach;
     }

Modified: trunk/opsview-web/lib/Opsview/Web/Controller/REST/Config/Monitoringserver.pm
===================================================================
--- trunk/opsview-web/lib/Opsview/Web/Controller/REST/Config/Monitoringserver.pm	2012-10-18 20:46:09 UTC (rev 10532)
+++ trunk/opsview-web/lib/Opsview/Web/Controller/REST/Config/Monitoringserver.pm	2012-10-18 20:59:47 UTC (rev 10533)
@@ -36,9 +36,7 @@
 sub no_method {
     my ( $self, $c ) = @_;
     $c->res->status(405);
-    $c->stash(
-        "rest" => { message => "Method " . $c->req->method . " not available" }
-    );
+    $c->error( "Method " . $c->req->method . " not available" );
     $c->detach;
 }
 

Modified: trunk/opsview-web/lib/Opsview/Web/Controller/REST/Downtime.pm
===================================================================
--- trunk/opsview-web/lib/Opsview/Web/Controller/REST/Downtime.pm	2012-10-18 20:46:09 UTC (rev 10532)
+++ trunk/opsview-web/lib/Opsview/Web/Controller/REST/Downtime.pm	2012-10-18 20:59:47 UTC (rev 10533)
@@ -70,9 +70,7 @@
         && !@{ $c->stash->{services} } )
     {
         $c->res->status(403);
-        $c->stash(
-            "rest" => { message => "No objects chosen for downtime deletion" }
-        );
+        $c->error( "No objects chosen for downtime deletion" );
         $c->detach;
     }
 
@@ -87,12 +85,9 @@
     my @all_errors = @{ $c->stash->{all_errors} };
     if (@all_errors) {
         $c->res->status(403);
-        $c->stash(
-            "rest" => {
-                message =>
-                  "Errors deleting downtime - some objects may have downtime deleted",
-                detail => join( "", @all_errors ),
-            }
+        $c->error(
+            "Errors deleting downtime - some objects may have downtime deleted: "
+              . join( "", @all_errors )
         );
         $c->detach;
     }
@@ -109,11 +104,7 @@
 
     if ( $_ = $c->stash->{validation_errors} ) {
         $c->res->status(403);
-        $c->stash(
-            "rest" => {
-                message => "Error validating downtime parameters",
-                detail  => join( "; ", @$_ )
-            }
+        $c->error( "Error validating downtime parameters: " . join( "; ", @$_ )
         );
         $c->detach;
     }
@@ -127,9 +118,7 @@
         && !@{ $c->stash->{services} } )
     {
         $c->res->status(403);
-        $c->stash(
-            "rest" => { message => "No objects chosen for downtime creation" }
-        );
+        $c->error( "No objects chosen for downtime creation" );
         $c->detach;
     }
 
@@ -140,12 +129,9 @@
     my @all_errors = @{ $c->stash->{all_errors} };
     if (@all_errors) {
         $c->res->status(403);
-        $c->stash(
-            "rest" => {
-                message =>
-                  "Errors setting downtime - some objects may have downtime set",
-                detail => join( "", @all_errors ),
-            }
+        $c->error(
+            "Errors setting downtime - some objects may have downtime set: "
+              . join( "", @all_errors )
         );
         $c->detach;
     }

Modified: trunk/opsview-web/lib/Opsview/Web/Controller/REST/Recheck.pm
===================================================================
--- trunk/opsview-web/lib/Opsview/Web/Controller/REST/Recheck.pm	2012-10-18 20:46:09 UTC (rev 10532)
+++ trunk/opsview-web/lib/Opsview/Web/Controller/REST/Recheck.pm	2012-10-18 20:59:47 UTC (rev 10533)
@@ -46,8 +46,8 @@
 }
 
 # Lists all services that can be rechecked based on user permissions
-sub recheck_GET : Does(ACL) : AllowedRole(ACTIONSOME) : AllowedRole(ACTIONALL) :
-  ACLDetachTo(/access_denied) {
+sub recheck_GET : Does(ACL) : AllowedRole(ACTIONSOME) : AllowedRole(ACTIONALL)
+  : ACLDetachTo(/access_denied) {
     my ( $self, $c ) = @_;
 
     my $args = $c->stash->{status_params} || $c->req->params;
@@ -79,7 +79,7 @@
     # If no objects, error
     if ( !@{ $c->stash->{hosts} } && !@{ $c->stash->{services} } ) {
         $c->res->status(403);
-        $c->stash( "rest" => { message => "No objects chosen for recheck" } );
+        $c->error( "No objects chosen for recheck" );
         $c->detach;
     }
 
@@ -89,12 +89,9 @@
     my @all_errors = @{ $c->stash->{all_errors} };
     if (@all_errors) {
         $c->res->status(403);
-        $c->stash(
-            "rest" => {
-                message =>
-                  "Errors requesting recheck - some objects may have been rechecked",
-                detail => join( "", @all_errors ),
-            }
+        $c->error(
+            "Errors requesting recheck - some objects may have been rechecked: "
+              . join( "", @all_errors )
         );
         $c->detach;
     }

Modified: trunk/opsview-web/lib/Opsview/Web/Controller/REST/Runtime/Performancemetric.pm
===================================================================
--- trunk/opsview-web/lib/Opsview/Web/Controller/REST/Runtime/Performancemetric.pm	2012-10-18 20:46:09 UTC (rev 10532)
+++ trunk/opsview-web/lib/Opsview/Web/Controller/REST/Runtime/Performancemetric.pm	2012-10-18 20:59:47 UTC (rev 10533)
@@ -71,7 +71,7 @@
 
         if ( !$coltype ) {
             $c->res->status(400);
-            $c->stash( "rest" => { message => "Bad type: $type" } );
+            $c->error( "Bad type: $type" );
 
             # This is a return() rather than a detach because this is called from /search/performancemetric
             # which uses this return code to know a failure occurred

Modified: trunk/opsview-web/lib/Opsview/Web/Controller/REST/Status/Viewport.pm
===================================================================
--- trunk/opsview-web/lib/Opsview/Web/Controller/REST/Status/Viewport.pm	2012-10-18 20:46:09 UTC (rev 10532)
+++ trunk/opsview-web/lib/Opsview/Web/Controller/REST/Status/Viewport.pm	2012-10-18 20:59:47 UTC (rev 10533)
@@ -83,7 +83,7 @@
     my $found = $c->stash->{rs}->search( { keyword => $viewport } )->count;
     unless ($found) {
         $c->res->status(403);
-        $c->stash( "rest" => { message => "No access to keyword $viewport" } );
+        $c->error( "No access to keyword $viewport" );
         $c->detach;
     }
 

Modified: trunk/opsview-web/lib/Opsview/Web/Controller/REST/Status.pm
===================================================================
--- trunk/opsview-web/lib/Opsview/Web/Controller/REST/Status.pm	2012-10-18 20:46:09 UTC (rev 10532)
+++ trunk/opsview-web/lib/Opsview/Web/Controller/REST/Status.pm	2012-10-18 20:59:47 UTC (rev 10533)
@@ -56,8 +56,8 @@
     $c->stash( 'rest' => $list_result );
 }
 
-sub status_POST : Does(ACL) : AllowedRole(ACTIONALL) : AllowedRole(ACTIONSOME) :
-  ACLDetachTo(/access_denied) {
+sub status_POST : Does(ACL) : AllowedRole(ACTIONALL) : AllowedRole(ACTIONSOME)
+  : ACLDetachTo(/access_denied) {
     my ( $self, $c ) = @_;
 
     # Validate status parameters
@@ -65,11 +65,7 @@
 
     if ( $_ = $c->stash->{validation_errors} ) {
         $c->res->status(403);
-        $c->stash(
-            "rest" => {
-                message => "Error validating status parameters",
-                detail  => join( "; ", @$_ )
-            }
+        $c->error( "Error validating status parameters: " . join( "; ", @$_ )
         );
         $c->detach;
     }
@@ -80,7 +76,7 @@
     # If no objects, error
     if ( !@{ $c->stash->{hosts} } && !@{ $c->stash->{services} } ) {
         $c->res->status(403);
-        $c->stash( "rest" => { message => "No objects chosen for status" } );
+        $c->error( "No objects chosen for status" );
         $c->detach;
     }
 
@@ -90,12 +86,9 @@
     my @all_errors = @{ $c->stash->{all_errors} };
     if (@all_errors) {
         $c->res->status(403);
-        $c->stash(
-            "rest" => {
-                message =>
-                  "Errors setting status - some objects may have been checked",
-                detail => join( "", @all_errors ),
-            }
+        $c->error(
+            "Errors setting status - some objects may have been checked: "
+              . join( "", @all_errors )
         );
         $c->detach;
     }

Modified: trunk/opsview-web/lib/Opsview/Web/Controller/REST.pm
===================================================================
--- trunk/opsview-web/lib/Opsview/Web/Controller/REST.pm	2012-10-18 20:46:09 UTC (rev 10532)
+++ trunk/opsview-web/lib/Opsview/Web/Controller/REST.pm	2012-10-18 20:59:47 UTC (rev 10533)
@@ -79,11 +79,7 @@
 
         # This catches all requests going to a wrong URL
         $c->res->status(404);
-        $c->stash(
-            "rest" => {
-                message => "Request not found for " . $c->uri_for_path(@args),
-            }
-        );
+        $c->error( "Request not found for " . $c->uri_for_path(@args) );
     }
     else {
         my $api_version = "";
@@ -160,11 +156,7 @@
         };
         if ( $count >= 5 ) {
             $c->res->status(503);
-            $c->stash(
-                "rest" => {
-                    message => "Error creating session token after 5 attempts"
-                }
-            );
+            $c->error( "Error creating session token after 5 attempts" );
             $c->detach;
         }
     }
@@ -269,7 +261,7 @@
             "REST authenticate error: $error. Username: '$username', Token: '$token'"
         );
         $c->res->status(401);
-        $c->stash( "rest" => { message => $error } );
+        $c->error($error);
         return 0;
     }
 
@@ -343,7 +335,7 @@
     }
     else {
         $c->res->status(401);
-        $c->stash( "rest" => { message => "Invalid login" } );
+        $c->error( "Invalid login" );
     }
 }
 
@@ -361,8 +353,7 @@
 
     unless ($username) {
         $c->res->status(401);
-        $c->stash(
-            "rest" => { message => "No username specified for REST login" } );
+        $c->error( "No username specified for REST login" );
         $c->detach;
     }
 
@@ -391,11 +382,7 @@
     }
     else {
         $c->res->status(401);
-        $c->stash(
-            "rest" => {
-                message => "Invalid login using authtkt",
-                detail  => $c->stash->{login_error}
-            }
+        $c->error( "Invalid login using authtkt: " . $c->stash->{login_error}
         );
         Opsview::Auditlog->system(
             "Invalid login using REST authtkt: " . $c->stash->{login_error}
@@ -530,8 +517,8 @@
 
     # Set master status based on server_status value
     $nodestatus_by_name->{ $master->host->name } =
-      $map_server_status_to_nagios_state->{ $restdata->{reload}
-          ->{server_status} };
+      $map_server_status_to_nagios_state->{ $restdata->{reload}->{server_status}
+      };
 
     # Parse through to pick out Slave-node status
     foreach my $host ( @{ $status->{list} } ) {
@@ -547,13 +534,13 @@
     foreach my $host ( @{ $perfdata->{list} } ) {
         foreach my $service ( @{ $host->{services} } ) {
             if (
-                $possible_nagios_status_servicename_lc->{ lc( $service->{name} )
-                } )
+                $possible_nagios_status_servicename_lc->{
+                    lc( $service->{name} ) } )
             {
                 foreach my $metric ( @{ $service->{metrics} || [] } ) {
                     if ( $metric->{name} eq "avgactsvclatency" ) {
-                        $nodeperformance_by_name->{ $host->{name} }->{latency} =
-                          $metric->{value};
+                        $nodeperformance_by_name->{ $host->{name} }->{latency}
+                          = $metric->{value};
                     }
                     if ( $metric->{name} eq "avgactsvcexect" ) {
                         $nodeperformance_by_name->{ $host->{name} }
@@ -603,8 +590,9 @@
                 num_passive_service_results_hour => undef,
             };
             if ( exists $nodestatus_by_name->{$nodename} ) {
-                $info_per_slavenode->{status} = $map_status_to_nagios_code
-                  ->{ $nodestatus_by_name->{$nodename} };
+                $info_per_slavenode->{status} =
+                  $map_status_to_nagios_code->{ $nodestatus_by_name->{$nodename}
+                  };
             }
             if ( exists $nodeperformance_by_name->{$nodename} ) {
                 if ( exists $nodeperformance_by_name->{$nodename}->{latency} ) {

Modified: trunk/opsview-web/lib/Opsview/Web/Controller/Root.pm
===================================================================
--- trunk/opsview-web/lib/Opsview/Web/Controller/Root.pm	2012-10-18 20:46:09 UTC (rev 10532)
+++ trunk/opsview-web/lib/Opsview/Web/Controller/Root.pm	2012-10-18 20:59:47 UTC (rev 10533)
@@ -602,7 +602,13 @@
 
             # Choose 400 because this is some client error
             $c->res->status(400);
+
+            # This error is automatically sorted in /access_denied, but we need to catch it
+            # to stop going to a hard error page
         }
+        elsif ( $errors[0] eq "Access denied" ) {
+            $c->clear_errors;
+        }
         else {
             $c->log->error( "Errors encountered: " . join( ', ', @errors ) );
             Opsview::Auditlog->system(
@@ -895,7 +901,7 @@
         }
     );
     $c->res->status(403);
-    $c->stash( "rest" => { message => "Access denied" } );
+    $c->error( "Access denied" );
     $c->detach;
 }
 

Modified: trunk/opsview-web/lib/Opsview/Web/Controller/Search.pm
===================================================================
--- trunk/opsview-web/lib/Opsview/Web/Controller/Search.pm	2012-10-18 20:46:09 UTC (rev 10532)
+++ trunk/opsview-web/lib/Opsview/Web/Controller/Search.pm	2012-10-18 20:59:47 UTC (rev 10533)
@@ -151,7 +151,9 @@
     }
 
     if ( !$c->forward("/rest/runtime/performancemetric/search") ) {
-        $c->stash( error => $c->stash->{rest}->{message} );
+        my $errmsg = $c->error->[0];
+        $c->clear_errors;
+        $c->stash( error => $errmsg );
         $c->detach( "/soft_error" );
     }
 

Modified: trunk/opsview-web/lib/Opsview/Web/ControllerBase/Config.pm
===================================================================
--- trunk/opsview-web/lib/Opsview/Web/ControllerBase/Config.pm	2012-10-18 20:46:09 UTC (rev 10532)
+++ trunk/opsview-web/lib/Opsview/Web/ControllerBase/Config.pm	2012-10-18 20:59:47 UTC (rev 10533)
@@ -49,12 +49,7 @@
         }
         catch {
             $c->res->status(400);
-            $c->stash(
-                "rest" => {
-                    message => "Error when parsing data",
-                    detail  => $_
-                }
-            );
+            $c->error( "Error when parsing data: $_" );
             $c->detach;
         };
         $c->req->data($data);
@@ -72,16 +67,18 @@
 
 };
 
-before 'end' => sub {
-    my ( $self, $c ) = @_;
+# I don't think this is required anymore, because detail is stringified into
+# $c->error() now
+#before 'end' => sub {
+#    my ( $self, $c ) = @_;
+#
+#    # Any rest data with message->{detail} as an object will be stringified
+#    my $rest = $c->stash->{rest};
+#    if ( exists $rest->{detail} && ref( $rest->{detail} ) ne "" ) {
+#        $rest->{detail} = "" . $rest->{detail};
+#    }
+#};
 
-    # Any rest data with message->{detail} as an object will be stringified
-    my $rest = $c->stash->{rest};
-    if ( exists $rest->{detail} && ref( $rest->{detail} ) ne "" ) {
-        $rest->{detail} = "" . $rest->{detail};
-    }
-};
-
 sub list : Chained("objecttype") : PathPart("") : Args(0) : ActionClass('REST')
 {
 }
@@ -155,12 +152,7 @@
         }
         catch {
             $c->res->status(400);
-            $c->stash(
-                "rest" => {
-                    message => "Error executing search",
-                    detail  => $_
-                }
-            );
+            $c->error( "Error executing search: $_" );
             $c->detach;
         };
         $result->{list} = \@items;
@@ -224,12 +216,8 @@
             }
             catch {
                 $c->res->status(400);
-                $c->stash(
-                    "rest" => {
-                        message =>
-                          "Rollback: Error when trying to delete old objects in a synchronise",
-                        detail => $_
-                    }
+                $c->error(
+                    "Rollback. Error when trying to delete old objects in a synchronise: $_"
                 );
                 $c->detach;
             };
@@ -252,7 +240,7 @@
 
     unless ($object) {
         $c->res->status(404);
-        $c->stash( "rest" => { message => "No object found with id=$id" } );
+        $c->error( "No object found with id=$id" );
         $c->detach;
     }
 
@@ -310,12 +298,7 @@
     }
     catch {
         $c->res->status(400);
-        $c->stash(
-            "rest" => {
-                message => "Error when deleting object",
-                detail  => $_
-            }
-        );
+        $c->error( "Error when deleting object: $_" );
         $c->detach;
     };
 
@@ -328,12 +311,12 @@
     my $data = ""
     unless ($data) {
         $c->res->status(400);
-        $c->stash( "rest" => { message => $message_if_data_not_set } );
+        $c->error($message_if_data_not_set);
         $c->detach;
     }
     unless ( ref $data eq "HASH" ) {
         $c->res->status(400);
-        $c->stash( "rest" => { message => "Data not in correct format" } );
+        $c->error( "Data not in correct format" );
         $c->detach;
     }
 
@@ -371,15 +354,16 @@
     catch {
         $c->res->status(400);
         my $message;
-        my $output = { detail => $_ };
+        my $detail = $_;
+        my $output = {};
         if ($list_object_number) {
             $output->{objects_updated} = 0;
             if ( $options->{create_only} ) {
-                $message = "Rollback. Error trying to create object: "
+                $message = "Rollback. Error trying to create object "
                   . $list_object_number;
             }
             else {
-                $message = "Rollback. Error trying to synchronise object: "
+                $message = "Rollback. Error trying to synchronise object "
                   . $list_object_number;
             }
         }
@@ -391,8 +375,8 @@
                 $message = "Error trying to synchronise object";
             }
         }
-        $output->{message} = $message;
         $c->stash( "rest" => $output );
+        $c->error( $message . ": " . $detail );
         $c->detach;
     };
     unless ($list_object_number) {
@@ -411,12 +395,7 @@
     }
     catch {
         $c->res->status(400);
-        $c->stash(
-            "rest" => {
-                message => "Error when parsing json_filter",
-                detail  => $_
-            }
-        );
+        $c->error( "Error when parsing json_filter: $_" );
         $c->detach;
     };
 

Modified: trunk/opsview-web/lib/Opsview/Web/ControllerBase/REST.pm
===================================================================
--- trunk/opsview-web/lib/Opsview/Web/ControllerBase/REST.pm	2012-10-18 20:46:09 UTC (rev 10532)
+++ trunk/opsview-web/lib/Opsview/Web/ControllerBase/REST.pm	2012-10-18 20:59:47 UTC (rev 10533)
@@ -66,6 +66,21 @@
 before 'end' => sub {
     my ( $self, $c ) = @_;
 
+    # Convert errors into messages - only converts the first error
+    # Expects error to be in the style of:
+    # MESSAGE_AS_EXPECTED: DETAIL_STUFF_TO_TALK_ABOUT
+    if ( my $err = $c->error->[0] ) {
+        my ( $message, undef, $detail ) = ( $err =~ m/^(.*?)(:\s*(.*))?$/s );
+        my $error_hash = { message => $message, };
+        $error_hash->{detail} = $detail if ($detail);
+        if ( $c->stash->{rest} ) {
+            $error_hash =
+              Catalyst::Utils::merge_hashes( $c->stash->{rest}, $error_hash );
+        }
+        $c->stash->{rest} = $error_hash;
+        $c->clear_errors;
+    }
+
     # Convert failures from C::A::REST which are strings into JSON
     if ( $c->res->status == 400 ) {
         my $content = $c->res->body;

Modified: trunk/opsview-web/t/650-api2-host.t
===================================================================
--- trunk/opsview-web/t/650-api2-host.t	2012-10-18 20:46:09 UTC (rev 10532)
+++ trunk/opsview-web/t/650-api2-host.t	2012-10-18 20:59:47 UTC (rev 10533)
@@ -43,7 +43,7 @@
 $res_data = eval $ua->content;
 is(
     $res_data->{message},
-    "Rollback. Error trying to synchronise object: 2",
+    "Rollback. Error trying to synchronise object 2",
     "Got correct error"
 ) || diag( "Full content: " . $ua->content );
 is( $res_data->{objects_updated}, 0, "No objects changed" );

Modified: trunk/opsview-web/t/650-api2-sync.t
===================================================================
--- trunk/opsview-web/t/650-api2-sync.t	2012-10-18 20:46:09 UTC (rev 10532)
+++ trunk/opsview-web/t/650-api2-sync.t	2012-10-18 20:59:47 UTC (rev 10533)
@@ -52,7 +52,7 @@
 $res_data = eval $ua->content;
 is(
     $res_data->{message},
-    "Rollback: Error when trying to delete old objects in a synchronise",
+    "Rollback. Error when trying to delete old objects in a synchronise",
     "Delete error caught"
 ) || diag( "Full content: " . $ua->content );
 like(

Modified: trunk/opsview-web/t/650-api2.t
===================================================================
--- trunk/opsview-web/t/650-api2.t	2012-10-18 20:46:09 UTC (rev 10532)
+++ trunk/opsview-web/t/650-api2.t	2012-10-18 20:59:47 UTC (rev 10533)
@@ -755,7 +755,7 @@
 $res_data = eval $ua->content;
 is(
     $res_data->{message},
-    "Rollback. Error trying to create object: 2",
+    "Rollback. Error trying to create object 2",
     "Correct error message"
 );
 like(

_______________________________________________
Opsview-checkins mailing list
[email protected]
http://lists.opsview.org/lists/listinfo/opsview-checkins

Reply via email to