safeCall<T> function

FutureTry<T> safeCall<T>(
  1. TryCall<T> action
)

Implementation

FutureTry<T> safeCall<T>(TryCall<T> action) async {
  try{
    return Try.success(await action.call());
  }on FormatException catch(e, stack){
    ///написать нужно те, которые вылетают при ошибке парсинга
    // Возможно ошибка при парсинге json
    return Try.error(
        ParsingError(
            stackTrace: stack
        )
    );
  }on DioException catch (e, stack){

    AppError getBadResponse(){
      if (e.response != null &&
          e.response?.statusCode != null &&
          e.response?.statusMessage != null) {
        return HttpError(
            url: e.requestOptions.path,
            statusCode: e.response?.statusCode ?? 0,
            statusMessage: e.response?.statusMessage,
            stackTrace: stack,
        );
      } else {
        return UnknownError(
            stackTrace: stack
        );
      }
    }

    return Try.error(
        switch (e.type){
          DioExceptionType.connectionTimeout => ConnectionError(
              cause: e,
              stackTrace: stack
          ),
          DioExceptionType.sendTimeout => ConnectionError(
              cause: e,
              stackTrace: stack
          ),
          DioExceptionType.receiveTimeout => ConnectionError(
              cause: e,
              stackTrace: stack
          ),
          DioExceptionType.badCertificate => ConnectionError(
              cause: e,
              stackTrace: stack
          ),
          DioExceptionType.badResponse => getBadResponse(),
          DioExceptionType.cancel => ConnectionError(
              cause: e,
              stackTrace: stack
          ),
          DioExceptionType.connectionError => ConnectionError(
              cause: e,
              stackTrace: stack
          ),
          DioExceptionType.unknown => UnknownError(
              cause: e,
              stackTrace: stack
          ),
        });
  } on SocketException catch(e, stack){
    return Try.error(SocketError (stackTrace: stack, cause: e));
  } on IOException catch(e, stack){
    return Try.error(
        ConnectionError(
            stackTrace: stack,
            cause: e
        )
    );
  } on TypeError catch(e, stack){
    return Try.error(
        DtoError(
            stackTrace: stack,
            error: e
        )
    );
  } catch (e, stack){
    return Try.error(
        UnknownError(
            error: e,
            stackTrace: stack,
        )
    );
  }
}