GQLException.fromException constructor

GQLException.fromException(
  1. dynamic exception, {
  2. List<GQLExceptionProvider>? exceptionProviders,
})

Implementation

factory GQLException.fromException(
  dynamic exception, {
  List<GQLExceptionProvider>? exceptionProviders,
}) {
  if (exception is Exception) {
    if (exception is OperationException) {
      if (exception.graphqlErrors.isNotEmpty) {
        final graphqlError = exception.graphqlErrors[0];
        final errorMessage = graphqlError.message;
        final errorCode = graphqlError.extensions?["code"] ?? 'NO_CODE';
        final extensions = graphqlError.extensions;

        // Check custom exception providers first (similar to auth providers)
        if (exceptionProviders != null && exceptionProviders.isNotEmpty) {
          for (final provider in exceptionProviders) {
            if (provider.errorCode == errorCode) {
              final customException = provider.createException(
                errorCode,
                errorMessage,
                extensions,
              );
              if (customException != null) {
                return customException;
              }
            }
          }
        }
        return AppError(
          AppErrorModel(message: errorMessage, code: errorCode),
        );
      }
      return AppError(AppErrorModel(message: 'Unknown GraphQL error'));
    } else if (exception is GQLException) {
      return exception;
    } else {
      return const UnExpectedError();
    }
  } else {
    if (exception is String && exception.contains('is not a subtype of')) {
      return const UnableToProcessError();
    } else {
      return const UnExpectedError();
    }
  }
}