Class: ExceptionSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/javonet-ruby-sdk/utils/exceptions/exception_serializer.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/Linux/X64/utils/exceptions/exception_serializer.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/MacOs/X64/utils/exceptions/exception_serializer.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/Windows/X64/utils/exceptions/exception_serializer.rb

Class Method Summary collapse

Class Method Details

.append_to_string(string, value) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/javonet-ruby-sdk/utils/exceptions/exception_serializer.rb', line 63

def self.append_to_string(string, value)
  if value.nil?
    string << "undefined"
  else
    string << value
  end
end

.get_exception_code(exception_name) ⇒ Object

def self.parse_stack_frame(stack_frame)

# Match: path:line:in 'Class#method'
match = /^(.+):(\d+):in\s+'([^#]+)#([^']+)'$/.match(stack_frame)
return ["undefined", "undefined", "undefined", "undefined"] if match.nil?

stack_file = match[1]
stack_line = match[2]
stack_class = match[3]
stack_method = match[4]

[stack_file, stack_class, stack_line, stack_method]

end



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/javonet-ruby-sdk/utils/exceptions/exception_serializer.rb', line 105

def self.get_exception_code(exception_name)
  case exception_name
  when "Exception"
    return ExceptionType::EXCEPTION
  when "IOError"
    return ExceptionType::IO_EXCEPTION
  when "Errno::ENOENT"
    return ExceptionType::FILE_NOT_FOUND_EXCEPTION
  when "RuntimeError"
    return ExceptionType::RUNTIME_EXCEPTION
  when "ZeroDivisionError"
    return ExceptionType::ARITHMETIC_EXCEPTION
  when "ArgumentError"
    return ExceptionType::ILLEGAL_ARGUMENT_EXCEPTION
  when "IndexError"
    return ExceptionType::INDEX_OUT_OF_BOUNDS_EXCEPTION
  when "TypeError"
    return ExceptionType::NULL_POINTER_EXCEPTION
  else
    return ExceptionType::EXCEPTION
  end
end

.parse_stack_frame(stack_frame) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/javonet-ruby-sdk/utils/exceptions/exception_serializer.rb', line 71

def self.parse_stack_frame(stack_frame)
  # Extract the file path, line number, and method name using regular expressions

  # Handles both 'Class#method' and '<top (required)>'

  match = /^(.+):(\d+):in ['`](.+)['`]$/.match(stack_frame)
  return ["undefined", "undefined", "undefined", "undefined"] if match.nil?
  stack_file = match[1]
  stack_line = match[2]
  method_info = match[3]
  if method_info =~ /^(.+)#(.+)$/
    stack_class = $1&.split('::')&.last || 'undefined'
    stack_method = $2
  elsif method_info =~ /^<(.+)>$/
    stack_class = "undefined"
    stack_method = method_info
  else
    stack_class = "undefined"
    stack_method = method_info
  end
  [stack_file, stack_class, stack_line, stack_method]
end

.serialize_exception(exception, command) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/javonet-ruby-sdk/utils/exceptions/exception_serializer.rb', line 5

def self.serialize_exception(exception, command)
  exception_command = Command.new(command.runtime_name, CommandType::EXCEPTION, [])
  begin
    stack_trace = exception.backtrace
    exception_message = exception.message
    exception_name = "Ruby Exception"
    #exception_name = exception.class.to_s # does not work - causes native issues


    stack_classes = ""
    stack_methods = ""
    stack_lines = ""
    stack_files = ""
    if !stack_trace.nil? && !stack_trace.empty?
      stack_trace.each_with_index do |value, index|
        stack_file, stack_class, stack_line, stack_method = parse_stack_frame(value)
        # Extract the file name without the full path - commented for better debugging

        # To be re-enabled in the future

        #unless stack_file.include?("javonet-ruby-sdk") or stack_file.include?("Binaries/Ruby")

          append_to_string(stack_classes, stack_class)
          append_to_string(stack_methods, stack_method)
          append_to_string(stack_lines, stack_line)
          append_to_string(stack_files, stack_file)
          if index != stack_trace.length - 1
            append_to_string(stack_classes, "|")
            append_to_string(stack_methods, "|")
            append_to_string(stack_lines, "|")
            append_to_string(stack_files, "|")
          end
        #end # To be re-enabled in the future

        end
    end

    exception_command = exception_command.add_arg_to_payload(get_exception_code(exception_name.to_s))
    exception_command = exception_command.add_arg_to_payload(command.to_string)
    exception_command = exception_command.add_arg_to_payload(exception_name.to_s)
    exception_command = exception_command.add_arg_to_payload(exception_message)
    exception_command = exception_command.add_arg_to_payload(stack_classes)
    exception_command = exception_command.add_arg_to_payload(stack_methods)
    exception_command = exception_command.add_arg_to_payload(stack_lines)
    exception_command = exception_command.add_arg_to_payload(stack_files)
  rescue Exception => e
    exception_command = Command.new(RuntimeName::RUBY, CommandType::EXCEPTION, [])
    exception_command = exception_command.add_arg_to_payload(ExceptionType::EXCEPTION)
    if command.nil?
      exception_command = exception_command.add_arg_to_payload("Command is nil")
    else
      exception_command = exception_command.add_arg_to_payload(command.to_string)
    end
    exception_command = exception_command.add_arg_to_payload("Ruby Exception Serialization Error")
    exception_command = exception_command.add_arg_to_payload(e.message)
    exception_command = exception_command.add_arg_to_payload("ExceptionSerializer")
    exception_command = exception_command.add_arg_to_payload("serialize_exception")
    exception_command = exception_command.add_arg_to_payload("undefined")
    exception_command = exception_command.add_arg_to_payload(__FILE__)
  end
  exception_command
end