Last active
August 29, 2015 14:08
-
-
Save camertron/0422ce551111cd23afdc to your computer and use it in GitHub Desktop.
Grape Middleware for Error Reporting
This file contains hidden or 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
module Grape | |
class Endpoint | |
def build_middleware_with_error_tracking | |
builder = build_middleware_without_error_tracking | |
builder.use(::ErrorTrackingMiddleware) | |
# HAAAAACK! | |
builder.instance_variable_get(:'@use').tap do |use_arr| | |
middleware_proc = use_arr.delete_at(use_arr.size - 1) | |
use_arr.insert(0, middleware_proc) | |
end | |
builder | |
end | |
alias_method :build_middleware_without_error_tracking, :build_middleware | |
alias_method :build_middleware, :build_middleware_with_error_tracking | |
end | |
end |
This file contains hidden or 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
module GrapeRollbar | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
module ClassMethods | |
def track_errors_with_rollbar(api) | |
set(:rollbar_api, api) | |
end | |
end | |
end |
This file contains hidden or 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
class ErrorTrackingMiddleware < Grape::Middleware::Error | |
def call!(env) | |
@env = env | |
# expected errors | |
error = catch(:error) do | |
return @app.call(env) | |
end | |
# send error to rollbar | |
error_response(error) | |
rescue StandardError => e | |
# unexpected errors | |
# send error to rollbar | |
raise e | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment