Exception: Foobara::Error

Inherits:
StandardError
  • Object
show all
Includes:
Manifestable
Defined in:
foobara-0.0.110/projects/common/src/error.rb,
foobara-0.0.110/projects/types/src/extensions/error.rb

Direct Known Subclasses

RuntimeError, Value::DataError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concern

foobara_class_methods_module_for, foobara_concern?, included

Constructor Details

#initialize(path: self.class.path, runtime_path: self.class.runtime_path, category: self.class.category, message: self.class.message, symbol: self.class.symbol, context: self.class.context, is_fatal: self.class.fatal?) ⇒ Error

TODO: seems like we should not allow the symbol to vary within instances of a class TODO: any items serializable in self.class.to_h should not be overrideable like this…



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'foobara-0.0.110/projects/common/src/error.rb', line 203

def initialize(
  path: self.class.path,
  runtime_path: self.class.runtime_path,
  category: self.class.category,
  message: self.class.message,
  symbol: self.class.symbol,
  context: self.class.context,
  is_fatal: self.class.fatal?
)
  self.error_key = ErrorKey.new

  self.symbol = symbol
  self.message = message
  self.context = context
  self.category = category
  self.path = path
  self.runtime_path = runtime_path
  self.is_fatal = is_fatal

  if !self.message.is_a?(String) || self.message.empty?
    # :nocov:
    raise "Bad error message, expected a string"
    # :nocov:
  end

  backtrace_when_initialized = caller[1..]
  index = 1

  has_build_error = false

  1.upto(10) do |i|
    if backtrace_when_initialized[i].end_with?("#build_error'")
      index = i + 1
      has_build_error = true
      break
    end
  end

  if has_build_error
    index.upto(10) do |i|
      unless backtrace_when_initialized[i].end_with?("#build_error'")
        index = i
        break
      end
    end
  end

  self.backtrace_when_initialized = backtrace_when_initialized[index..]

  super(message)
end

Instance Attribute Details

#backtrace_when_initializedObject

TODO: rename :path to data_path



8
9
10
# File 'foobara-0.0.110/projects/common/src/error.rb', line 8

def backtrace_when_initialized
  @backtrace_when_initialized
end

#backtrace_when_raisedObject

TODO: rename :path to data_path



8
9
10
# File 'foobara-0.0.110/projects/common/src/error.rb', line 8

def backtrace_when_raised
  @backtrace_when_raised
end

#contextObject

TODO: rename :path to data_path



8
9
10
# File 'foobara-0.0.110/projects/common/src/error.rb', line 8

def context
  @context
end

#error_keyObject

TODO: rename :path to data_path



8
9
10
# File 'foobara-0.0.110/projects/common/src/error.rb', line 8

def error_key
  @error_key
end

#is_fatalObject

TODO: rename :path to data_path



8
9
10
# File 'foobara-0.0.110/projects/common/src/error.rb', line 8

def is_fatal
  @is_fatal
end

#messageObject

TODO: rename :path to data_path



8
9
10
# File 'foobara-0.0.110/projects/common/src/error.rb', line 8

def message
  @message
end

Class Method Details

.abstractObject



13
14
15
# File 'foobara-0.0.110/projects/common/src/error.rb', line 13

def abstract
  @abstract = true
end

.abstract?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'foobara-0.0.110/projects/common/src/error.rb', line 17

def abstract?
  @abstract
end

.categoryObject



48
49
50
# File 'foobara-0.0.110/projects/common/src/error.rb', line 48

def category
  nil
end

.context(*args, &block) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'foobara-0.0.110/projects/common/src/error.rb', line 70

def context(*args, &block)
  if block_given?
    args = [*args, block]
  end
  args_size = args.size

  case args_size
  when 0
    {}
  when 1
    arg = args.first
    singleton_class.define_method :context_type_declaration do
      arg
    end
  else
    # :nocov:
    raise ArgumentError, "expected 0 or 1 argument, got #{args_size}"
    # :nocov:
  end
end

.fatal?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'foobara-0.0.110/projects/common/src/error.rb', line 91

def fatal?
  false
end

.foobara_manifestObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'foobara-0.0.110/projects/common/src/error.rb', line 105

def foobara_manifest
  to_include = TypeDeclarations.foobara_manifest_context_to_include

  types = types_depended_on.map do |t|
    if to_include
      to_include << t
    end
    t.foobara_manifest_reference
  end

  base = nil
  # don't bother including these core errors
  unless superclass == Foobara::Error
    base = superclass
    if to_include
      to_include << base
    end
  end

  manifest = super

  unless types.empty?
    manifest[:types_depended_on] = types.sort
  end

  h = manifest.merge(Util.remove_blank(to_h)).merge(
    error_class: name
  )

  if base
    h[:base_error] = base.foobara_manifest_reference
  end

  if abstract?
    h[:abstract] = true
  end

  h
