Class: BloggerException

Inherits:
Object
  • Object
show all
Defined in:
lib/BloggerException.rb

Constant Summary collapse

DESCRIPTIONS =
{
  ZeroDivisionError: "Attempting to divide an Integer by 0.", 
  NoMethodError: "A method is called on a receiver that does not have that method defined."
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBloggerException

Returns a new instance of BloggerException.



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/BloggerException.rb', line 2

def initialize
  @file = $0
  @line = $!.backtrace[0].split(':')[1]
  @cause = $!.backtrace[0].split('`')[-1]
  @message = $!.message
  @type = $!.class.to_s
  @description = generate_description(self.type.to_sym)
  @scope = $!.backtrace[0].split(/\./)[0]
  @time = Time.now.strftime("%I:%M:%S %p")
  @date = Time.now.strftime("%m/%d/%y")
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



54
55
56
# File 'lib/BloggerException.rb', line 54

def date
  @date
end

#descriptionObject (readonly)

Returns the value of attribute description.



54
55
56
# File 'lib/BloggerException.rb', line 54

def description
  @description
end

#fileObject (readonly)

Returns the value of attribute file.



54
55
56
# File 'lib/BloggerException.rb', line 54

def file
  @file
end

#lineObject (readonly)

Returns the value of attribute line.



54
55
56
# File 'lib/BloggerException.rb', line 54

def line
  @line
end

#timeObject (readonly)

Returns the value of attribute time.



54
55
56
# File 'lib/BloggerException.rb', line 54

def time
  @time
end

#typeObject (readonly)

Returns the value of attribute type.



54
55
56
# File 'lib/BloggerException.rb', line 54

def type
  @type
end

Instance Method Details

#displayObject



32
33
34
35
36
37
38
39
40
# File 'lib/BloggerException.rb', line 32

def display
  puts "  \\tClass \#{self.type}\n  \\tDescription: \#{self.description}\n  \\tLine: \#{self.line}\n  \\tTime: \#{self.time}\n  \\tDate: \#{self.date}\n  MLS\nend\n"

#generate_description(type) ⇒ Object



49
50
51
52
# File 'lib/BloggerException.rb', line 49

def generate_description(type)
  return DESCRIPTIONS[type] if DESCRIPTIONS.key?(type)
  return 'Check out https://ruby-doc.org/core-2.7.0/Exception.html for more information.'
end

#logObject



42
43
44
45
46
47
# File 'lib/BloggerException.rb', line 42

def log
  File.open('structured_exceptions.yml', 'a+') do |file|
    file.write (self.to_yaml)
  end 
  puts 'Bug Logged Successfully:'
end

#to_sObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/BloggerException.rb', line 19

def to_s
  "  :file: \#{file}\n  :line: \#{line}\n  :cause: \#{cause}\n  :message: \#{message}\n  :type: \#{type}\n  :description \#{description}\n  :scope: \#{scope}\n  :time: \#{time}\n  MLS\nend\n"