Exception: MotherBrain::MBError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/mb/errors.rb

Constant Summary collapse

DEFAULT_EXIT_CODE =
1

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil) ⇒ MBError

Returns a new instance of MBError.

Parameters:

  • message (String) (defaults to: nil)


52
53
54
55
# File 'lib/mb/errors.rb', line 52

def initialize(message = nil)
  super(message)
  @message = message
end

Class Method Details

.error_code(code = -1)) ⇒ Integer

Parameters:

  • code (Integer) (defaults to: -1))

Returns:

  • (Integer)


43
44
45
46
47
48
# File 'lib/mb/errors.rb', line 43

def error_code(code = -1)
  return @error_code if @error_code
  @error_code = code
  Errors.register(self)
  @error_code
end

.exit_code(code = DEFAULT_EXIT_CODE) ⇒ Integer

Parameters:

  • code (Integer) (defaults to: DEFAULT_EXIT_CODE)

Returns:

  • (Integer)


36
37
38
# File 'lib/mb/errors.rb', line 36

def exit_code(code = DEFAULT_EXIT_CODE)
  @exit_code ||= code
end

Instance Method Details

#error_codeInteger

Returns:

  • (Integer)


63
64
65
# File 'lib/mb/errors.rb', line 63

def error_code
  self.class.error_code
end

#exit_codeInteger

Returns:

  • (Integer)


58
59
60
# File 'lib/mb/errors.rb', line 58

def exit_code
  self.class.exit_code
end

#messageString

Returns:

  • (String)


68
69
70
# File 'lib/mb/errors.rb', line 68

def message
  @message || self.class.to_s
end

#to_hashObject



76
77
78
79
80
81
# File 'lib/mb/errors.rb', line 76

def to_hash
  {
    code: error_code,
    message: message
  }
end

#to_json(options = {}) ⇒ String

Parameters:

  • options (Hash) (defaults to: {})

    a set of options to pass to MultiJson.encode

Returns:

  • (String)


87
88
89
# File 'lib/mb/errors.rb', line 87

def to_json(options = {})
  MultiJson.encode(self.to_hash, options)
end

#to_sObject



72
73
74
# File 'lib/mb/errors.rb', line 72

def to_s
  "[err_code]: #{error_code} [message]: #{message}"
end