Exception: Exception
- Defined in:
- opal/opal/corelib/error.rb
Direct Known Subclasses
Enumerator.self::Lazy.self::StopLazyError , NoMemoryError , ScriptError , SecurityError , SignalException , StandardError , SystemExit , SystemStackError
Class Method Summary collapse
Instance Method Summary collapse
- #backtrace ⇒ Object
- #backtrace_locations ⇒ Object
- #cause ⇒ Object
- #exception(str = nil) ⇒ Object
- #full_message(kwargs = nil) ⇒ Object
-
#initialize(*args) ⇒ Exception
constructor
A new instance of Exception.
- #inspect ⇒ Object
-
#message ⇒ Object
not using alias message to_s because you need to be able to override to_s and have message use overridden method, won't work with alias.
- #set_backtrace(backtrace) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(*args) ⇒ Exception
Returns a new instance of Exception.
34 35 36 37
# File 'opal/opal/corelib/error.rb', line 34 def initialize(*args) # using self.message aka @message to retain compatibility with native exception's message property `self.message = (args.length > 0) ? args[0] : nil` end
Class Method Details
.exception(*args) ⇒ Object
30 31 32
# File 'opal/opal/corelib/error.rb', line 30 def self.exception(*args) new(*args) end
.new(*args) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
# File 'opal/opal/corelib/error.rb', line 7 def self.new(*args) %x{ var message = (args.length > 0) ? args[0] : nil; var error = new self.$$constructor(message); error.name = self.$$name; error.message = message; error.cause = #{$!}; Opal.send(error, error.$initialize, args); // Error.captureStackTrace() will use .name and .toString to build the // first line of the stack trace so it must be called after the error // has been initialized. // https://nodejs.org/dist/latest-v6.x/docs/api/errors.html if (Opal.config.enable_stack_trace && Error.captureStackTrace) { // Passing Kernel.raise will cut the stack trace from that point above Error.captureStackTrace(error, stack_trace_limit); } return error; } end
Instance Method Details
#backtrace ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
# File 'opal/opal/corelib/error.rb', line 70 def backtrace %x{ if (self.backtrace) { // nil is a valid backtrace return self.backtrace; } var backtrace = self.stack; if (typeof(backtrace) !== 'undefined' && backtrace.$$is_string) { return self.backtrace = correct_backtrace(backtrace.split("\n")); } else if (backtrace) { return self.backtrace = correct_backtrace(backtrace); } return []; } end
#backtrace_locations ⇒ Object
90 91 92 93 94 95 96 97 98
# File 'opal/opal/corelib/error.rb', line 90 def backtrace_locations %x{ if (self.backtrace_locations) return self.backtrace_locations; self.backtrace_locations = #{backtrace&.map do |loc| ::Thread::Backtrace::Location.new(loc) end} return self.backtrace_locations; } end
#cause ⇒ Object
100 101 102
# File 'opal/opal/corelib/error.rb', line 100 def cause `self.cause || nil` end
#exception(str = nil) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117
# File 'opal/opal/corelib/error.rb', line 104 def exception(str = nil) %x{ if (str === nil || self === str) { return self; } var cloned = #{clone}; cloned.message = str; if (self.backtrace) cloned.backtrace = self.backtrace.$dup(); cloned.stack = self.stack; cloned.cause = self.cause; return cloned; } end
#full_message(kwargs = nil) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
# File 'opal/opal/corelib/error.rb', line 124 def full_message(kwargs = nil) unless defined? Hash # We are dealing with an unfully loaded Opal library, so we should # do with as little as we can. return "#{@message}\n#{`self.stack`}" end kwargs = { highlight: $stderr.tty?, order: :top }.merge(kwargs || {}) highlight, order = kwargs[:highlight], kwargs[:order] ::Kernel .raise ::ArgumentError , "expected true or false as highlight: #{highlight}" unless [true, false].include? highlight ::Kernel .raise ::ArgumentError , "expected :top or :bottom as order: #{order}" unless %i[topbottom].include? order if highlight bold_underline = "\e[1;4m" bold = "\e[1m" reset = "\e[m" else bold_underline = bold = reset = '' end bt = backtrace.dup bt = caller if !bt || bt.empty? first = bt.shift msg = "#{first}: " msg += "#{bold}#{to_s } (#{bold_underline}#{self.class}#{reset}#{bold})#{reset}\n" msg += bt.map { |loc| "\tfrom #{loc}\n" }.join msg += cause.full_message(highlight: highlight) if cause if order == :bottom msg = msg.split("\n").reverse.join("\n") msg = "#{bold}Traceback#{reset} (most recent call last):\n" + msg end msg end
#inspect ⇒ Object
#message ⇒ Object
not using alias message to_s because you need to be able to override to_s and have message use overridden method, won't work with alias
120 121 122
# File 'opal/opal/corelib/error.rb', line 120 def message to_s end
#set_backtrace(backtrace) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
# File 'opal/opal/corelib/error.rb', line 170 def set_backtrace(backtrace) %x{ var valid = true, i, ii; if (backtrace === nil) { self.backtrace = nil; self.stack = ''; } else if (backtrace.$$is_string) { self.backtrace = [backtrace]; self.stack = ' from ' + backtrace; } else { if (backtrace.$$is_array) { for (i = 0, ii = backtrace.length; i < ii; i++) { if (!backtrace[i].$$is_string) { valid = false; break; } } } else { valid = false; } if (valid === false) { #{::Kernel .raise ::TypeError , 'backtrace must be Array of String'} } self.backtrace = backtrace; self.stack = #{`backtrace`.map { |i| ' from ' + i }}.join("\n"); } return backtrace; } end