sendError static method

Future<void> sendError(
  1. P2PStream stream,
  2. String errorMessage,
  3. int errorCode, {
  4. Duration timeout = defaultTimeout,
  5. String context = 'unknown',
})

Send an error response

Convenience method for sending standardized error responses.

Implementation

static Future<void> sendError(
  core_p2p_stream.P2PStream stream,
  String errorMessage,
  int errorCode, {
  Duration timeout = defaultTimeout,
  String context = 'unknown',
}) async {
  final errorPayload = jsonEncode({
    'error_code': errorCode,
    'error_message': errorMessage,
    'timestamp': DateTime.now().toIso8601String(),
  });

  final errorFrame = OBPFrame(
    type: OBPMessageType.error,
    streamId: _nextStreamId(),
    payload: Uint8List.fromList(utf8.encode(errorPayload)),
  );

  await sendResponse(stream, errorFrame, timeout: timeout, context: context);
}