Module: Protocol::GRPC::Metadata

Defined in:
lib/protocol/grpc/metadata.rb

Class Method Summary collapse

Class Method Details

.assign_status!(headers, status: Status::OK, message: nil, error: nil) ⇒ Object Also known as: add_status!

Assign gRPC status, message, and optional backtrace to headers.

Whether these become headers or trailers is controlled by the protocol layer.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/protocol/grpc/metadata.rb', line 57

def self.assign_status!(headers, status: Status::OK, message: nil, error: nil)
	headers["grpc-status"] = status
	
	if error && message.nil?
		# If message is not provided but error is, use error message
		message = error.message
	end
	
	if message
		headers["grpc-message"] = message
	end
	
	# Add backtrace from error if available
	if error && error.backtrace && !error.backtrace.empty?
		# Assign backtrace array directly - Split header will handle it
		headers["backtrace"] = error.backtrace
	end
	
	return headers
end

.extract_message(headers) ⇒ Object

Extract gRPC status message from headers. Returns Nil if message is not present.



38
39
40
41
42
43
44
45
46
47
# File 'lib/protocol/grpc/metadata.rb', line 38

def self.extract_message(headers)
	# Ensure policy is set - setting policy clears the index (@indexed = nil)
	# The index will be rebuilt automatically on next access via to_h
	headers.policy = Protocol::GRPC::HEADER_POLICY unless headers.policy == Protocol::GRPC::HEADER_POLICY
	
	message = headers["grpc-message"]
	return nil unless message
	
	return message.decode
end

.extract_status(headers) ⇒ Object

Extract gRPC status from headers. Returns Status::UNKNOWN if status is not present.

Note: In Protocol::HTTP::Headers, trailers are merged into the headers so users just access headers regardless of whether it was sent as an initial header or trailer.



22
23
24
25
26
27
28
29
30
31
# File 'lib/protocol/grpc/metadata.rb', line 22

def self.extract_status(headers)
	# Ensure policy is set - setting policy clears the index (@indexed = nil)
	# The index will be rebuilt automatically on next access via to_h
	headers.policy = Protocol::GRPC::HEADER_POLICY unless headers.policy == Protocol::GRPC::HEADER_POLICY
	
	status = headers["grpc-status"]
	return Status::UNKNOWN unless status
	
	return status.to_i
end