-
-
Save rstacruz/4286559 to your computer and use it in GitHub Desktop.
Better Errors for non-web apps.
https://github.com/charliesome/better_errors/issues/55
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'better_wrapper' | |
BetterWrapper.wrap do | |
raise "Did it work?" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'better_errors' | |
require 'binding_of_caller' | |
module BetterWrapper | |
def self.wrap(&blk) | |
yield | |
rescue => e | |
require 'rack' | |
BetterErrors.application_root = File.expand_path("..", $0) | |
print_error e | |
silence_output | |
server.run builder_for(e), :Port => 9292 | |
end | |
# This is optional | |
def self.silence_output | |
require 'stringio' | |
$stderr = $stdout = StringIO.new | |
end | |
def self.print_error(e) | |
puts "#{e.class}: #{e.message}" | |
puts " " + e.backtrace.join("\n ") | |
puts "" | |
puts "See http://localhost:9292 for errors. ^C to exit." | |
end | |
def self.server | |
server = Rack::Handler::WEBrick | |
trap('INT') { server.shutdown } | |
server | |
end | |
def self.builder_for(e) | |
Rack::Builder.new do | |
run BetterErrors::Middleware.new(Proc.new { |env| | |
# Only raise once so not to expire sessions | |
unless @raised | |
@raised = true | |
raise e | |
end | |
[301, { "Location" => "/__better_errors" }, ['']] | |
}) | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment