rpcStatus function

RpcError rpcStatus(
  1. int code, [
  2. Uint8List? details
])

Implementation

RpcError rpcStatus(int code, [Uint8List? details]) {
  var message = switch (code) {
    1 => 'Call cancelled',
    4 => 'Deadline exceeded',
    _ => 'RPC failed ($code)',
  };
  // core.v1.Error message is field 2. Preserve the entire original protobuf.
  if (details != null) {
    try {
      var position = 0;
      int varint() {
        var value = 0;
        for (var shift = 0; shift < 63; shift += 7) {
          final byte = details[position++];
          value |= (byte & 127) << shift;
          if (byte < 128) return value;
        }
        throw const FormatException('Malformed varint');
      }

      while (position < details.length) {
        final tag = varint();
        switch (tag & 7) {
          case 0:
            varint();
          case 1:
            position += 8;
          case 2:
            final size = varint();
            if (size < 0 || size > details.length - position) {
              throw const FormatException('Invalid length');
            }
            if (tag == 18) {
              message = utf8.decode(details.sublist(position, position + size));
            }
            position += size;
          case 5:
            position += 4;
          default:
            throw const FormatException('Unknown wire type');
        }
      }
    } on Object {
      // Malformed details must not replace the authoritative terminal status.
    }
  }
  return RpcError(code, message, details);
}