sendError method

  1. @override
Future sendError(
  1. int id,
  2. String topic,
  3. String method,
  4. JsonRpcError error, {
  5. EncodeOptions? encodeOptions,
  6. RpcOptions? rpcOptions,
  7. String? appLink,
})
override

Implementation

@override
Future<dynamic> sendError(
  int id,
  String topic,
  String method,
  JsonRpcError error, {
  EncodeOptions? encodeOptions,
  RpcOptions? rpcOptions,
  String? appLink,
}) async {
  final Map<String, dynamic> payload = JsonRpcUtils.formatJsonRpcError(
    id,
    error,
  );

  final String? message = await core.crypto.encode(
    topic,
    payload,
    options: encodeOptions,
  );

  if (message == null) {
    return;
  }

  core.logger.d(
    '[$runtimeType] sendRequest appLink: $appLink, '
    'id: $id topic: $topic, method: $method, error: $error',
  );
  if ((appLink ?? '').isNotEmpty) {
    final redirectURL = ReownCoreUtils.getLinkModeURL(
      appLink!,
      topic,
      message,
    );
    await ReownCoreUtils.openURL(redirectURL);
  } else {
    final fallbackMethod = MethodConstants.UNREGISTERED_METHOD;
    final methodOpts = MethodConstants.RPC_OPTS[method];
    final fallbackMethodOpts = MethodConstants.RPC_OPTS[fallbackMethod]!;
    final relayOpts = methodOpts ?? fallbackMethodOpts;
    final fallbackOpts = relayOpts['reject'] ?? relayOpts['res']!;
    await core.relayClient.publish(
      topic: topic,
      message: message,
      ttl: (rpcOptions ?? fallbackOpts).ttl,
      tag: (rpcOptions ?? fallbackOpts).tag,
    );
  }
}