Module: SemanticLogger::ExceptionDetails

Defined in:
lib/semantic_logger/exception_details.rb

Overview

Separates the detail that Ruby's Exception#detailed_message adds to an exception's message, such as did_you_mean suggestions or error_highlight's source snippet, from the message itself.

Internal: Log caches the result for each of its exceptions; see Log#exception_details.

Class Method Summary collapse

Class Method Details

.extract(exception, source: false) ⇒ Object

Returns [String] the detail that #detailed_message adds to the exception's message, without the class name that Ruby also adds, or nil when there is none or it cannot be separated.

Never raises, so that an exception's own #detailed_message cannot stop a log entry being written, nor its #message, which Ruby's #detailed_message calls: as Utils.exception_message, that includes a ScriptError, such as the NotImplementedError of an abstract #message.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/semantic_logger/exception_details.rb', line 17

def self.extract(exception, source: false)
  detailed = full_detailed_message(exception, source)
  return unless detailed

  base = DETAILED_MESSAGE.bind_call(exception, highlight: false)
  # An override that does not build on Ruby's message cannot be separated from it.
  return unless detailed.is_a?(String) && detailed.start_with?(base)

  details = detailed.delete_prefix(base).sub(/\A\n+/, "").rstrip
  details unless details.empty?
rescue StandardError, ScriptError
  nil
end