From 9aa2f3b6409b3f90a04ad125b8adda222120547e Mon Sep 17 00:00:00 2001 From: John Miller Date: Fri, 30 Aug 2024 16:07:17 +0100 Subject: [PATCH] Fix null issues Refactor environment variables --- .../Repository/CustomerAddressRepository.cs | 4 +--- .../Helpers/EnvironmentVariables.cs | 20 +++++++++++++++---- .../EnvironmentVariableNotFoundException.cs | 18 +++++++++++++++++ .../Interceptors/ServerLoggerInterceptor.cs | 6 +++--- .../Middleware/ExceptionHandlingMiddleware.cs | 3 +-- 5 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 Microservice.CustomerAddress.Grpc/Helpers/Exceptions/EnvironmentVariableNotFoundException.cs diff --git a/Microservice.CustomerAddress.Grpc/Data/Repository/CustomerAddressRepository.cs b/Microservice.CustomerAddress.Grpc/Data/Repository/CustomerAddressRepository.cs index f263dab..f33242f 100644 --- a/Microservice.CustomerAddress.Grpc/Data/Repository/CustomerAddressRepository.cs +++ b/Microservice.CustomerAddress.Grpc/Data/Repository/CustomerAddressRepository.cs @@ -6,11 +6,9 @@ namespace Microservice.CustomerAddress.Grpc.Data.Repository; public class CustomerAddressRepository(IDbContextFactory dbContextFactory) : ICustomerAddressRepository { - public IDbContextFactory _dbContextFactory { get; set; } = dbContextFactory; - public async Task ByIdAsync(Guid customerId, Guid addressId) { - await using var db = await _dbContextFactory.CreateDbContextAsync(); + await using var db = await dbContextFactory.CreateDbContextAsync(); return await db.CustomerAddresses .Where(o => o.Id.Equals(addressId) && o.CustomerId.Equals(customerId)) .Include(e => e.Country) diff --git a/Microservice.CustomerAddress.Grpc/Helpers/EnvironmentVariables.cs b/Microservice.CustomerAddress.Grpc/Helpers/EnvironmentVariables.cs index 9eff45a..2ded1b8 100644 --- a/Microservice.CustomerAddress.Grpc/Helpers/EnvironmentVariables.cs +++ b/Microservice.CustomerAddress.Grpc/Helpers/EnvironmentVariables.cs @@ -1,8 +1,20 @@ -namespace Microservice.CustomerAddress.Grpc.Helpers; +using Microservice.CustomerAddress.Grpc.Helpers.Exceptions; + +namespace Microservice.CustomerAddress.Grpc.Helpers; public class EnvironmentVariablesHelper { - public static string JwtIssuer = Environment.GetEnvironmentVariable(Constants.JwtIssuer); - public static string JwtAudience = Environment.GetEnvironmentVariable(Constants.JwtAudience); - public static string JwtSymmetricSecurityKey = Environment.GetEnvironmentVariable(Constants.JwtSymmetricSecurityKey); + public static string JwtIssuer => GetEnvironmentVariable(Constants.JwtIssuer); + public static string JwtAudience => GetEnvironmentVariable(Constants.JwtAudience); + public static string JwtSymmetricSecurityKey => GetEnvironmentVariable(Constants.JwtSymmetricSecurityKey); + + public static string GetEnvironmentVariable(string name) + { + var variable = Environment.GetEnvironmentVariable(name); + + if (string.IsNullOrEmpty(variable)) + throw new EnvironmentVariableNotFoundException($"Environment Variable Not Found: {name}."); + + return variable; + } } \ No newline at end of file diff --git a/Microservice.CustomerAddress.Grpc/Helpers/Exceptions/EnvironmentVariableNotFoundException.cs b/Microservice.CustomerAddress.Grpc/Helpers/Exceptions/EnvironmentVariableNotFoundException.cs new file mode 100644 index 0000000..d068b4c --- /dev/null +++ b/Microservice.CustomerAddress.Grpc/Helpers/Exceptions/EnvironmentVariableNotFoundException.cs @@ -0,0 +1,18 @@ +namespace Microservice.CustomerAddress.Grpc.Helpers.Exceptions; + +public class EnvironmentVariableNotFoundException : Exception +{ + public EnvironmentVariableNotFoundException() + { + } + + public EnvironmentVariableNotFoundException(string message) + : base(message) + { + } + + public EnvironmentVariableNotFoundException(string message, Exception inner) + : base(message, inner) + { + } +} \ No newline at end of file diff --git a/Microservice.CustomerAddress.Grpc/Helpers/Interceptors/ServerLoggerInterceptor.cs b/Microservice.CustomerAddress.Grpc/Helpers/Interceptors/ServerLoggerInterceptor.cs index de166cc..5920ce4 100644 --- a/Microservice.CustomerAddress.Grpc/Helpers/Interceptors/ServerLoggerInterceptor.cs +++ b/Microservice.CustomerAddress.Grpc/Helpers/Interceptors/ServerLoggerInterceptor.cs @@ -20,12 +20,12 @@ public override async Task UnaryServerHandler( } catch (RpcNotFoundException) { - _logger.LogError($"Error thrown by {context.Method}."); + _logger.LogError("Error thrown by {context.Method}.", context.Method); throw; } catch (Exception ex) { - _logger.LogError(ex, $"Error thrown by {context.Method}."); + _logger.LogError(ex, "Error thrown by {context.Method}.", context.Method); throw new RpcException(new Status(StatusCode.Internal, ex.ToString())); } } @@ -35,7 +35,7 @@ private void LogMethodCall(TRequest request, ServerCallCont switch (context.Method) { case "/CustomerAddress.CustomerAddressGrpc/GetCustomerAddress": - _logger.LogInformation($"Call to Method: {context.Method}. Request: {request}"); + _logger.LogInformation("Call to Method: {context.Method}. Request: {request}", context.Method, request); break; } } diff --git a/Microservice.CustomerAddress.Grpc/Middleware/ExceptionHandlingMiddleware.cs b/Microservice.CustomerAddress.Grpc/Middleware/ExceptionHandlingMiddleware.cs index a6fd269..6bdc990 100644 --- a/Microservice.CustomerAddress.Grpc/Middleware/ExceptionHandlingMiddleware.cs +++ b/Microservice.CustomerAddress.Grpc/Middleware/ExceptionHandlingMiddleware.cs @@ -15,7 +15,7 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next) } catch (Exception e) { - _logger.LogError(e, e.Message); + _logger.LogError(e, "{e.Message}", e.Message); await HandleExceptionAsync(context, e); } } @@ -61,5 +61,4 @@ private static int GetStatusCode(Exception exception) => NotFoundException => StatusCodes.Status404NotFound, _ => StatusCodes.Status500InternalServerError }; - } \ No newline at end of file