Refactor error handling in DocumentService and consumers
Refactored `DocumentService.GetDocumentAsync` to throw `HttpRequestException` on failure instead of returning a tuple. Updated all consumers to handle exceptions, improving error handling consistency across the application. Enhanced error messages for better user feedback and added logging for improved traceability. Updated `EnvelopeReceiverPage`, `EnvelopeReceiverPage_DxReportViewer`, and `ReportViewer` to use the new exception-based API and handle errors gracefully. This change simplifies the API, improves maintainability, and makes the application more robust and user-friendly.
This commit is contained in:
@@ -11,17 +11,25 @@ public class DocumentService(HttpClient http, IOptions<ApiOptions> apiOptions)
|
||||
|
||||
/// <summary>
|
||||
/// Fetches the PDF bytes for the given envelope key from the API.
|
||||
/// Returns null bytes with the HTTP status code on failure.
|
||||
/// Throws HttpRequestException on failure with appropriate status code.
|
||||
/// </summary>
|
||||
public async Task<(byte[]? Bytes, HttpStatusCode StatusCode)> GetDocumentAsync(string envelopeKey, CancellationToken cancel = default)
|
||||
/// <exception cref="HttpRequestException">Thrown when the API request fails.</exception>
|
||||
public async Task<byte[]?> GetDocumentAsync(string envelopeKey, CancellationToken cancel = default)
|
||||
{
|
||||
var response = await http.GetAsync($"{_api.BaseUrl}/api/Document/{Uri.EscapeDataString(envelopeKey)}", cancel);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
return (null, response.StatusCode);
|
||||
{
|
||||
var statusCode = (int)response.StatusCode;
|
||||
var reasonPhrase = response.ReasonPhrase ?? "Unknown error";
|
||||
throw new HttpRequestException(
|
||||
$"Failed to load document. Status: {statusCode} ({reasonPhrase})",
|
||||
null,
|
||||
response.StatusCode);
|
||||
}
|
||||
|
||||
var bytes = await response.Content.ReadAsByteArrayAsync(cancel);
|
||||
return (bytes, response.StatusCode);
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user