Class: LightServiceExt::ErrorInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/light-service-ext/error_info.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error, message: nil, fatal: false) ⇒ ErrorInfo

Returns a new instance of ErrorInfo.



7
8
9
10
11
12
13
# File 'lib/light-service-ext/error_info.rb', line 7

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

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



5
6
7
# File 'lib/light-service-ext/error_info.rb', line 5

def error
  @error
end

#messageObject (readonly)

Returns the value of attribute message.



5
6
7
# File 'lib/light-service-ext/error_info.rb', line 5

def message
  @message
end

#titleObject (readonly)

Returns the value of attribute title.



5
6
7
# File 'lib/light-service-ext/error_info.rb', line 5

def title
  @title
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/light-service-ext/error_info.rb', line 5

def type
  @type
end

Instance Method Details

#backtraceObject



51
52
53
# File 'lib/light-service-ext/error_info.rb', line 51

def backtrace
  error&.backtrace || []
end

#clean_backtraceObject



55
56
57
58
59
60
61
# File 'lib/light-service-ext/error_info.rb', line 55

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

#error_summaryObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/light-service-ext/error_info.rb', line 19

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

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

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

    #{'=' * 56}
  TEXT
end

#errorsObject



32
33
34
35
36
37
# File 'lib/light-service-ext/error_info.rb', line 32

def errors
  model = error.try(:model) || error.try(:record)
  message = model.present? ? model.errors.messages : error.message

  { base: message }
end

#fatal_error?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/light-service-ext/error_info.rb', line 15

def fatal_error?
  @fatal || !non_fatal_error?
end

#non_fatal_error?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/light-service-ext/error_info.rb', line 63

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

#to_hObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/light-service-ext/error_info.rb', line 39

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