class Closure::ShowExceptions::Javascript

@private - internal use only

Public Class Methods

new(app) click to toggle source
# File lib/closure/show_exceptions.rb, line 30
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/closure/show_exceptions.rb, line 34
def call(env)
  @app.call(env)
rescue Closure::Compiler::Error => e
  raise e unless env[Script::ENV_ERROR_CONTENT_TYPE] == 'application/javascript'
  body = 'try{console.error('
  body += '"Closure Compiler: %s\n", '
  body += "#{e.message.rstrip.dump}"
  body += ')}catch(err){}'
  [200,
   {"Content-Type" => "application/javascript",
    "Content-Length" => body.size.to_s},
   [body]]
rescue Closure::Templates::Error => e
  raise e unless env[Script::ENV_ERROR_CONTENT_TYPE] == 'application/javascript'
  body = 'try{console.error('
  body += '"Closure Templates: 1 error(s)\n$s", '
  body += "#{e.message.rstrip.dump}"
  body += ')}catch(err){}'
  [200,
   {"Content-Type" => "application/javascript",
    "Content-Length" => body.size.to_s},
   [body]]
rescue StandardError, LoadError, SyntaxError => e
  raise e unless env[Script::ENV_ERROR_CONTENT_TYPE] == 'application/javascript'
  body = 'try{console.error('
  if e.class.to_s == e.message.rstrip
      body += '"Closure Script: %s\n%s", '
  else
      body += '"Closure Script: %s\n\n%s\n\n%s", '
      body += "#{e.class.to_s.dump}, "
  end
  body += "#{e.message.rstrip.dump}, "
  body += "#{e.backtrace.join("\n").dump}"
  body += ')}catch(err){}'
  [200,
   {"Content-Type" => "application/javascript",
    "Content-Length" => body.size.to_s},
   [body]]
end