Implemented handling of server side issues using the rfc for problem detasils in the frontend
This commit is contained in:
30
Moonlight.Frontend/Helpers/ProblemDetailsHelper.cs
Normal file
30
Moonlight.Frontend/Helpers/ProblemDetailsHelper.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Net.Http.Json;
|
||||
using Microsoft.AspNetCore.Components.Forms;
|
||||
using Moonlight.Shared.Http.Responses;
|
||||
|
||||
namespace Moonlight.Frontend.Helpers;
|
||||
|
||||
public static class ProblemDetailsHelper
|
||||
{
|
||||
public static async Task HandleProblemDetailsAsync(HttpResponseMessage response, object model, ValidationMessageStore validationMessageStore)
|
||||
{
|
||||
var problemDetails = await response.Content.ReadFromJsonAsync<ProblemDetails>();
|
||||
|
||||
if (problemDetails == null)
|
||||
response.EnsureSuccessStatusCode(); // Trigger exception when unable to parse
|
||||
else
|
||||
{
|
||||
if(!string.IsNullOrEmpty(problemDetails.Detail))
|
||||
validationMessageStore.Add(new FieldIdentifier(model, string.Empty), problemDetails.Detail);
|
||||
|
||||
if (problemDetails.Errors != null)
|
||||
{
|
||||
foreach (var error in problemDetails.Errors)
|
||||
{
|
||||
foreach (var message in error.Value)
|
||||
validationMessageStore.Add(new FieldIdentifier(model, error.Key), message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user