* Default error messages
authorBlake Mizerany <blake@blake.local>
Fri, 30 Nov 2007 02:35:06 +0000 (29 18:35 -0800)
committerBlake Mizerany <blake@blake.local>
Fri, 30 Nov 2007 02:35:06 +0000 (29 18:35 -0800)
* Use SINATRA_ENV var if set

Rakefile
images/404.png [new file with mode: 0644]
images/500.png [new file with mode: 0644]
lib/sinatra.rb

index adbf2fd..0011b2f 100644 (file)
--- a/Rakefile
+++ b/Rakefile
@@ -4,5 +4,6 @@ require 'rake/testtask'
 task :default => :test
 
 Rake::TestTask.new do |t|
+  ENV['SINATRA_ENV'] = 'test'
   t.pattern = "test/*_test.rb"
 end
diff --git a/images/404.png b/images/404.png
new file mode 100644 (file)
index 0000000..902110e
Binary files /dev/null and b/images/404.png differ
diff --git a/images/500.png b/images/500.png
new file mode 100644 (file)
index 0000000..57c84c3
Binary files /dev/null and b/images/500.png differ
index 9bb99a6..8585118 100644 (file)
@@ -64,7 +64,7 @@ module Sinatra
       
   class Event
 
-    URI_CHAR = '[^/?:,&#]'.freeze unless defined?(URI_CHAR)
+    URI_CHAR = '[^/?:,&#\.]'.freeze unless defined?(URI_CHAR)
     PARAM = /:(#{URI_CHAR}+)/.freeze unless defined?(PARAM)
     
     attr_reader :path, :block, :param_keys, :pattern
@@ -328,6 +328,11 @@ def layout(name = :layout, &b)
   Sinatra.application.define_layout(name, &b)
 end
 
+def configures(*envs, &b)
+  yield if  envs.include?(Sinatra.application.options.env) ||
+            envs.empty?
+end
+
 ### Misc Core Extensions
 
 module Kernel
@@ -445,3 +450,74 @@ at_exit do
   raise $! if $!
   Sinatra.run if Sinatra.application.options.run
 end
+
+ENV['SINATRA_ENV'] = 'test' if $0 =~ /_test\.rb$/
+Sinatra::Application.default_options.merge!(
+  :env => (ENV['SINATRA_ENV'] || 'development').to_sym
+)
+
+configures :development do
+  
+  get '/sinatra_custom_images/:image.png' do
+    File.read(File.dirname(__FILE__) + "/../images/#{params[:image]}.png")
+  end
+  
+  error 404 do
+    %Q(
+    <html>
+      <body style='text-align: center; color: #888; font-family: Arial; font-size: 22px; margin: 20px'>
+      <h2>Sinatra doesn't know this diddy.</h2>
+      <img src='/sinatra_custom_images/404.png'></img>
+      </body>
+    </html>
+    )
+  end
+  
+  error 500 do
+    @error = request.env['sinatra.error']
+    %Q(
+    <html>
+       <body>
+               <style type="text/css" media="screen">
+                       body {
+                               font-family: Verdana;
+                               color: #333;
+                       }
+
+                       #content {
+                               width: 700px;
+                               margin-left: 20px;
+                       }
+
+                       #content h1 {
+                               width: 99%;
+                               color: #1D6B8D;
+                               font-weight: bold;
+                       }
+                       
+                       #stacktrace {
+                         margin-top: -20px;
+                       }
+
+                       #stacktrace pre {
+                               font-size: 12px;
+                               border-left: 2px solid #ddd;
+                               padding-left: 10px;
+                       }
+
+                       #stacktrace img {
+                               margin-top: 10px;
+                       }
+               </style>
+               <div id="content">
+               <img src="/sinatra_custom_images/500.png" />
+                       <div id="stacktrace">
+                               <h1>#{@error.message}</h1>
+                               <pre><code>#{@error.backtrace.join("\n")}</code></pre>
+               </div>
+       </body>
+    </html>
+    )
+  end
+  
+end