|
| 1 | +//----------------------------------------------------------- |
| 2 | +// Copyright (c) Coalition of Good-Hearted Engineers |
| 3 | +// Free To Use To Build Reliable Library Management Solutions |
| 4 | +//----------------------------------------------------------- |
| 5 | + |
| 6 | +using EFxceptions.Models.Exceptions; |
| 7 | +using LibraryManagement.Api.Models.Foundations.Books; |
| 8 | +using LibraryManagement.Api.Models.Foundations.Books.Exceptions; |
| 9 | +using Microsoft.Data.SqlClient; |
| 10 | +using Xeptions; |
| 11 | + |
| 12 | +namespace LibraryManagement.Api.Services.Foundations.Books |
| 13 | +{ |
| 14 | + public partial class BookService |
| 15 | + { |
| 16 | + private delegate ValueTask<Book> ReturningBookFunction(); |
| 17 | + |
| 18 | + private async ValueTask<Book> TryCatch(ReturningBookFunction returningBookFunction) |
| 19 | + { |
| 20 | + try |
| 21 | + { |
| 22 | + return await returningBookFunction(); |
| 23 | + } |
| 24 | + catch (NullBookException nullBookException) |
| 25 | + { |
| 26 | + throw CreateAndLogValidationException(nullBookException); |
| 27 | + } |
| 28 | + catch (InvalidBookException invalidBookException) |
| 29 | + { |
| 30 | + throw CreateAndLogValidationException(invalidBookException); |
| 31 | + } |
| 32 | + catch (SqlException sqlException) |
| 33 | + { |
| 34 | + var failedBookStorageException = |
| 35 | + new FailedBookStorageException(sqlException); |
| 36 | + |
| 37 | + throw CreateAndLogCriticalDependencyException(failedBookStorageException); |
| 38 | + } |
| 39 | + catch (DuplicateKeyException duplicateKeyException) |
| 40 | + { |
| 41 | + var alreadyExistsBookException = |
| 42 | + new AlreadyExistsBookException(duplicateKeyException); |
| 43 | + |
| 44 | + throw CreateAndLogDependencyValidationException(alreadyExistsBookException); |
| 45 | + } |
| 46 | + catch (Exception exception) |
| 47 | + { |
| 48 | + var failedBookServiceException = |
| 49 | + new FailedBookServiceException(exception); |
| 50 | + |
| 51 | + throw CreateAndLogServiceException(failedBookServiceException); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private BookValidationException CreateAndLogValidationException(Xeption exception) |
| 56 | + { |
| 57 | + var bookValidationException = |
| 58 | + new BookValidationException(exception); |
| 59 | + |
| 60 | + this.loggingBroker.LogError(bookValidationException); |
| 61 | + |
| 62 | + return bookValidationException; |
| 63 | + } |
| 64 | + |
| 65 | + private BookDependencyException CreateAndLogCriticalDependencyException(Xeption exception) |
| 66 | + { |
| 67 | + var bookDependencyException = |
| 68 | + new BookDependencyException(exception); |
| 69 | + |
| 70 | + this.loggingBroker.LogCritical(bookDependencyException); |
| 71 | + |
| 72 | + return bookDependencyException; |
| 73 | + } |
| 74 | + |
| 75 | + private BookDependencyValidationException CreateAndLogDependencyValidationException(Xeption exception) |
| 76 | + { |
| 77 | + var bookDependencyValidationException = |
| 78 | + new BookDependencyValidationException(exception); |
| 79 | + |
| 80 | + this.loggingBroker.LogError(bookDependencyValidationException); |
| 81 | + |
| 82 | + return bookDependencyValidationException; |
| 83 | + } |
| 84 | + |
| 85 | + private BookServiceException CreateAndLogServiceException(Xeption exception) |
| 86 | + { |
| 87 | + var bookServiceException = |
| 88 | + new BookServiceException(exception); |
| 89 | + |
| 90 | + this.loggingBroker.LogError(bookServiceException); |
| 91 | + |
| 92 | + return bookServiceException; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments