From ad77c2fe2b60b2c312ce875cf3a8ecc0208652d3 Mon Sep 17 00:00:00 2001 From: ThompsonNye Date: Thu, 16 Oct 2025 17:15:11 +0200 Subject: [PATCH] Fix logs showing non enumerated enumerable as error messages --- src/Vegasco.Server.Api/Cars/CreateCar.cs | 30 +++++++++---------- src/Vegasco.Server.Api/Cars/UpdateCar.cs | 11 ++++--- .../Consumptions/CreateConsumption.cs | 11 ++++--- .../Consumptions/UpdateConsumption.cs | 16 ++++++---- 4 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/Vegasco.Server.Api/Cars/CreateCar.cs b/src/Vegasco.Server.Api/Cars/CreateCar.cs index c4ea28b..981ac36 100644 --- a/src/Vegasco.Server.Api/Cars/CreateCar.cs +++ b/src/Vegasco.Server.Api/Cars/CreateCar.cs @@ -11,6 +11,7 @@ namespace Vegasco.Server.Api.Cars; public static class CreateCar { public record Request(string Name); + public record Response(Guid Id, string Name); public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder) @@ -43,17 +44,21 @@ public static class CreateCar CancellationToken cancellationToken) { ILogger logger = loggerFactory.CreateLogger(typeof(CreateCar)); - - List failedValidations = await validators.ValidateAllAsync(request, cancellationToken: cancellationToken); + + List failedValidations = + await validators.ValidateAllAsync(request, cancellationToken: cancellationToken); if (failedValidations.Count > 0) { + string[] errors = failedValidations + .Where(x => !x.IsValid) + .SelectMany(x => x.Errors) + .Select(x => x.ErrorMessage) + .ToArray(); + logger.LogDebug( "Validation failed for request {@Request} with errors {@Errors}", request, - failedValidations - .Where(x => !x.IsValid) - .SelectMany(x => x.Errors) - .Select(x => x.ErrorMessage)); + errors); return TypedResults.BadRequest(new HttpValidationProblemDetails(failedValidations.ToCombinedDictionary())); } @@ -74,18 +79,11 @@ public static class CreateCar { logger.LogDebug("User with ID '{UserId}' not found, creating new user", userId); - user = new User - { - Id = userId - }; + user = new User { Id = userId }; await dbContext.Users.AddAsync(user, cancellationToken); } - Car car = new() - { - Name = request.Name.Trim(), - UserId = userId - }; + Car car = new() { Name = request.Name.Trim(), UserId = userId }; await dbContext.Cars.AddAsync(car, cancellationToken); await dbContext.SaveChangesAsync(cancellationToken); @@ -95,4 +93,4 @@ public static class CreateCar Response response = new(car.Id.Value, car.Name); return TypedResults.Created($"/v1/cars/{car.Id}", response); } -} +} \ No newline at end of file diff --git a/src/Vegasco.Server.Api/Cars/UpdateCar.cs b/src/Vegasco.Server.Api/Cars/UpdateCar.cs index 7410622..6baf675 100644 --- a/src/Vegasco.Server.Api/Cars/UpdateCar.cs +++ b/src/Vegasco.Server.Api/Cars/UpdateCar.cs @@ -49,13 +49,16 @@ public static class UpdateCar List failedValidations = await validators.ValidateAllAsync(request, cancellationToken); if (failedValidations.Count > 0) { + string[] errors = failedValidations + .Where(x => !x.IsValid) + .SelectMany(x => x.Errors) + .Select(x => x.ErrorMessage) + .ToArray(); + logger.LogDebug( "Validation failed for request {@Request} with errors {@Errors}", request, - failedValidations - .Where(x => !x.IsValid) - .SelectMany(x => x.Errors) - .Select(x => x.ErrorMessage)); + errors); return TypedResults.BadRequest(new HttpValidationProblemDetails(failedValidations.ToCombinedDictionary())); } diff --git a/src/Vegasco.Server.Api/Consumptions/CreateConsumption.cs b/src/Vegasco.Server.Api/Consumptions/CreateConsumption.cs index bdf4a8c..767cdab 100644 --- a/src/Vegasco.Server.Api/Consumptions/CreateConsumption.cs +++ b/src/Vegasco.Server.Api/Consumptions/CreateConsumption.cs @@ -58,13 +58,16 @@ public static class CreateConsumption List failedValidations = await validators.ValidateAllAsync(request, cancellationToken); if (failedValidations.Count > 0) { + string[] errors = failedValidations + .Where(x => !x.IsValid) + .SelectMany(x => x.Errors) + .Select(x => x.ErrorMessage) + .ToArray(); + logger.LogDebug( "Validation failed for request {@Request} with errors {@Errors}", request, - failedValidations - .Where(x => !x.IsValid) - .SelectMany(x => x.Errors) - .Select(x => x.ErrorMessage)); + errors); return TypedResults.BadRequest(new HttpValidationProblemDetails(failedValidations.ToCombinedDictionary())); } diff --git a/src/Vegasco.Server.Api/Consumptions/UpdateConsumption.cs b/src/Vegasco.Server.Api/Consumptions/UpdateConsumption.cs index e81f245..80ce097 100644 --- a/src/Vegasco.Server.Api/Consumptions/UpdateConsumption.cs +++ b/src/Vegasco.Server.Api/Consumptions/UpdateConsumption.cs @@ -52,17 +52,20 @@ public static class UpdateConsumption CancellationToken cancellationToken) { ILogger logger = loggerFactory.CreateLogger(typeof(UpdateConsumption)); - + List failedValidations = await validators.ValidateAllAsync(request, cancellationToken); if (failedValidations.Count > 0) { + string[] errors = failedValidations + .Where(x => !x.IsValid) + .SelectMany(x => x.Errors) + .Select(x => x.ErrorMessage) + .ToArray(); + logger.LogDebug( "Validation failed for request {@Request} with errors {@Errors}", request, - failedValidations - .Where(x => !x.IsValid) - .SelectMany(x => x.Errors) - .Select(x => x.ErrorMessage)); + errors); return TypedResults.BadRequest(new HttpValidationProblemDetails(failedValidations.ToCombinedDictionary())); } @@ -81,6 +84,7 @@ public static class UpdateConsumption logger.LogTrace("Updated consumption: {@Consumption}", consumption); - return TypedResults.Ok(new Response(consumption.Id.Value, consumption.DateTime, consumption.Distance, consumption.Amount, consumption.CarId.Value)); + return TypedResults.Ok(new Response(consumption.Id.Value, consumption.DateTime, consumption.Distance, + consumption.Amount, consumption.CarId.Value)); } } \ No newline at end of file