Sha256: 96d53b45fd1eeb4e975d336a5b1d4713d3dac1dd1aadb506d922a1e04e708ae9

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

module Isolator
  # Wrapper over different notifications methods (exceptions, logging, uniform notifier)
  class Notifier
    attr_reader :exception, :backtrace

    def initialize(exception, backtrace = caller)
      @exception = exception
      @backtrace = backtrace
    end

    def call
      log_exception
      send_notifications if send_notifications?
      raise(exception.class, exception.message, filtered_backtrace) if raise_exceptions?
    end

    private

    def raise_exceptions?
      Isolator.config.raise_exceptions?
    end

    def send_notifications?
      Isolator.config.send_notifications?
    end

    def log_exception
      return unless Isolator.config.logger
      Isolator.config.logger.warn(
        "[ISOLATOR EXCEPTION]\n" \
        "#{exception.message}\n" \
        "  ↳ #{filtered_backtrace.first}"
      )
    end

    def send_notifications
      return unless uniform_notifier_loaded?

      ::UniformNotifier.active_notifiers.each do |notifier|
        notifier.out_of_channel_notify exception.message
      end
    end

    def filtered_backtrace
      backtrace.reject { |line| line =~ /gems/ }.take_while { |line| line !~ /ruby/ }
    end

    def uniform_notifier_loaded?
      return true if defined?(::UniformNotifier)

      begin
        require "uniform_notifier"
      rescue LoadError
        warn(
          "Please, install and configure 'uniform_notifier' to send notifications:\n" \
          "# Gemfile\n" \
          "gem 'uniform_notifer', '~> 1.11', require: false"
        )
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
isolator-0.1.0.pre2 lib/isolator/notifier.rb
isolator-0.1.0.pre lib/isolator/notifier.rb