tryEncodingResponse<T> method

T? tryEncodingResponse<T>(
  1. BaseServiceResponse response, {
  2. ServiceReponseEncoding? encoding,
})

Attempts to convert a successful response to the requested encoding type.

Unlike toEncodingResponse, this method returns null if the conversion fails instead of throwing an exception.

If response represents an error response, the corresponding service exception is still thrown.

Implementation

T? tryEncodingResponse<T>(
  BaseServiceResponse response, {
  ServiceReponseEncoding? encoding,
}) {
  return switch (response) {
    BaseServiceErrorResponse() => throw _error(response),
    BaseServiceSuccessRespose() => () {
      try {
        return response.toEncodingResponse(encoding ?? responseEncoding);
      } catch (_) {
        return null;
      }
    }(),
  };
}