Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MRI errors second try #270

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions lib/goliath/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,7 @@ def call(env)
env[ASYNC_CALLBACK].call(validation_error(e.status_code, e.message))

rescue Exception => e
logthis = "#{e.backtrace[0]}: #{e.message} (#{e.class})\n"
e.backtrace[1..-1].each do |bt|
logthis += " from #{bt}\n"
end
env.logger.error(logthis)
env.log_exception(e)
env[RACK_EXCEPTION] = e

message = Goliath.env?(:production) ? 'An error happened' : e.message
Expand Down
11 changes: 11 additions & 0 deletions lib/goliath/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ def logger
self[RACK_LOGGER]
end

# Convenience method for logging exceptions
#
# @param e [Exception] The exception to log
def log_exception(e)
logthis = "Exception:\n#{e.backtrace[0].gsub(/(.*?:.*?):(.*)/,'\1 \2')}: #{e.message} (#{e.class})\n"
e.backtrace[1..-1].each do |bt|
logthis += "\tfrom #{bt.gsub(/(.*?:.*?):(.*)/,'\1 \2')}\n"
end
self[RACK_LOGGER].error(logthis)
end

# @param name [Symbol] The method to check if we respond to it.
# @return [Boolean] True if the Env responds to the method, false otherwise
def respond_to?(name)
Expand Down
3 changes: 1 addition & 2 deletions lib/goliath/rack/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ def safely(env, headers={})
rescue Goliath::Validation::Error => e
validation_error(e.status_code, e.message, headers)
rescue Exception => e
env.logger.error(e.message)
env.logger.error(e.backtrace.join("\n"))
env.log_exception(e)
validation_error(500, e.message, headers)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/goliath/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def server_exception(e)
if e.is_a?(Goliath::Validation::Error)
status, headers, body = [e.status_code, {}, ('{"error":"%s"}' % e.message)]
else
@env[RACK_LOGGER].error("#{e.message}\n#{e.backtrace.join("\n")}")
@env.log_exception(e)
message = Goliath.env?(:production) ? 'An error happened' : e.message

status, headers, body = [500, {}, message]
Expand Down
2 changes: 1 addition & 1 deletion lib/goliath/websocket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def max_frame_size
env['handler'] = EM::WebSocket::HandlerFactory.build_with_request(conn, request,
upgrade_data, false, false)
rescue Exception => e
env.logger.error("#{e.message}\n#{e.backtrace.join("\n")}")
env.log_exception(e)
return [500, {}, {:error => "Upgrade failed"}]
end
env['handler'].run
Expand Down