Exception: BusinessError::Error

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, msg, code, http_status = Config.default_http_status, format = Config.default_format) ⇒ Error

Returns a new instance of Error.



9
10
11
12
13
# File 'lib/business_error/error.rb', line 9

def initialize(name, msg, code, http_status = Config.default_http_status, format = Config.default_format)
  msg = name.to_s.humanize if msg.blank?
  @name, @msg, @code, @http_status = name, msg, code, http_status
  self.format = format
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



7
8
9
# File 'lib/business_error/error.rb', line 7

def code
  @code
end

#formatObject

Returns the value of attribute format.



7
8
9
# File 'lib/business_error/error.rb', line 7

def format
  @format
end

#http_statusObject

Returns the value of attribute http_status.



7
8
9
# File 'lib/business_error/error.rb', line 7

def http_status
  @http_status
end

#msgObject

Returns the value of attribute msg.



7
8
9
# File 'lib/business_error/error.rb', line 7

def msg
  @msg
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/business_error/error.rb', line 7

def name
  @name
end

Instance Method Details

#format!(template, **addition_content) ⇒ Object Also known as: render!

Raises:

  • (self)


23
24
25
26
27
# File 'lib/business_error/error.rb', line 23

def format!(template, **addition_content)
  content = Config.formats[template].each_with_index.map { |k, i| [k, info.values[i]] }.to_h
  @info = { only: content.merge(addition_content) }
  raise self
end

#infoObject



15
16
17
# File 'lib/business_error/error.rb', line 15

def info
  @info ||= { code: @code, msg: @msg, http: @http_status }
end

#messageObject



36
# File 'lib/business_error/error.rb', line 36

def message; info.to_s end

#throw!Object



19
20
21
# File 'lib/business_error/error.rb', line 19

def throw!
  format ? format!(format) : (raise self)
end

#with!(**addition_content) ⇒ Object

Raises:

  • (self)


31
32
33
34
# File 'lib/business_error/error.rb', line 31

def with!(**addition_content)
  info.merge!(data: addition_content)
  raise self
end