Sha256: 20d3c162ec7ea06d12113fbf1e93988e1765ec66622ea0037e1d90425031b521

Contents?: true

Size: 1.5 KB

Versions: 6

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module LightServiceExt
  class ErrorInfo
    attr_reader :error, :type, :message, :title

    def initialize(error, message: nil, fatal: false)
      @fatal = fatal
      @error = error
      @type = error.class.name
      @message = message || error&.message
      @title = "#{error.class.name} : #{error&.message}"
    end

    def fatal_error?
      @fatal || !non_fatal_error?
    end

    def error_summary
      header = fatal_error? ? "SERVER ERROR FOUND" : "ERROR FOUND"

      <<~TEXT
        =========== #{header}: #{title} ===========

        FULL STACK TRACE
        #{clean_backtrace.join("\n")}

        #{'=' * 56}
      TEXT
    end

    def errors
      model = error && (error.try(:model) || error.try(:record))
      return model.errors.messages if model.present?

      { base: message }
    end

    def to_h
      {
        type: type,
        message: message,
        exception: title,
        backtrace: clean_backtrace[0, 3]&.join("\n"),
        error: error,
        fatal_error?: fatal_error?,
        errors: errors
      }
    end

    def backtrace
      error&.backtrace || []
    end

    def clean_backtrace
      @clean_backtrace ||= if defined? Rails.backtrace_cleaner
                             Rails.backtrace_cleaner.clean(backtrace || [])
                           else
                             backtrace || []
                           end
    end

    def non_fatal_error?
      error.nil? || LightServiceExt.config.non_fatal_error?(error)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
light-service-ext-0.1.8 lib/light-service-ext/error_info.rb
light-service-ext-0.1.7 lib/light-service-ext/error_info.rb
light-service-ext-0.1.6 lib/light-service-ext/error_info.rb
light-service-ext-0.1.5 lib/light-service-ext/error_info.rb
light-service-ext-0.1.4 lib/light-service-ext/error_info.rb
light-service-ext-0.1.3 lib/light-service-ext/error_info.rb