replyError function

String replyError(
  1. Object? id,
  2. String code,
  3. String message
)

Encodes a failed reply. Used by generated code.

Implementation

String replyError(Object? id, String code, String message) {
  final s = _escapeScriptSeparators(jsonEncode({'id': id, 'ok': false, 'e': {'code': code, 'message': message}}));
  if (utf8.encode(s).length <= maxMessageBytes) return s;
  // A malformed request can supply an arbitrary id before the nonce check.
  // Preserve only finite numeric correlation ids in the bounded fallback.
  return jsonEncode({
    'id': id is num && id.isFinite ? id : null,
    'ok': false,
    'e': {'code': BridgeErrorCode.tooLarge, 'message': 'error reply is over the $maxMessageBytes byte bridge cap'},
  });
}