Sha256: 6258a0651952f6c8b2a4287b4374dd07237252f2fbcef974b59dccfbe9bed4d7

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

require 'uri'

module RestfulResourceBugsnag
  class Middleware
    def initialize(bugsnag)
      @bugsnag = bugsnag
    end

    def call(notification)
      exception = notification.exceptions.first

      if exception.is_a?(RestfulResource::HttpClient::HttpError)
        notification.add_tab(:restful_resource_response, {
          status: exception.response.status,
          body: exception.response.body,
          headers: exception.response.headers
        })
        notification.add_tab(:restful_resource_request, {
          method: exception.request.method,
          url: exception.request.url,
          accept: exception.request.accept,
          body: exception.request.body
        })
      end

      # Display the request host in the context so its easy to see in Bugsnag which service was unresponsive
      # Group the errors by host to reduce the amount of error spam
      if exception.is_a?(RestfulResource::HttpClient::ServiceUnavailable)
        notification.context = "HTTP 503: Service unavailable #{request_host_from_exception exception}"
        notification.grouping_hash = notification.context
      end

      if exception.is_a?(RestfulResource::HttpClient::ClientError)
        notification.context = "Client error: Service unavailable #{request_host_from_exception exception}"
        notification.grouping_hash = notification.context
      end

      @bugsnag.call(notification)
    end

    private

    def request_host_from_exception(exception)
      URI.parse(exception.request.url).host
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
restful_resource_bugsnag-0.3.0 lib/restful_resource_bugsnag/middleware.rb
restful_resource_bugsnag-0.2.0 lib/restful_resource_bugsnag/middleware.rb