Exception: Foobara::Error

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

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…



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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'projects/typesystem/projects/common/src/error.rb', line 227

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

  if path.is_a?(::String) || path.is_a?(::Symbol)
    path = path.to_s.split(".").map(&:to_sym)
  end

  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|
    backtrace_line = backtrace_when_initialized[i]

    break unless backtrace_line

    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



10
11
12
# File 'projects/typesystem/projects/common/src/error.rb', line 10

def backtrace_when_initialized
  @backtrace_when_initialized
end

#backtrace_when_raisedObject

TODO: rename :path to data_path



10
11
12
# File 'projects/typesystem/projects/common/src/error.rb', line 10

def backtrace_when_raised
  @backtrace_when_raised
end

#contextObject

TODO: rename :path to data_path



10
11
12
# File 'projects/typesystem/projects/common/src/error.rb', line 10

def context
  @context
end

#error_keyObject

TODO: rename :path to data_path



10
11
12
# File 'projects/typesystem/projects/common/src/error.rb', line 10

def error_key
  @error_key
end

#is_fatalObject

TODO: rename :path to data_path



10
11
12
# File 'projects/typesystem/projects/common/src/error.rb', line 10

def is_fatal
  @is_fatal
end

#messageObject

TODO: rename :path to data_path



10
11
12
# File 'projects/typesystem/projects/common/src/error.rb', line 10

def message
  @message
end

Class Method Details

.abstractObject



15
16
17
# File 'projects/typesystem/projects/common/src/error.rb', line 15

def abstract
  @abstract = true
end

.abstract?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'projects/typesystem/projects/common/src/error.rb', line 19

def abstract?
  @abstract
end

.categoryObject



50
51
52
# File 'projects/typesystem/projects/common/src/error.rb', line 50

def category
  nil
end

.context(*args, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'projects/typesystem/projects/common/src/error.rb', line 72

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)


93
94
95
# File 'projects/typesystem/projects/common/src/error.rb', line 93

def fatal?
  false
end

.foobara_manifestObject



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
144
145
# File 'projects/typesystem/projects/common/src/error.rb', line 107

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'projects/typesystem/projects/common/src/error.rb', line 54

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?



42
43
44
# File 'projects/typesystem/projects/common/src/error.rb', line 42

def path
  ErrorKey::EMPTY_PATH
end

.runtime_pathObject



46
47
48
# File 'projects/typesystem/projects/common/src/error.rb', line 46

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



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
189
190
# File 'projects/typesystem/projects/common/src/error.rb', line 147

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'projects/typesystem/projects/common/src/error.rb', line 23

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



97
98
99
100
101
102
103
104
105
# File 'projects/typesystem/projects/common/src/error.rb', line 97

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 'projects/typesystem/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



296
297
298
# File 'projects/typesystem/projects/common/src/error.rb', line 296

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

#categoryObject



201
202
203
# File 'projects/typesystem/projects/common/src/error.rb', line 201

def category
  error_key.category
end

#category=(category) ⇒ Object



205
206
207
# File 'projects/typesystem/projects/common/src/error.rb', line 205

def category=(category)
  error_key.category = category
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


300
301
302
303
304
# File 'projects/typesystem/projects/common/src/error.rb', line 300

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

  symbol == other.symbol
end

#fatal?Boolean

Returns:

  • (Boolean)


288
289
290
# File 'projects/typesystem/projects/common/src/error.rb', line 288

def fatal?
  is_fatal
end

#keyObject



292
293
294
# File 'projects/typesystem/projects/common/src/error.rb', line 292

def key
  error_key.to_s
end

#pathObject



209
210
211
# File 'projects/typesystem/projects/common/src/error.rb', line 209

def path
  error_key.path
end

#path=(path) ⇒ Object



213
214
215
# File 'projects/typesystem/projects/common/src/error.rb', line 213

def path=(path)
  error_key.path = path
end

#prepend_path!Object



306
307
308
309
# File 'projects/typesystem/projects/common/src/error.rb', line 306

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

#runtime_pathObject



193
194
195
# File 'projects/typesystem/projects/common/src/error.rb', line 193

def runtime_path
  error_key.runtime_path
end

#runtime_path=(runtime_path) ⇒ Object



197
198
199
# File 'projects/typesystem/projects/common/src/error.rb', line 197

def runtime_path=(runtime_path)
  error_key.runtime_path = runtime_path
end

#symbolObject



217
218
219
# File 'projects/typesystem/projects/common/src/error.rb', line 217

def symbol
  error_key.symbol
end

#symbol=(symbol) ⇒ Object



221
222
223
# File 'projects/typesystem/projects/common/src/error.rb', line 221

def symbol=(symbol)
  error_key.symbol = symbol
end

#to_hObject



311
312
313
314
315
316
317
318
319
320
321
322
# File 'projects/typesystem/projects/common/src/error.rb', line 311

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