Class: Fusion::Interpreter::ErrorVal

Inherits:
Object
  • Object
show all
Defined in:
lib/fusion/interpreter/error_val.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload, runtime: false) ⇒ ErrorVal

Returns a new instance of ErrorVal.



12
13
14
15
# File 'lib/fusion/interpreter/error_val.rb', line 12

def initialize(payload, runtime: false)
  @payload = payload
  @runtime = runtime
end

Instance Attribute Details

#payloadObject (readonly)

Returns the value of attribute payload.



10
11
12
# File 'lib/fusion/interpreter/error_val.rb', line 10

def payload
  @payload
end

Class Method Details

.from_runtime(kind:, origin:, operation:, input:, file: nil, expected: nil, message: nil) ⇒ Object

Build a runtime-produced error with the standardized payload shape documented in docs/user/reference.md §6.5.

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fusion/interpreter/error_val.rb', line 46

def self.from_runtime(kind:, origin:, operation:, input:, file: nil, expected: nil, message: nil)
  raise Unreachable, "an error with `expected` must not also carry a `message`" if expected && message

  received_error = input.is_a?(ErrorVal)

  payload = { "kind" => kind, "origin" => origin }
  payload["file"] = file if file
  payload["operation"] = operation
  payload["status"] = received_error ? 1 : 0
  payload["input"] = received_error ? input.payload : input
  payload["expected"] = expected if expected
  payload["message"] = message if message

  new(payload, runtime: true)
end

Instance Method Details

#inspectObject



62
63
64
# File 'lib/fusion/interpreter/error_val.rb', line 62

def inspect
  "!#{payload.inspect}"
end

#runtime?Boolean

Was this error runtime-produced (as opposed to user-constructed via !expr)? Runtime errors use lenient serialization (docs/user/reference.md §9.3) and get a call-site file stamped.

Returns:

  • (Boolean)


40
41
42
# File 'lib/fusion/interpreter/error_val.rb', line 40

def runtime?
  @runtime
end

#to_sObject



66
67
68
# File 'lib/fusion/interpreter/error_val.rb', line 66

def to_s
  inspect
end

#with_call_site(file) ⇒ Object

Attach the call-site file to a runtime error. Idempotent.

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fusion/interpreter/error_val.rb', line 18

def with_call_site(file)
  # Only stamp runtime-produced errors. After this check we are sure that the payload wasn't user-constructed.
  return self unless @runtime
  raise Unreachable, "Unexpected runtime error payload: #{@payload}" unless @payload.is_a?(Hash)
  # Don't double stamp. Idempotency.
  return self if @payload.key?("file")
  # Only stamp certain errors.
  return self unless ["builtin", "stdlib"].include?(@payload["origin"])

  # Insert "file" after "origin"
  reordered = {}
  @payload.each do |key, value|
    reordered[key] = value
    reordered["file"] = file if key == "origin"
  end
  @payload = reordered
  self
end