getSolanaErrorFromJsonRpcError function

SolanaError getSolanaErrorFromJsonRpcError(
  1. Object? putativeErrorResponse
)

Converts a JSON-RPC error response into a SolanaError.

putativeErrorResponse should be a map with code, message, and optional data fields (standard JSON-RPC error format).

Implementation

SolanaError getSolanaErrorFromJsonRpcError(Object? putativeErrorResponse) {
  if (_isRpcErrorResponse(putativeErrorResponse)) {
    final response = putativeErrorResponse! as Map<String, Object?>;
    final rawCode = response['code']!;
    final codeInt = rawCode is BigInt
        ? rawCode.toInt()
        : (rawCode as num).toInt();
    final message = response['message']! as String;
    final data = response['data'];

    final code = SolanaErrorCode.fromValue(codeInt);

    if (code == null) {
      return SolanaError(SolanaErrorCode.malformedJsonRpcError, {
        'error': putativeErrorResponse,
        'message': message,
      });
    }

    if (code ==
        SolanaErrorCode.jsonRpcServerErrorSendTransactionPreflightFailure) {
      final preflightData = data as Map<String, Object?>? ?? {};
      final err = preflightData['err'];
      final preflightContext = Map<String, Object?>.from(preflightData)
        ..remove('err');
      _addSendTransactionPreflightDefaults(preflightContext);

      if (err != null) {
        final cause = getSolanaErrorFromTransactionError(err);
        preflightContext['cause'] = cause;
      }

      return SolanaError(
        SolanaErrorCode.jsonRpcServerErrorSendTransactionPreflightFailure,
        preflightContext,
      );
    }

    Map<String, Object?>? errorContext;

    switch (code) {
      case SolanaErrorCode.jsonRpcInternalError:
      case SolanaErrorCode.jsonRpcInvalidParams:
      case SolanaErrorCode.jsonRpcInvalidRequest:
      case SolanaErrorCode.jsonRpcMethodNotFound:
      case SolanaErrorCode.jsonRpcParseError:
      case SolanaErrorCode.jsonRpcScanError:
      case SolanaErrorCode.jsonRpcServerErrorBlockCleanedUp:
      case SolanaErrorCode.jsonRpcServerErrorBlockNotAvailable:
      case SolanaErrorCode.jsonRpcServerErrorBlockStatusNotAvailableYet:
      case SolanaErrorCode.jsonRpcServerErrorKeyExcludedFromSecondaryIndex:
      case SolanaErrorCode.jsonRpcServerErrorLongTermStorageSlotSkipped:
      case SolanaErrorCode.jsonRpcServerErrorSlotSkipped:
      case SolanaErrorCode
          .jsonRpcServerErrorTransactionPrecompileVerificationFailure:
      case SolanaErrorCode.jsonRpcServerErrorUnsupportedTransactionVersion:
        errorContext = {'__serverMessage': message};
      case _:
        if (data is Map<String, Object?>) {
          errorContext = data;
        }
    }

    return SolanaError(code, errorContext);
  }

  // Malformed JSON-RPC error.
  final message =
      putativeErrorResponse is Map<String, Object?> &&
          putativeErrorResponse['message'] is String
      ? putativeErrorResponse['message']! as String
      : 'Malformed JSON-RPC error with no message attribute';
  return SolanaError(SolanaErrorCode.malformedJsonRpcError, {
    'error': putativeErrorResponse,
    'message': message,
  });
}