Permalink
Browse files

Improved reporting for template compile errors

Previously when a template failed to compile (i.e., for CoffeeScript, HAML, SASS, etc.) a long, useless stack trace was produced. In such cases, the stack trace contains information about Sinatra internals that the web developer likely has little interest in. With this commit, for such errors only the actual error message (which might look like "Error: Parse error on line 190: Unexpected 'INDENT'") is shown to the user. This greatly improves the experience of writing web applications using compiled languages.
  • Loading branch information...
1 parent a6f6a14 commit 1cb56cce421162e38a056641d016cb9d394e9e99 Micah Wylde committed Jun 29, 2011
Showing with 15 additions and 3 deletions.
  1. +15 −3 lib/sinatra/base.rb
View
18 lib/sinatra/base.rb
@@ -395,6 +395,8 @@ def time_for(value)
# object rather than the application instance.
# :views Views directory to use.
module Templates
+ class TemplateError < StandardError; end
+
module ContentTyped
attr_accessor :content_type
end
@@ -517,8 +519,14 @@ def render(engine, data, options={}, locals={}, &block)
# compile and render template
layout_was = @default_layout
@default_layout = false
- template = compile_template(engine, data, options, views)
- output = template.render(scope, locals, &block)
+
+ begin
+ template = compile_template(engine, data, options, views)
+ output = template.render(scope, locals, &block)
+ rescue => e
+ raise TemplateError.new("#{data} - #{e.message}")
+ end
+
@default_layout = layout_was
# render layout
@@ -816,7 +824,11 @@ def error_block!(key)
end
def dump_errors!(boom)
- msg = ["#{boom.class} - #{boom.message}:", *boom.backtrace].join("\n\t")
+ if boom.is_a? Templates::TemplateError
+ msg = boom.message
+ else
+ msg = ["#{boom.class} - #{boom.message}:", *boom.backtrace].join("\n\t")
+ end
@env['rack.errors'].puts(msg)
end

0 comments on commit 1cb56cc

Please sign in to comment.