end

.message(*args) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'foobara-0.0.110/projects/common/src/error.rb', line 52

def message(*args)
  args_size = args.size

  case args_size
  when 0
    Util.humanize(symbol.to_s)
  when 1
    arg = args.first
    singleton_class.define_method :message do
      arg
    end
  else
    # :nocov:
    raise ArgumentError, "expected 0 or 1 argument, got #{args_size}"
    # :nocov:
  end
end

.pathObject

Is this actually used?



40
41
42
# File 'foobara-0.0.110/projects/common/src/error.rb', line 40

def path
  ErrorKey::EMPTY_PATH
end

.runtime_pathObject



44
45
46
# File 'foobara-0.0.110/projects/common/src/error.rb', line 44

def runtime_path
  ErrorKey::EMPTY_PATH
end

.subclass(context: {}, name: nil, symbol: nil, message: nil, base_error: self, mod: base_error, category: base_error.category, is_fatal: false, abstract: false) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'foobara-0.0.110/projects/common/src/error.rb', line 145

def subclass(
  # TODO: technically context doesn't belong here. But maybe it should.
  context: {},
  name: nil,
  symbol: nil,
  message: nil,
  base_error: self,
  mod: base_error,
  category: base_error.category,
  is_fatal: false,
  abstract: false
)
  name ||= [*mod.name, "#{Util.classify(symbol)}Error"].join("::")

  klass = Util.make_class_p(name, base_error) do
    singleton_class.define_method :category do
      category
    end

    if symbol
      singleton_class.define_method :symbol do
        symbol
      end
    end

    singleton_class.define_method :fatal? do
      is_fatal
    end

    singleton_class.define_method :context_type_declaration do
      context
    end

    if message
      singleton_class.define_method :message do
        message
      end
    end
  end

  klass.abstract if abstract

  klass
end

.symbol(*args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'foobara-0.0.110/projects/common/src/error.rb', line 21

def symbol(*args)
  args_size = args.size

  case args_size
  when 0
    Util.non_full_name_underscore(self).gsub(/_error$/, "").to_sym
  when 1
    arg = args.first
    singleton_class.define_method :symbol do
      arg
    end
  else
    # :nocov:
    raise ArgumentError, "expected 0 or 1 argument, got #{args_size}"
    # :nocov:
  end
end

.to_hObject



95
96
97
98
99
100
101
102
103
# File 'foobara-0.0.110/projects/common/src/error.rb', line 95

def to_h
  {
    category:,
    symbol:,
    # TODO: this is a bad dependency direction but maybe time to bite the bullet and finally merge these...
    context_type_declaration: context_type&.declaration_data,
    is_fatal: fatal?
  }
end

.types_depended_on(*args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'foobara-0.0.110/projects/types/src/extensions/error.rb', line 4

def types_depended_on(*args)
  if args.size == 1
    context_type.types_depended_on(args.first)
  elsif args.empty?
    begin
      if context_type
        context_type.types_depended_on
      else
        raise Foobara::TypeDeclarations::ErrorExtension::NoContextTypeSetError
      end
    rescue Foobara::TypeDeclarations::ErrorExtension::NoContextTypeSetError
      if abstract?
        []
      else
        # :nocov:
        raise
        # :nocov:
      end
    end

  else
    # :nocov:
    raise ArgumentError, "Too many arguments #{args}"
    # :nocov:
  end
end

Instance Method Details

#==(other) ⇒ Object



263
264
265
# File 'foobara-0.0.110/projects/common/src/error.rb', line 263

def ==(other)
  equal?(other) || eql?(other)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


267
268
269
270
271
# File 'foobara-0.0.110/projects/common/src/error.rb', line 267

def eql?(other)
  return false unless other.is_a?(Error)

  symbol == other.symbol
end

#fatal?Boolean

Returns:

  • (Boolean)


255
256
257
# File 'foobara-0.0.110/projects/common/src/error.rb', line 255

def fatal?
  is_fatal
end

#keyObject



259
260
261
# File 'foobara-0.0.110/projects/common/src/error.rb', line 259

def key
  error_key.to_s
end

#prepend_path!Object



273
274
275
276
# File 'foobara-0.0.110/projects/common/src/error.rb', line 273

def prepend_path!(...)
  error_key.prepend_path!(...)
  self
end

#to_hObject



278
279
280
281
282
283
284
285
286
287
288
289
# File 'foobara-0.0.110/projects/common/src/error.rb', line 278

def to_h
  {
    key:,
    path:,
    runtime_path:,
    category:,
    symbol:,
    message:,
    context:,
    is_fatal: fatal?
  }
end