Exception: ReeErrors::Error

Inherits:
StandardError
  • Object
show all
Includes:
Ree::LinkDSL
Defined in:
lib/ree_lib/packages/ree_errors/package/ree_errors/error.rb

Instance Method Summary collapse

Constructor Details

#initialize(msg = nil) ⇒ Error

Returns a new instance of Error.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ree_lib/packages/ree_errors/package/ree_errors/error.rb', line 9

def initialize(msg = nil)
  if !msg && !locale && !default_msg
    raise ArgumentError, "message, locale or default message should be specified"
  end

  message = if msg
    msg
  elsif default_msg
    default_msg
  else
    path = (locale || code).to_s
    has_path = path.include?(".")

    if has_path
      pre_path = [caller_module, caller_class].compact.map { underscore(_1)}.join(".")

      if check_locale_exists(path)
        t(path, default_by_locale: :en)
      else
        t("#{pre_path}.#{path}", default_by_locale: :en)
      end
    else
      if caller_module
        mod = underscore(caller_module)
        klass = underscore(caller_class)

        if check_locale_exists(loc = "#{mod}.errors.#{klass}.#{path}")
          t(loc, default_by_locale: :en)
        else check_locale_exists(loc = "#{mod}.errors.#{path}")
          t(loc, default_by_locale: :en)
        end
      else
      end
    end
  end

  super(message)
end

Instance Method Details

#callerObject



68
69
70
# File 'lib/ree_lib/packages/ree_errors/package/ree_errors/error.rb', line 68

def caller
  self.class.instance_variable_get(:@caller)
end

#caller_classObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ree_lib/packages/ree_errors/package/ree_errors/error.rb', line 88

def caller_class
  @caller_class ||= begin
    if !caller
      nil
    else
      c = caller

      if c.is_a?(Class)
        extract_class_name(c.to_s)
      else
        extract_class_name(c.class.to_s)
      end
    end
  end
end

#caller_moduleObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ree_lib/packages/ree_errors/package/ree_errors/error.rb', line 72

def caller_module
  @caller_module ||= begin
    if !caller
      nil
    else
      c = caller

      if c.is_a?(Class)
        extract_module_name(c.to_s)
      else
        extract_module_name(c.class.to_s)
      end
    end
  end
end

#codeObject



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ree_lib/packages/ree_errors/package/ree_errors/error.rb', line 104

def code
  c = self.class.instance_variable_get(:@code)

  if !c
    raise ArgumentError.new(
      "code not specified for error => #{self.inspect}"
    )
  end

  c
end

#default_msgObject



60
61
62
# File 'lib/ree_lib/packages/ree_errors/package/ree_errors/error.rb', line 60

def default_msg
  self.class.instance_variable_get(:@default_msg)
end

#localeObject



64
65
66
# File 'lib/ree_lib/packages/ree_errors/package/ree_errors/error.rb', line 64

def locale
  self.class.instance_variable_get(:@locale)
end

#typeObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ree_lib/packages/ree_errors/package/ree_errors/error.rb', line 48

def type
  t = self.class.instance_variable_get(:@type)

  if !t
    raise ArgumentError.new(
      "type was not specified for domain error => #{self.inspect}"
    )
  end

  t
end