[Jifty-commit] r749 - in jifty/trunk: . doc lib/Jifty share/web/templates/__jifty share/web/templates/__jifty/error share/web/templates/__jifty/error/_elements

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Mon Mar 27 21:30:39 EST 2006


Author: alexmv
Date: Mon Mar 27 21:30:38 2006
New Revision: 749

Added:
   jifty/trunk/doc/edit-in-place
   jifty/trunk/share/web/templates/__jifty/error/_elements/
   jifty/trunk/share/web/templates/__jifty/error/_elements/error_text
   jifty/trunk/share/web/templates/__jifty/error/_elements/wrapper
   jifty/trunk/share/web/templates/__jifty/error/error.css
   jifty/trunk/share/web/templates/__jifty/error/mason_internal_error
Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/Action/Devel/FileEditor.pm
   jifty/trunk/lib/Jifty/Dispatcher.pm
   jifty/trunk/lib/Jifty/Response.pm
   jifty/trunk/share/web/templates/__jifty/edit_file
   jifty/trunk/share/web/templates/__jifty/error/dhandler

Log:
 r11910 at zoq-fot-pik:  chmrr | 2006-03-27 21:30:28 -0500
  * Devel mode error handling and file editing


Added: jifty/trunk/doc/edit-in-place
==============================================================================
--- (empty file)
+++ jifty/trunk/doc/edit-in-place	Mon Mar 27 21:30:38 2006
@@ -0,0 +1,22 @@
+# These are dispatcher rules that halos and error handling need to be
+# able to edit files in place.  These should become a plugin of some
+# sort, when we have that infrastructure.
+
+before '*', run {
+    Jifty->web->deny_actions(qw/Jifty::Action::Devel/)
+      unless Jifty->config->framework('DevelMode');
+};
+
+on qr'^/__jifty/edit/(.*?)/(.*)$', run {
+    my $editor = Jifty->web->new_action(
+        class     => 'Jifty::Action::Devel::FileEditor',
+        moniker   => 'editpage',
+        arguments => {
+            source_path => $2,
+            file_type   => $1,
+        }
+    );
+
+    set editor => $editor;
+    show '/__jifty/edit_file';
+};

Modified: jifty/trunk/lib/Jifty/Action/Devel/FileEditor.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Action/Devel/FileEditor.pm	(original)
+++ jifty/trunk/lib/Jifty/Action/Devel/FileEditor.pm	Mon Mar 27 21:30:38 2006
@@ -66,7 +66,6 @@
 
 The actual content of the file we're editing.
 
-
 =back
 
 =cut
