Skip to content

Commit

Permalink
Added support for handle database errors (#184)
Browse files Browse the repository at this point in the history
* Added `EntityFramework.Exceptions` package

* Changed the type of the `ResponseBase.Errors` property to `IEnumerable<string>`

* Error has been handled when the unique restriction is violated
  • Loading branch information
MrDave1999 committed Apr 18, 2023
1 parent e819778 commit 83277af
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/Constants/ResponseMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class ResponseMessages

public const string UnexpectedErrorMessage = "Hubo un error inesperado, por favor intente de nuevo.";
public const string UnexpectedErrorsMessage = "Se encontraron errores inesperados, por favor intente de nuevo.";
public const string UniqueConstraintViolatedMessage = "Se detectó una violación a una restricción única (UNIQUE INDEX). " +
"Por favor, no envíe una entrada duplicada.";

public const string DirectLineTokenFailedMessage = "Direct Line token API call failed.";
public const string InactiveUserAccountMessage = "Cuenta de usuario inactiva.";
Expand Down
1 change: 1 addition & 0 deletions src/DataAccess/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
// See https://github.com/DentallApp/back-end/issues/25.
warnings.Ignore(CoreEventId.PossibleIncorrectRequiredNavigationWithQueryFilterInteractionWarning));
optionsBuilder.AddDelegateDecompiler();
optionsBuilder.UseExceptionProcessor();
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
Expand Down
1 change: 1 addition & 0 deletions src/DentallApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<PackageReference Include="EFCore.CustomDbContext" Version="1.0.0" />
<PackageReference Include="EFCore.CustomQueryPreprocessor" Version="2.0.0" />
<PackageReference Include="EFCore.NamingConventions" Version="7.0.0" />
<PackageReference Include="EntityFrameworkCore.Exceptions.MySQL.Pomelo" Version="6.0.3" />
<PackageReference Include="File.TypeChecker" Version="3.0.0" />
<PackageReference Include="itext7.pdfhtml" Version="4.0.5" />
<PackageReference Include="libphonenumber-csharp" Version="8.13.4" />
Expand Down
4 changes: 2 additions & 2 deletions src/Extensions/MvcBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ where state.Value.Errors.Count > 0
ErrorMessages = state.Value.Errors.Select(modelError => modelError.ErrorMessage)
}).ToDictionary(x => x.Key, x => x.ErrorMessages);
var result = new Response
var result = new
{
Success = false,
Message = InvalidModelStateMessage,
Errors = errors
Errors = errors
};
return new BadRequestObjectResult(result);
};
Expand Down
2 changes: 2 additions & 0 deletions src/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@
global using SendGrid.Helpers.Mail;
global using SendGrid.Extensions.DependencyInjection;

global using EntityFramework.Exceptions.Common;
global using EntityFramework.Exceptions.MySQL.Pomelo;
global using DelegateDecompiler;
global using FileTypeChecker;
global using FileTypeChecker.Extensions;
Expand Down
6 changes: 6 additions & 0 deletions src/Middlewares/ExceptionHandlingMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public async Task InvokeAsync(HttpContext context)
Message = UnexpectedErrorMessage
};

if (exception is UniqueConstraintException)
{
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
response.Errors = new[] { UniqueConstraintViolatedMessage };
}

await context.Response.WriteAsJsonAsync(response);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Responses/ResponseBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class ResponseBase
{
public bool Success { get; set; }
public string Message { get; set; }
public IDictionary<string, IEnumerable<string>> Errors { get; set; }
public IEnumerable<string> Errors { get; set; }

public ResponseBase()
{
Expand Down

0 comments on commit 83277af

Please sign in to comment.