Improve error handling and logging for envelope receiver

Added a null check in `EnvelopeReceiverPage.razor` to log a warning when `_envelopeReceiver` is null. Updated `ReportViewer.razor` to wrap `EnvelopeReceiverService.GetAsync` in a `try-catch` block, logging `HttpRequestException` errors and allowing the UI to handle null values gracefully.

Enhanced `EnvelopeReceiverService.GetAsync` to throw detailed `HttpRequestException` on API failures, including status code and reason phrase. Added `using System.Net;` to support HTTP-related classes. Updated method documentation to reflect the new behavior.

These changes improve error diagnostics, logging, and maintainability across the codebase.
This commit is contained in:
2026-06-11 13:40:30 +02:00
parent 7001d7351f
commit 7d0c5a0ee5
3 changed files with 25 additions and 2 deletions

View File

@@ -576,6 +576,10 @@ const int MaxThumbnailWidth = 400;
_signatures = signatures.Convert(UnitOfLength.Point);
_envelopeReceiver = await EnvelopeReceiverService.GetAsync(EnvelopeKey);
if (_envelopeReceiver is null)
{
logger.LogWarning("Envelope receiver data is null for envelope {EnvelopeKey}", EnvelopeKey);
}
await JSRuntime.InvokeVoidAsync("console.log", "Loaded signatures:", _signatures);

View File

@@ -343,7 +343,13 @@ Shown="OnPopupShownAsync">
}
_annotations = await AnnotationService.GetAnnotationsAsync(EnvelopeKey);
_envelopeReceiver = await EnvelopeReceiverService.GetAsync(EnvelopeKey);
try {
_envelopeReceiver = await EnvelopeReceiverService.GetAsync(EnvelopeKey);
} catch (HttpRequestException ex) {
// Log error but continue - UI will handle null envelope receiver gracefully
Console.WriteLine($"Failed to load envelope receiver: {ex.Message}");
}
if (!AppOptions.Value.UsePredefinedReports && !string.IsNullOrWhiteSpace(EnvelopeKey)) {
try {

View File

@@ -1,3 +1,4 @@
using System.Net;
using System.Net.Http.Json;
using System.Text.Json;
using EnvelopeGenerator.ReceiverUI.Models;
@@ -14,13 +15,25 @@ public class EnvelopeReceiverService(HttpClient http, IOptions<ApiOptions> apiOp
{
private static readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web);
/// <summary>
/// Fetches the envelope receiver data for the given envelope key from the API.
/// Throws HttpRequestException on failure with appropriate status code.
/// </summary>
/// <exception cref="HttpRequestException">Thrown when the API request fails.</exception>
public async Task<EnvelopeReceiverDto?> GetAsync(string envelopeKey, CancellationToken cancel = default)
{
var url = $"{apiOptions.Value.BaseUrl}/api/EnvelopeReceiver/{Uri.EscapeDataString(envelopeKey)}";
var response = await http.GetAsync(url, cancel);
if (!response.IsSuccessStatusCode)
return null;
{
var statusCode = (int)response.StatusCode;
var reasonPhrase = response.ReasonPhrase ?? "Unknown error";
throw new HttpRequestException(
$"Failed to load envelope receiver data. Status: {statusCode} ({reasonPhrase})",
null,
response.StatusCode);
}
return await response.Content.ReadFromJsonAsync<EnvelopeReceiverDto>(_jsonOptions, cancel);
}