Sha256: f59cb5b4d0b9455c0ecdc203126d532b1ee48ec30a7f9b5d899a52fbcd3cef14

Contents?: true

Size: 1.41 KB

Versions: 5

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true
require 'rack/utils'
require 'active_model_serializers'

module Noise
  # Generic error serializer
  class ErrorSerializer < ActiveModel::Serializer
    BUGSNAG_URL =
      'https://app.bugsnag.com/{organization}/{project}/errors?filters[event.since][]=30d&filters[user.name][]={id}'

    attributes(
      :id,
      :code,
      :links,
      :title,
      :fallback_message,
    )

    def attributes(*)
      data = super
      data['object'] = 'error'
      data
    end

    def id
      scope.try(:[], :id)
    end

    def code
      code_from_http_status
    end

    def title
      'Internal Server Error'
    end

    def links
      {
        'about' => {
          'href' => bugsnag_search_url.to_s,
        },
      }
    end

    def fallback_message
      nil
    end

    private

    def code_from_http_status
      http_status = scope.try(:[], :http_status).to_i
      default_message = Rack::Utils::HTTP_STATUS_CODES[500]
      status_code = Rack::Utils::HTTP_STATUS_CODES.fetch(http_status, default_message)
      status_code.parameterize.underscore.to_sym
    end

    def bugsnag_search_url
      return unless Noise.config.bugsnag_project
      return unless id
      require 'addressable/template'

      template = Addressable::Template.new(BUGSNAG_URL)
      template.expand(id: id, organization: Noise.config.bugsnag_organization, project: Noise.config.bugsnag_project)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
white_noise-1.1.2 lib/noise/error_serializer.rb
white_noise-1.1.1 lib/noise/error_serializer.rb
white_noise-1.1.0 lib/noise/error_serializer.rb
white_noise-1.0.1 lib/noise/error_serializer.rb
white_noise-1.0.0 lib/noise/error_serializer.rb