Wie protokolliere ich die Ausnahmen, einschließlich einer serverabhängigen eindeutigen Ursachenerklärung, wenn ich mit HC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Wie protokolliere ich die Ausnahmen, einschließlich einer serverabhängigen eindeutigen Ursachenerklärung, wenn ich mit H

Post by Anonymous »

Ich nehme an, das folgende Beispiel für die Verwendung von HttpClient mit Fehlerbehandlung ist ziemlich einfach:

Code: Select all

public class TaskHTTP_ClientClient
{

private readonly HttpClient httpClient = new HttpClient();
private readonly JsonSerializerOptions serializerOptions = new() { PropertyNamingPolicy = null };

public override async System.Threading.Tasks.Task RetrieveSelection(
TaskGateway.SelectionRetrieving.RequestParameters requestParameters
)
{

HttpResponseMessage response = await this.httpClient.GetAsync(/* */);

if (response.IsSuccessStatusCode)
{
return await response.Content.ReadFromJsonAsync();
}

throw new Exception("Failed to retrieve tasks selection.", response.StatusCode);

}
}
Auf diese Weise erfahren wir jedoch nicht die genaue Ursache der Ausnahme, da diese in den Antwortdaten enthalten ist.

Code: Select all

public class TaskHTTP_ClientClient
{

private readonly HttpClient httpClient = new HttpClient();
private readonly JsonSerializerOptions serializerOptions = new() { PropertyNamingPolicy = null };

public override async System.Threading.Tasks.Task RetrieveSelection(
TaskGateway.SelectionRetrieving.RequestParameters requestParameters
)
{

HttpResponseMessage response = await this.httpClient.GetAsync(/* */);

if (response.IsSuccessStatusCode)
{
return await response.Content.ReadFromJsonAsync();
}

string errorData = await response.Content.ReadAsStringAsync();

throw new Exception(errorData);

}
}
Besser, aber wenn diese Ausnahme abgefangen und System.Diagnostics.Debug.WriteLine() abgefangen wird, ist sie schwer zu lesen:
Image


System.Exception: {"type": "https://tools.ietf.org/html/rfc9110#section-15.5.1", "title": "Ein oder mehrere Validierungsfehler sind aufgetreten.", "status":400, "errors":{"optionalFiltering":["Das optionalFiltering-Feld ist erforderlich."]},"traceId":"00-7612a7eb6ea01309ea7dd827171dea35-1486e2e916e49364-00"
at Client.Data.FromServer.TaskHTTP_ClientGateway.RetrieveSelection(RequestParameters requestParameters)
at Client.SharedState.TasksSharedState.retrieveTasksSelection(Options Optionen)

Warum nicht die Fehlerdaten verschönern?

Code: Select all

public class TaskHTTP_ClientClient
{

private readonly HttpClient httpClient = new HttpClient();
private readonly JsonSerializerOptions serializerOptions = new() { PropertyNamingPolicy = null };

public override async System.Threading.Tasks.Task RetrieveSelection(
TaskGateway.SelectionRetrieving.RequestParameters requestParameters
)
{

HttpResponseMessage response = await this.httpClient.GetAsync(/* */);

if (response.IsSuccessStatusCode)
{
return await response.Content.ReadFromJsonAsync();
}

object errorData = await response.Content.ReadFromJsonAsync();

throw new Exception(
System.Text.Json.JsonSerializer.Serialize(
errorData,
options: new System.Text.Json.JsonSerializerOptions
{
WriteIndented = true
}
)
);

}
}
Image
Jetzt können wir die Fehlerursache sofort erkennen.
Mache ich etwas falsch?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post