Exception: JSON5::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/json5/errors.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, code: "parse_error", filename: nil, byte_offset: nil, end_byte_offset: nil, line: nil, column: nil, unexpected: nil, expected: nil, excerpt: nil) ⇒ Error

Returns a new instance of Error.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/json5/errors.rb', line 8

def initialize(message = nil, code: "parse_error", filename: nil,
               byte_offset: nil, end_byte_offset: nil, line: nil,
               column: nil, unexpected: nil, expected: nil, excerpt: nil)
  @code = code.to_s.freeze
  @filename = filename
  @byte_offset = byte_offset
  @end_byte_offset = end_byte_offset
  @line = line
  @column = column
  @unexpected = unexpected
  @expected = expected&.map(&:to_s)&.freeze
  @excerpt = excerpt
  super(message || @code)
end

Instance Attribute Details

#byte_offsetObject (readonly)

Returns the value of attribute byte_offset.



5
6
7
# File 'lib/json5/errors.rb', line 5

def byte_offset
  @byte_offset
end

#codeObject (readonly)

Returns the value of attribute code.



5
6
7
# File 'lib/json5/errors.rb', line 5

def code
  @code
end

#columnObject (readonly)

Returns the value of attribute column.



5
6
7
# File 'lib/json5/errors.rb', line 5

def column
  @column
end

#end_byte_offsetObject (readonly)

Returns the value of attribute end_byte_offset.



5
6
7
# File 'lib/json5/errors.rb', line 5

def end_byte_offset
  @end_byte_offset
end

#excerptObject (readonly)

Returns the value of attribute excerpt.



5
6
7
# File 'lib/json5/errors.rb', line 5

def excerpt
  @excerpt
end

#expectedObject (readonly)

Returns the value of attribute expected.



5
6
7
# File 'lib/json5/errors.rb', line 5

def expected
  @expected
end

#filenameObject (readonly)

Returns the value of attribute filename.



5
6
7
# File 'lib/json5/errors.rb', line 5

def filename
  @filename
end

#lineObject (readonly)

Returns the value of attribute line.



5
6
7
# File 'lib/json5/errors.rb', line 5

def line
  @line
end

#unexpectedObject (readonly)

Returns the value of attribute unexpected.



5
6
7
# File 'lib/json5/errors.rb', line 5

def unexpected
  @unexpected
end

Instance Method Details

#to_hObject



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

def to_h
  {
    code: code,
    message: message,
    filename: filename,
    byte_offset: byte_offset,
    end_byte_offset: end_byte_offset,
    line: line,
    column: column,
    unexpected: unexpected,
    expected: expected,
    excerpt: excerpt
  }
end

#with_context(filename: self.filename, byte_offset: self.byte_offset, end_byte_offset: self.end_byte_offset, line: self.line, column: self.column, excerpt: self.excerpt) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/json5/errors.rb', line 38

def with_context(filename: self.filename, byte_offset: self.byte_offset,
                 end_byte_offset: self.end_byte_offset, line: self.line,
                 column: self.column, excerpt: self.excerpt)
  return self if self.byte_offset && self.end_byte_offset && self.line && self.column && self.excerpt

  enriched = self.class.new(
    message,
    code: code,
    filename: filename,
    byte_offset: byte_offset,
    end_byte_offset: end_byte_offset,
    line: line,
    column: column,
    unexpected: unexpected,
    expected: expected,
    excerpt: excerpt
  )
  enriched.set_backtrace(backtrace) if backtrace
  enriched
end