@@ -75,8 +74,7 @@
 sub arguments {
     my $self = shift;
 
-    {   path      => { type => 'text', constructor => 1 },
-        file_type => {
+    {   file_type => {
             default      => 'mason_component',
             render_as    => 'Select',
             valid_values => [qw/mason_component library/],
@@ -93,8 +91,8 @@
 
 =head2 get_default_content
 
-Finds the version of the C<source_path> (of type C<file_type>) and loads it into C<content>.
-
+Finds the version of the C<source_path> (of type C<file_type>) and
+loads it into C<content>.
 
 =cut
 
@@ -108,26 +106,35 @@
     my $out = '';
     my %cfg = Jifty->handler->mason_config;
     
-    my $local_template_base;
-    foreach my $item (@{$cfg{comp_root}}) {
-        $local_template_base = $item->[1] if ($item->[0] eq 'application');
-        my $qualified_path = File::Spec->catfile($item->[1],$path);
-        if (-f $qualified_path and -r $qualified_path)  {
-            $self->argument_value(qualified_path => $qualified_path);
-            my $filehandle;
-            open ($filehandle, "<$qualified_path")||die "Couldn't read $qualified_path: $!";
-            $out = join('',<$filehandle>);
-            close($filehandle);
-            last; # We want the first match
+    my($local_template_base, $qualified_path);
+    if ($type eq "mason_component") {
+        foreach my $item (@{$cfg{comp_root}}) {
+            $local_template_base = $item->[1] if $item->[0] eq 'application';
+            $qualified_path = File::Spec->catfile($item->[1],$path);
+            # We want the first match
+            last if -f $qualified_path and -r $qualified_path;
+            undef $qualified_path;
         }
+        $self->argument_value(destination_path => File::Spec->catfile($local_template_base, $path));
+    } else {
+        $qualified_path = "/$path" if -f "/$path" and -r "/$path";
+        $self->argument_value(destination_path => $qualified_path);
+    }
+
+    if ($qualified_path) {
+        local $/;
+        my $filehandle;
+        open ($filehandle, "<$qualified_path")||die "Couldn't read $qualified_path: $!";
+        $self->argument_value(content => <$filehandle>);
+        close($filehandle);
     }
-    $self->argument_value(content => $out);
-    $self->argument_value(destination_path => File::Spec->catfile($local_template_base, $path));
 }
 
 =head2 validate_destination_path PATH
 
-Returns true if the user can write to the directory C<PATH>. False otherwise. Should be refactored to a C<path_writable> routine and a trivial validator.
+Returns true if the user can write to the directory C<PATH>. False
+otherwise. Should be refactored to a C<path_writable> routine and a
+trivial validator.
 
 =cut
 
@@ -141,7 +148,7 @@
     if (-f $self->{'write_to'} and not -w $self->{'write_to'}) {
         return  $self->validation_error( destination_path => "Can't save the file to ".$self->{'write_to'});
     }
-    return $self->validation_ok;
+    return $self->validation_ok( 'write_to' );
 }
 
 
@@ -158,8 +165,12 @@
     pop @dirs; # discard filename. we only want to make the directory ;)
     Jifty::Util->make_path( File::Spec->catdir(@dirs));
     my $writehandle = IO::File->new();
+
+    my $content = $self->argument_value('content');
+    $content =~ s/\r\n/\n/g;
+
     $writehandle->open(">$dest") || die "Couldn't open $dest for writing: ".$!;
-    $writehandle->print( $self->argument_value('content')) || die " Couldn't write to $dest: ".$!;
+    $writehandle->print( $content ) || die " Couldn't write to $dest: ".$!;
     $writehandle->close() || die "Couldn't close filehandle $dest ".$!;
     $self->result->message("Updated $dest");
 }

Modified: jifty/trunk/lib/Jifty/Dispatcher.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Dispatcher.pm	(original)
+++ jifty/trunk/lib/Jifty/Dispatcher.pm	Mon Mar 27 21:30:38 2006
@@ -706,9 +706,18 @@
     my $err = $@;
     # Handle parse errors
     if ( $err and not eval { $err->isa( 'HTML::Mason::Exception::Abort' ) } ) {
-        # XXX TODO: get this into the browser somehow
-        warn "Mason error: $err";
-        Jifty->web->redirect("/__jifty/error/mason_internal_error");
+        # Save the request away, and redirect to an error page
+        Jifty->web->response->error($err);
+        my $c = Jifty::Continuation->new(
+            request  => Jifty->web->request,
+            response => Jifty->web->response,
+            parent   => Jifty->web->request->continuation,
+        );
+
+        warn "$err";
+
+        # Redirect with a continuation
+        Jifty->web->_redirect( "/__jifty/error/mason_internal_error?J:C=" . $c->id );
     } elsif ($err) {
         die $err;
     }

Modified: jifty/trunk/lib/Jifty/Response.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Response.pm	(original)
+++ jifty/trunk/lib/Jifty/Response.pm	Mon Mar 27 21:30:38 2006
@@ -3,8 +3,6 @@
 
 package Jifty::Response;
 
-use base qw/Jifty::Object Class::Accessor/;
-
 =head1 NAME
 
 Jifty::Response - Canonical internal representation of the result of a L<Jifty::Action>
@@ -17,6 +15,10 @@
 
 =cut
 
+use base qw/Jifty::Object Class::Accessor/;
+
+__PACKAGE__->mk_accessors(qw(error));
+
 =head2 new
 
 Creates a new L<Jifty::Response> object.
@@ -96,21 +98,28 @@
     return map {$_, $results{$_}->message} grep {defined $results{$_}->message and length $results{$_}->message} keys %results;
 }
 
+=head2 error [MESSAGE]
+
+Gets or sets a generalized error response.  Setting an error also
+makes the response a L</failure>.
+
 =head2 success
 
-Returns true if none of the results are failures.
+Returns true if none of the results are failures and there is no
+L</error> set.
 
 =cut
 
 sub success {
     my $self = shift;
-    return 1 unless grep {$_->failure} values %{$self->{results}};
-    return 0;
+    return 0 if grep {$_->failure} values %{$self->{results}};
+    return 1;
 }
 
 =head2 failure
 
-Returns true if any of the results failed.
+Returns true if any of the results failed or there was an L</error>
+set.
 
 =cut
 

Modified: jifty/trunk/share/web/templates/__jifty/edit_file
==============================================================================
--- jifty/trunk/share/web/templates/__jifty/edit_file	(original)
+++ jifty/trunk/share/web/templates/__jifty/edit_file	Mon Mar 27 21:30:38 2006
@@ -7,7 +7,6 @@
 </%init>
 <&|/_elements/wrapper, title => $title &>
 <% Jifty->web->form->start %>
-<%$editor->form_field('file_type')%>
 <%$editor->form_field('destination_path')%>
 <%$editor->form_field('content')%>
 <% Jifty->web->return(label => 'Save and return', submit => $editor )%>

Added: jifty/trunk/share/web/templates/__jifty/error/_elements/error_text
==============================================================================
--- (empty file)
+++ jifty/trunk/share/web/templates/__jifty/error/_elements/error_text	Mon Mar 27 21:30:38 2006
@@ -0,0 +1,21 @@
+<h1>Sorry, something went awry</h1>
+<p>For one reason or another, you got to a web page that caused a bit
+of an error. And then you got to our "basic" error handler. Which
+means we haven't written a pretty, easy to understand error message
+for you just yet. The message we <em>do</em> have is:</p>
+
+<blockquote>
+<b><% $error %></b>
+</blockquote>
+
+<p>There's a pretty good chance that error message doesn't mean
+anything to you, but we'd rather you have a little bit of information
+about what went wrong than nothing. We've logged this error, so we
+know we need to write something friendly explaining just what happened
+and how to fix it.</p>
+
+<p>For now <% Jifty->web->link( url =>"/", label => 'head on back
+home')%> and try to forget that we let you down.</p>
+<%args>
+$error
+</%args>
\ No newline at end of file

Added: jifty/trunk/share/web/templates/__jifty/error/_elements/wrapper
==============================================================================
--- (empty file)
+++ jifty/trunk/share/web/templates/__jifty/error/_elements/wrapper	Mon Mar 27 21:30:38 2006
@@ -0,0 +1,30 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
+<head>
+  <title>Mason error</title>  
+  <link rel="stylesheet" type="text/css" href="/__jifty/error/error.css" media="all" />
+</head>
+<body>
+  <div id="headers">
+    <h1 class="title">Mason error</h1>
+  </div>
+  <div id="content">
+    <a name="content"></a>
+% if (Jifty->config->framework('AdminMode') ) {
+<div class="warning admin_mode">
+Alert: Jifty <% Jifty->web->tangent( label => 'administration mode' , url => '/__jifty/admin/')%> is enabled.
+</div>
+% }
+  <% Jifty->web->render_messages %>
+
+  <% $m->content |n%>
+
+  </div>
+</body>
+</html>
+<%doc>
+
+This exists as a fallback wrapper, in case the mason error in question
+is caused by the Jifty app's wrapper, for instance.
+
+</%doc>
\ No newline at end of file

Modified: jifty/trunk/share/web/templates/__jifty/error/dhandler
==============================================================================
--- jifty/trunk/share/web/templates/__jifty/error/dhandler	(original)
+++ jifty/trunk/share/web/templates/__jifty/error/dhandler	Mon Mar 27 21:30:38 2006
@@ -1,17 +1,7 @@
 <&|/_elements/wrapper, title => 'Something went awry' &>
 
-<h1>Sorry, something went awry</h1>
-<p>
-For one reason or another, you got to a web page that caused a bit of an error. And then you got to our "basic" error handler. Which means we haven't written a pretty, easy to understand error message for you just yet. The message we <em>do</em> have is:</p>
-<blockquote>
-<b><%$m->dhandler_arg%></b>
-</blockquote>
+<& _elements/error_text, error => $m->dhandler_arg &>
 
-
-<p>There's a pretty good chance that error message doesn't mean anything to you, but we'd rather you have a little bit of information about what went wrong than nothing. We've logged this error, so we know we need to write something friendly explaining just what happened and how to fix it. 
-</p>
-
-<p>For now <% Jifty->web->link(  url =>"/", label => 'head on back home')%> and try to forget that we let you down.</p>
 </&>
 <%init>
 Jifty->log->error("Unhandled web error ". $m->dhandler_arg);

Added: jifty/trunk/share/web/templates/__jifty/error/error.css
==============================================================================
--- (empty file)
+++ jifty/trunk/share/web/templates/__jifty/error/error.css	Mon Mar 27 21:30:38 2006
@@ -0,0 +1,13 @@
+h1 {
+    color: red;
+}
+
+<%init>
+$r->content_type("text/css");
+</%init>
+<%doc>
+
+This exists as a fallback CSS, in case the Jifty app's CSS is causing
+the error.
+
+</%doc>
\ No newline at end of file

Added: jifty/trunk/share/web/templates/__jifty/error/mason_internal_error
==============================================================================
--- (empty file)
+++ jifty/trunk/share/web/templates/__jifty/error/mason_internal_error	Mon Mar 27 21:30:38 2006
@@ -0,0 +1,56 @@
+<&| $wrapper, title => "Mason error" &>
+
+Error in <% $file %>, lines <% "@lines" %>:
+<pre><% $msg %></pre>
+
+<% Jifty->web->return( label => "Try again" ) %>
+
+<h2>Call stack</h2>
+<ul>
+% for my $frame (@stack) {
+%   next if $frame->filename =~ m{/HTML/Mason/};
+%   if (-w $frame->filename) {
+%     my $path = $frame->filename;
+%     for (map {$_->[1]} @{Jifty->handler->mason->interp->comp_root}) {
+%       last if $path =~ s/^\Q$_\E//;
+%     }
+%     if ($path ne $frame->filename) {
+<li>Template <% Jifty->web->tangent( url =>"/__jifty/edit/mason_component$path",
+                            label => "$path line ".$frame->line,
+                            parameters => { line => $frame->line } ) %></li>
+%     } else {
+<li><% Jifty->web->tangent( url =>"/__jifty/edit/library$path",
+                            label => "$path line ".$frame->line,
+                            parameters => { line => $frame->line } ) %></li>
+%     }
+%   } else {
+<li><% $frame->filename %> line <% $frame->line %></li>
+%   }
+% }
+</ul>
+
+</&>
+<%init>
+my $wrapper = "/_elements/wrapper";
+
+my $cont = Jifty->web->request->continuation;
+$wrapper = "/__jifty/error/_elements/wrapper"
+  if $cont and $cont->request->path eq "/__jifty/error/mason_internal_error";
+
+# If we're not in devel, bail
+if (not Jifty->config->framework("DevelMode") or not $cont) {
+    $m->comp({content => sub {
+        $m->comp("_elements/error_text", error => "mason internal error");
+    }, $wrapper, title => "Something went awry"});
+    $m->abort;
+}
+
+my $e = $cont->response->error;
+my $msg = $e->message;
+$msg =~ s/, <\S+> (line|chunk) \d+\././;
+
+my $info = $e->analyze_error;
+my $file = $info->{file};
+my @lines = @{$info->{lines}};
+my @stack = @{$info->{frames}};
+</%init>
\ No newline at end of file


More information about the Jifty-commit mailing list