Exception: Structured::Errors::MultipleErrors

Inherits:
Base
  • Object
show all
Defined in:
lib/structured/errors/multiple_errors.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#stack

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stack, errors) ⇒ MultipleErrors

Returns a new instance of MultipleErrors.



6
7
8
9
10
11
# File 'lib/structured/errors/multiple_errors.rb', line 6

def initialize(stack, errors)
  @errors = errors

  messages = errors.map { |msg| "- #{msg}" }.join("\n")
  super(stack, "Failed to parse #{stack.type.type_name} due to #{errors.length} errors:\n#{messages}")
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



4
5
6
# File 'lib/structured/errors/multiple_errors.rb', line 4

def errors
  @errors
end

#rootObject (readonly)

Returns the value of attribute root.



4
5
6
# File 'lib/structured/errors/multiple_errors.rb', line 4

def root
  @root
end

Class Method Details

.union(stack, exceptions) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/structured/errors/multiple_errors.rb', line 23

def self.union(stack, exceptions)
  return exceptions.first if exceptions.size == 1

  flattened_exceptions = exceptions.flat_map do |exception|
    case exception
    when MultipleErrors then exception.errors
    when Base then [exception]
    else raise "Cannot wrap a #{exception.class.name} in a ParsingFailed exception"
    end
  end

  new(stack, flattened_exceptions)
end

Instance Method Details

#annotation(header: true) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/structured/errors/multiple_errors.rb', line 13

def annotation(header: true)
  annotations = errors.map { |error| error.annotation(header: false) }
  if header
    head = "Failed to compile #{CGI.escape_html(stack.root.type.type_name)} due to #{errors.length} errors:"
    head + "\n\n" + annotations.join("\n\n")
  else
    annotations.join("\n\n")
  end
end