Exception: Alphred::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/alphred/error.rb

Constant Summary collapse

ASSETS =
File.expand_path('../../../assets', __FILE__)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error) ⇒ Error

Returns a new instance of Error.



18
19
20
# File 'lib/alphred/error.rb', line 18

def initialize(error)
  @error = error
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



16
17
18
# File 'lib/alphred/error.rb', line 16

def error
  @error
end

Class Method Details

.tryObject



10
11
12
13
14
# File 'lib/alphred/error.rb', line 10

def self.try
  yield
rescue Exception => e
  print self.new(e).to_json
end

Instance Method Details

#backtrace_itemsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/alphred/error.rb', line 34

def backtrace_items
  icon = File.join(ASSETS, 'more.png')

  base_dirs = $LOAD_PATH + [ENV['alfred_preferences']]
  to_delete = /^#{base_dirs.join(?|)}/

  self.error.backtrace.map { |line|
    title = line.sub(to_delete, '...')
    Item.new(
      title: title,
      subtitle: line,
      arg: line,
      icon: icon,
    )
  }
end

#to_itemsObject



26
27
28
29
30
31
32
# File 'lib/alphred/error.rb', line 26

def to_items
  icon = File.join(ASSETS, 'close.png')
  items = [ Alphred::Item.new(title: "#{self.error.class}: #{self.error}",
                              icon: icon) ]
  items.concat(self.backtrace_items)
  Items[*items]
end

#to_json(options = nil) ⇒ Object



22
23
24
# File 'lib/alphred/error.rb', line 22

def to_json(options=nil)
  to_items.to_json(options)
end