Exception: Exception

Defined in:
opal/opal/corelib/error.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Exception

Returns a new instance of Exception



21
22
23
24
# File 'opal/opal/corelib/error.rb', line 21

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



17
18
19
# File 'opal/opal/corelib/error.rb', line 17

def self.exception(*args)
  new(*args)
end

.new(*args) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'opal/opal/corelib/error.rb', line 2

def self.new(*args)
  %x{
    var message = (args.length > 0) ? args[0] : nil;
    var err = new self.$$alloc(message);

    if (Error.captureStackTrace) {
      Error.captureStackTrace(err);
    }

    err.name = self.$$name;
    err.$initialize.apply(err, args);
    return err;
  }
end

Instance Method Details

#backtraceObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'opal/opal/corelib/error.rb', line 26

def backtrace
  %x{
    var backtrace = self.stack;

    if (typeof(backtrace) === 'string') {
      return backtrace.split("\n").slice(0, 15);
    }
    else if (backtrace) {
      return backtrace.slice(0, 15);
    }

    return [];
  }
end

#exception(str = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'opal/opal/corelib/error.rb', line 41

def exception(str=nil)
  %x{
    if (str === nil || self === str) {
      return self;
    }
    
    var cloned = #{self.clone};
    cloned.message = str;
    return cloned;
  }
end

#inspectObject



58
59
60
61
# File 'opal/opal/corelib/error.rb', line 58

def inspect
  as_str = to_s
  as_str.empty? ? self.class.to_s : "#<#{self.class.to_s}: #{to_s}>"
end

#messageObject

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



54
55
56
# File 'opal/opal/corelib/error.rb', line 54

def message
  to_s
end

#to_sObject



63
64
65
66
# File 'opal/opal/corelib/error.rb', line 63

def to_s
  # using self.message aka @message to retain compatibility with native exception's message property
  (@message && @message.to_s) || self.class.to_s
end