Exception: Exception

Defined in:
lib/openc3/core_ext/exception.rb,
lib/openc3/io/json_rpc.rb

Overview

OpenC3 specific additions to the Ruby Exception class

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_hash(hash) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/openc3/io/json_rpc.rb', line 160

def self.from_hash(hash)
  begin
    # Get Error class handling namespaced constants
    split_error_class_name = hash['class'].split("::")
    error_class = Object
    split_error_class_name.each do |name|
      error_class = error_class.const_get(name)
    end
  rescue
    error = OpenC3::JsonDRbUnknownError.new(hash['message'])
    error.set_backtrace(hash['backtrace'].concat(caller()))
    raise error
  end
  error = error_class.new(hash['message'])
  error.set_backtrace(hash['backtrace'].concat(caller())) if hash['backtrace']
  hash['instance_variables'].each do |name, value|
    error.instance_variable_set(name.intern, value)
  end
  error
end

Instance Method Details

#as_json(*a) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/openc3/io/json_rpc.rb', line 143

def as_json(*a)
  hash = {}
  hash['class'] = self.class.name
  hash['message'] = self.message
  hash['backtrace'] = self.backtrace
  instance_vars = {}
  self.instance_variables.each do |instance_var_name|
    instance_vars[instance_var_name.to_s] = self.instance_variable_get(instance_var_name.to_s.intern)
  end
  hash['instance_variables'] = instance_vars
  hash.as_json(*a)
end

#filteredObject



25
26
27
28
29
30
# File 'lib/openc3/core_ext/exception.rb', line 25

def filtered
  backtrace = self.backtrace.select do |line|
    !line.include?('lib/ruby/gems')
  end
  return "#{self.message}\n#{backtrace.join("\n")}"
end

#formatted(hide_runtime_error_class = false, include_backtrace = true) ⇒ String

Returns The formatted Exception.

Parameters:

  • hide_runtime_error_class (Boolean) (defaults to: false)

    Whether to hide the Exception error class if the class is RuntimeError. Other classes will continue to be printed.

  • include_backtrace (Boolean) (defaults to: true)

    Whether to include the full exception backtrace

Returns:

  • (String)

    The formatted Exception



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/openc3/core_ext/exception.rb', line 38

def formatted(hide_runtime_error_class = false, include_backtrace = true)
  if include_backtrace and self.backtrace
    if hide_runtime_error_class and self.class == RuntimeError
      "#{self.message}\n#{self.backtrace.join("\n")}"
    else
      "#{self.class.to_s.split('::')[-1]} : #{self.message}\n#{self.backtrace.join("\n")}"
    end
  else
    if hide_runtime_error_class and self.class == RuntimeError
      "#{self.message}"
    else
      "#{self.class.to_s.split('::')[-1]} : #{self.message}"
    end
  end
end

#sourceArray(String, Fixnum)

Returns The filename and line number where the Exception occurred.

Returns:

  • (Array(String, Fixnum))

    The filename and line number where the Exception occurred



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/openc3/core_ext/exception.rb', line 56

def source
  trace = self.backtrace[0]
  split_trace = trace.split(':')
  filename = ''
  line_number = ''
  if trace[1..1] == ':' # Windows Path
    filename = split_trace[0] + ':' + split_trace[1]
    line_number = split_trace[2].to_i
  else
    filename = split_trace[0]
    line_number = split_trace[1].to_i
  end

  [filename, line_number]
end

#to_json(*a) ⇒ Object



156
157
158
# File 'lib/openc3/io/json_rpc.rb', line 156

def to_json(*a)
  as_json(*a).to_json(*a)
end