Exception: Cupper::Errors::CupperError

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CupperError

Returns a new instance of CupperError.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cupper/errors.rb', line 28

def initialize(*args)
  key     = args.shift if args.first.is_a?(Symbol)
  message = args.shift if args.first.is_a?(Hash)
  message ||= {}
  @extra_data    = message.dup
  message[:_key] ||= error_key
  message[:_namespace] ||= error_namespace
  message[:_key] = key if key
  I18n.load_path << File.expand_path("../../../templates/locales/en.yml", __FILE__)

  if message[:_key]
    message = translate_error(message)
  else
    message = error_message
  end

  super(message)
end

Instance Attribute Details

#extra_dataObject

Returns the value of attribute extra_data.



8
9
10
# File 'lib/cupper/errors.rb', line 8

def extra_data
  @extra_data
end

Class Method Details

.error_key(key = nil, namespace = nil) ⇒ Object



10
11
12
13
# File 'lib/cupper/errors.rb', line 10

def self.error_key(key=nil, namespace=nil)
  define_method(:error_key) { key }
  error_namespace(namespace) if namespace
end

.error_message(message) ⇒ Object



15
16
17
# File 'lib/cupper/errors.rb', line 15

def self.error_message(message)
  define_method(:error_message) { message }
end

.error_namespace(namespace) ⇒ Object



19
20
21
# File 'lib/cupper/errors.rb', line 19

def self.error_namespace(namespace)
  define_method(:error_namespace) { namespace }
end

Instance Method Details

#error_keyObject

The key for the error message. This should be set using the error_key method but can be overridden here if needed.



58
# File 'lib/cupper/errors.rb', line 58

def error_key; nil; end

#error_messageObject

The error message for this error. This is used if no error_key is specified for a translatable error message.



49
# File 'lib/cupper/errors.rb', line 49

def error_message; "No error message"; end

#error_namespaceObject

The default error namespace which is used for the error key. This can be overridden here or by calling the “error_namespace” class method.



54
# File 'lib/cupper/errors.rb', line 54

def error_namespace; "cupper.errors"; end

#status_codeInteger

This is the exit code that should be used when exiting from this exception.

Returns:

  • (Integer)


64
# File 'lib/cupper/errors.rb', line 64

def status_code; 1; end

#translate_error(opts) ⇒ Object



23
24
25
26
# File 'lib/cupper/errors.rb', line 23

def translate_error(opts)
  return nil if !opts[:_key]
  I18n.t("#{opts[:_namespace]}.#{opts[:_key]}", opts)
end