Skip to content

Commit

Permalink
Example: Dancer's error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
agordon committed Jul 26, 2012
1 parent d473ea5 commit 39254b7
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/dancer_bootstrap_fontawesome_template.pm
Expand Up @@ -11,6 +11,7 @@ use examples::show_file;
use examples::photo_gallery;
use examples::markdown;
use examples::template_plugins;
use examples::error_handling;

our $VERSION = '0.1';

Expand Down
30 changes: 30 additions & 0 deletions lib/examples/error_handling.pm
@@ -0,0 +1,30 @@
package examples::error_handling;
use Dancer ':syntax';
use strict;
use warnings;

=pod
This module demonstrates Dancer's error handling capabilities, in development and production modes.
=cut

get '/error_handling' => sub {
template 'examples/error_handling';
};

post '/error_handling' => sub {
my $show_errors = param 'showerrors';
print STDERR "show_errors = $show_errors\n";
$show_errors = 1 unless $show_errors eq "0";

=pod
NOTE:
In a real project, you should never set "show_errors" like this.
Set it properly in both "./environments/development.yml" and
./environments/production.yml" .
=cut
set show_errors => $show_errors;
die "Oops, I did it again";
};

true;
94 changes: 94 additions & 0 deletions views/examples/error_handling.tt
@@ -0,0 +1,94 @@
[%###
This page demonstrates Dancer's stack-trace error handling in development mode.
###%]

<!-- BootStrap NavBar -->
<div class="navbar">
<div class="navbar-inner">
<div class="container">
<h3><a class="brand" href="[% request.uri_base %]"><img src="images/dancer_man.png"> Perl Dancer</a></h3>
</div>
</div>
</div>

<!-- HEADER line -->
<div class="container">

<div class="page-header">
<div class="row">
<div class="span12">
<h2>Dancer + Bootstrap Exapmles<//h2>
<h1>Dancer's Error Handling</h1>
</div>
</div>
</div>

<div class="row">
<div class="span12">

This page demonstrates Dancer's error handling code, in development and production modes.
<br/>
In development mode, any <code>die</code> statement will generate a detailed stack-trace with loads of information. In production mode, <code>die</code> statements will generate a generic error message.
<br/>
<br/>

<h3>Code Highlights</h3>
<ul>
<li>The Dancer code is in
<a href="[% request.uri_for("/show_file",file => "error_handling.pm", example => "Error Handling", url => request.uri_for("/error_handling") ) %]">
<code>./lib/examples/error_handling.pm</code> <i class="icon-eye-open"></i> </a>.
</li>

<li>This HTML tempate is in
<a href="[% request.uri_for("/show_file",file => "error_handling.tt", example => "Error Handling", url => request.uri_for("/error_handling") ) %]">
<code>./views/examples/error_handling.tt</code> <i class="icon-eye-open"></i> </a>.
</li>
<li>Showing or hiding the detailed error information is controlled by the <a target="_blank" href="http://search.cpan.org/dist/Dancer/lib/Dancer/Introduction.pod#EXECUTION_ERRORS">show_errors</a> configuration key - which can be set either in the YML configuration files or at runtime.</li>
<li>Standard Dancer applications have <a target="_blank" href="http://search.cpan.org/dist/Dancer/lib/Dancer/Introduction.pod#CONFIGURATION_AND_ENVIRONMENTS">two YML configuration files</a>: <strong>development</strong> settings go in <code>./environments/development.yml</code> and <strong>production</strong> settings go in <code>./environment/production.yml</code>.</li>
</ul>

</div>
</div>

<br/>
<br/>

<div class="row">
<div class="span12">
[%################################
Error Handling Example starts here
#################################%]

<form class="form-horizontal" method="post">
<fieldset>
<legend>Error Handling Example</legend>

<div class="alert alert-info">
By clicking on the button below, you will get a <strong>Runtime error message</strong> - it's not a bug - it's the whole point of this demonstration.
</div>

<!-- Radio Buttons Controls -->
<div class="control-group">
<label class="control-label">Show errors</label>
<div class="controls">
<label class="radio">
<input type="radio" name="showerrors" id="showerror1" value="1" checked>
yes (development mode)
</label>
<label class="radio">
<input type="radio" name="showerrors" id="showerror2" value="0">
no (production mode)
</label>
</div>
</div>


<!-- The Submit buttons -->
<div class="form-actions">
<button type="submit" class="btn btn-primary">Crash and Burn!</button>
</div>
</fieldset>
</form>

</div>
</div>
1 change: 1 addition & 0 deletions views/index.tt
Expand Up @@ -92,6 +92,7 @@
<li><a href="[% request.uri_for("/photo_grid")%]">Photo Gallery Grid / Thumbnails</a></li>
<li><a href="[% request.uri_for("/markdown")%]">Markdown Text Rendering</a></li>
<li><a href="[% request.uri_for("/template_plugins")%]">Template::Toolkit plugins</a></li>
<li><a href="[% request.uri_for("/error_handling")%]">Dancer's Error Handling</a></li>
</ul>
</div>
</div>
Expand Down

0 comments on commit 39254b7

Please sign in to comment.