adapt method

  1. @override
Future<Result<T>> adapt(
  1. Future<HttpResponse<T>> call()
)
override

Adapts the given call function to return type T.

This method transforms the original call (which returns R) into a new format (returning T). Implementations typically wrap the result in error handling, logging, or other cross-cutting concerns.

  • call - A function that executes the original API call

Returns the adapted result of type T.

Implementation

@override
Future<Result<T>> adapt(Future<HttpResponse<T>> Function() call) async {
  try {
    final httpResponse = await call();
    final data = httpResponse.data;
    return Result<T>.ok(data);
  } on DioException catch (e, s) {
    final res = e.response;
    // logger.i(res?.headers);
    // final status = res?.statusCode;
    // logger.e("DioException", error: e, stackTrace: s);
    // logger.e("Status: $status, Body: $body");
    return Result.err(
      BetterError(
        code: res?.data?['code'] ?? "ERROR",
        message: res?.data?['message'] ?? e.message,
        stack: s.toString(),
      ),
    );
  } catch (e, s) {
    // logger.e("Unknown Error", error: e, stackTrace: s);
    return Result.err(
      BetterError(message: e.toString(), stack: s.toString()),
    );
  }
}