Module: HBW

Defined in:
lib/hbw.rb,
lib/hbw/config.rb,
lib/hbw/version.rb

Defined Under Namespace

Classes: ArgumentError, Config

Constant Summary collapse

VERSION =
"0.2.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.notice_only_notifierObject (readonly)

Returns the value of attribute notice_only_notifier.



10
11
12
# File 'lib/hbw.rb', line 10

def notice_only_notifier
  @notice_only_notifier
end

Class Method Details

.configure {|config| ... } ⇒ Object

Yields:

  • (config)


127
128
129
130
131
132
133
# File 'lib/hbw.rb', line 127

def configure
  config = Config.new
  yield(config)
  if config.notice_only_api_key && should_use_honeybadger?
    @notice_only_notifier = build_notifier(config.notice_only_api_key)
  end
end

.notify(exception_or_error_class, option_or_error_message = nil, error_class: nil, error_message: nil, context: nil, backtrace: nil, parameters: nil, action: nil, raise_development: true, notice_only: false) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hbw.rb', line 42

def notify(exception_or_error_class, option_or_error_message = nil,
           error_class: nil, error_message: nil, context: nil, backtrace: nil, parameters: nil, action: nil,
           raise_development: true, notice_only: false)
  if exception_or_error_class.is_a?(Exception)
    # option_or_error_message must be option (Hash-like object)
    if !(option_or_error_message.nil? || option_or_error_message.respond_to?(:to_hash))
      raise HBW::ArgumentError.new("option_or_error_message must be nil or Hash-like object, but got #{option_or_error_message}")
    end
    if option_or_error_message.nil?
      opts = {}
    else
      opts = option_or_error_message.to_hash
    end
    merge_opts!(opts,
                error_class: error_class, error_message: error_message,
                context: context, backtrace: backtrace, parameters: parameters, action: action, raise_development: raise_development, notice_only: notice_only)
    notify_raw(exception_or_error_class, opts)
  elsif exception_or_error_class.is_a?(String)
    # option_or_error_message must be error_message (String)
    if option_or_error_message.nil? || !option_or_error_message.respond_to?(:to_s)
      raise HBW::ArgumentError.new("option_or_error_message must be String-like object, but got #{option_or_error_message}")
    end
    # error_class and error_message must be nil
    if (!error_class.nil? || !error_message.nil?)
      raise HBW::ArgumentError.new("error_class must be nil but got #{error_class}, error_message must be nil but got #{error_message}")
    end
    opts = {}
    merge_opts!(opts,
                error_class: exception_or_error_class, error_message: option_or_error_message,
                context: context, backtrace: backtrace, parameters: parameters, action: action, raise_development: raise_development, notice_only: notice_only)
    notify_raw(opts)
  else
    raise HBW::ArgumentError.new("Invalid argument")
  end

rescue => e
  if should_use_honeybadger?
    # Note: Something wrong. exception must not occur when
    # should_use_honeybadger? is true.
    honeybadger_notify(e)
    return nil
  else
    raise e
  end
end

.notify_raw(exception_or_opts, opts = {}) ⇒ NilClass

raise_development is supported. By default, raise_development is true. notice_only is supported. By default, notice_only is false.

Parameters:

  • exception_or_opts (Exception, Hash)
  • opts (Hash) (defaults to: {})

Returns:

  • (NilClass)

Raises:

  • (RuntimeError)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/hbw.rb', line 94

def notify_raw(exception_or_opts, opts = {})
  opts.merge!(exception_or_opts.to_hash) if exception_or_opts.respond_to?(:to_hash)
  raise_development = opts.has_key?(:raise_development) ? opts.delete(:raise_development) : true
  notice_only       = opts.has_key?(:notice_only)       ? opts.delete(:notice_only)       : false

  if notice_only
    opts[:error_message] = "[Notice Only] #{error_class(exception_or_opts, opts)}: #{error_message(exception_or_opts, opts)}"
  end

  # QA, Production
  if should_use_honeybadger?
    args =
      if exception_or_opts.respond_to?(:to_hash)  # Already merged to opts
        [opts]
      else
        [exception_or_opts, opts]
      end
    notify_internal(args, notice_only: notice_only)
    return nil
  end

  # Development
  return nil if !raise_development

  if notice_only
    raise opts[:error_message]
  else
    raise exception_or_error_message(exception_or_opts, opts)
  end

  nil
end