Class: Airbrake::Notice

Inherits:
Object
  • Object
show all
Includes:
Ignorable, Loggable, Stashable
Defined in:
lib/airbrake-ruby/notice.rb

Overview

Represents a chunk of information that is meant to be either sent to Airbrake or ignored completely.

Since:

  • v1.0.0

Constant Summary collapse

CONTEXT =

Returns the information to be displayed in the Context tab in the dashboard.

Returns:

  • (Hash{Symbol=>String,Hash})

    the information to be displayed in the Context tab in the dashboard

Since:

  • v1.0.0

{
  os: RUBY_PLATFORM,
  language: "#{RUBY_ENGINE}/#{RUBY_VERSION}".freeze,
  notifier: Airbrake::NOTIFIER_INFO,
}.freeze
MAX_NOTICE_SIZE =

Returns the maxium size of the JSON payload in bytes.

Returns:

  • (Integer)

    the maxium size of the JSON payload in bytes

Since:

  • v1.0.0

64000
PAYLOAD_MAX_SIZE =

Returns the maximum size of hashes, arrays and strings in the notice.

Returns:

  • (Integer)

    the maximum size of hashes, arrays and strings in the notice.

Since:

  • v1.0.0

10000
JSON_EXCEPTIONS =

Returns the list of possible exceptions that might be raised when an object is converted to JSON.

Returns:

  • (Array<StandardError>)

    the list of possible exceptions that might be raised when an object is converted to JSON

Since:

  • v1.0.0

[
  IOError,
  NotImplementedError,
  JSON::GeneratorError,
  Encoding::UndefinedConversionError,
].freeze
WRITABLE_KEYS =

Returns the list of keys that can be be overwritten with #[]=.

Returns:

  • (Array<Symbol>)

    the list of keys that can be be overwritten with #[]=

Since:

  • v1.0.0

%i[notifier context environment session params].freeze
TRUNCATABLE_KEYS =

Returns parts of a Notice’s payload that can be modified by the truncator.

Returns:

  • (Array<Symbol>)

    parts of a Notice’s payload that can be modified by the truncator

Since:

  • v1.0.0

%i[errors environment session params].freeze
HOSTNAME =

Returns the name of the host machine.

Returns:

  • (String)

    the name of the host machine

Since:

  • v1.0.0

Socket.gethostname.freeze
DEFAULT_SEVERITY =

Returns:

  • (String)

Since:

  • v1.0.0

'error'.freeze

Instance Attribute Summary

Attributes included from Ignorable

#ignored

Instance Method Summary collapse

Methods included from Stashable

#stash

Methods included from Loggable

#logger

Methods included from Ignorable

#ignore!, #ignored?

Constructor Details

#initialize(exception, params = {}) ⇒ Notice

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Notice.

Since:

  • v1.0.0



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/airbrake-ruby/notice.rb', line 50

def initialize(exception, params = {})
  @config = Airbrake::Config.instance
  @truncator = Airbrake::Truncator.new(PAYLOAD_MAX_SIZE)

  @payload = {
    errors: NestedException.new(exception).as_json,
    context: context(exception),
    environment: {
      program_name: $PROGRAM_NAME,
    },
    session: {},
    params: params,
  }

  stash[:exception] = exception
end

Instance Method Details

#[](key) ⇒ Object

Reads a value from notice’s payload.

Returns:

  • (Object)

Raises:

Since:

  • v1.0.0



91
92
93
94
# File 'lib/airbrake-ruby/notice.rb', line 91

def [](key)
  raise_if_ignored
  @payload[key]
end

#[]=(key, value) ⇒ void

This method returns an undefined value.

Writes a value to the payload hash. Restricts unrecognized writes.

Examples:

notice[:params][:my_param] = 'foobar'

Raises:

Since:

  • v1.0.0



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/airbrake-ruby/notice.rb', line 105

def []=(key, value)
  raise_if_ignored

  unless WRITABLE_KEYS.include?(key)
    raise Airbrake::Error,
          ":#{key} is not recognized among #{WRITABLE_KEYS}"
  end

  unless value.respond_to?(:to_hash)
    raise Airbrake::Error, "Got #{value.class} value, wanted a Hash"
  end

  @payload[key] = value.to_hash
end

#to_json(*_args) ⇒ Hash{String=>String}?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts the notice to JSON. Calls to_json on each object inside notice’s payload. Truncates notices, JSON representation of which is bigger than MAX_NOTICE_SIZE.

Returns:

  • (Hash{String=>String}, nil)

Since:

  • v1.0.0



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/airbrake-ruby/notice.rb', line 73

def to_json(*_args)
  loop do
    begin
      json = @payload.to_json
    rescue *JSON_EXCEPTIONS => ex
      logger.debug("#{LOG_LABEL} `notice.to_json` failed: #{ex.class}: #{ex}")
    else
      return json if json && json.bytesize <= MAX_NOTICE_SIZE
    end

    break if truncate == 0
  end
end