Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit 1cb56cce421162e38a056641d016cb9d394e9e99 1 parent a6f6a14
Micah Wylde authored
Showing with 15 additions and 3 deletions.
  1. +15 −3 lib/sinatra/base.rb
18 lib/sinatra/base.rb
View
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.