Class: Mfp::Proto::Error

Inherits:
Object
  • Object
show all
Defined in:
lib/mfp/proto/error.rb

Overview

Error is the payload of a MessageKindError frame. It signals a problem on either a stream or the connection. The enclosing frame's StreamID disambiguate the scope: zero for connection-level errors, non-zero for stream-level errors.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, terminal: false, debug: nil) ⇒ Error

Returns a new instance of Error.



12
13
14
15
16
# File 'lib/mfp/proto/error.rb', line 12

def initialize(code, terminal: false, debug: nil)
  @code = code
  @terminal = terminal
  @debug = debug
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



10
11
12
# File 'lib/mfp/proto/error.rb', line 10

def code
  @code
end

#debugObject

Returns the value of attribute debug.



10
11
12
# File 'lib/mfp/proto/error.rb', line 10

def debug
  @debug
end

#terminalObject

Returns the value of attribute terminal.



10
11
12
# File 'lib/mfp/proto/error.rb', line 10

def terminal
  @terminal
end

Class Method Details

.read(fr) ⇒ Object

Raises:



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mfp/proto/error.rb', line 38

def self.read(fr)
  raise ParserError.new(ErrorKind::FRAME_SIZE_ERROR) if fr.length < 5

  reader = Reader.from_bytes(fr.payload)
  terminal = fr.flags.allbits?(0x01)
  code = reader.read_byte
  debug_len = reader.read_u32
  debug = nil
  debug = reader.read_n(debug_len) if debug_len.positive?

  new(code, terminal:, debug:)
end

Instance Method Details

#encode(stream_id) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mfp/proto/error.rb', line 18

def encode(stream_id)
  buf = Writer.new
  buf.write_u8(@code)
  if (size = @debug&.bytesize || 0).positive?
    buf.write_u32(size)
    buf.write(@debug)
  else
    buf.write_u32(0)
  end

  Frame.new(
    call_sign: nil,
    stream_id:,
    kind: MessageKind::ERROR,
    flags: @terminal ? 1 : 0,
    length: buf.length,
    payload: buf.string,
  )
end