Skip to content

Commit 59bac5a

Browse files
CODE RUB: Implement TryCatch
1 parent 669dd3e commit 59bac5a

File tree

2 files changed

+33
-88
lines changed

2 files changed

+33
-88
lines changed

LibraryManagement.Api/Services/Foundations/Books/BookService.Exceptions.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using LibraryManagement.Api.Models.Foundations.Books;
88
using LibraryManagement.Api.Models.Foundations.Books.Exceptions;
99
using Microsoft.Data.SqlClient;
10+
using Microsoft.EntityFrameworkCore;
1011
using Xeptions;
1112

1213
namespace LibraryManagement.Api.Services.Foundations.Books
@@ -34,6 +35,20 @@ private async ValueTask<Book> TryCatch(ReturningBookFunction returningBookFuncti
3435
{
3536
throw CreateAndLogValidationException(notFoundBookException);
3637
}
38+
catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
39+
{
40+
var lockedBookException =
41+
new LockedBookException(dbUpdateConcurrencyException);
42+
43+
throw CreateAndLogDependencyValidationException(lockedBookException);
44+
}
45+
catch (DbUpdateException dbUpdateException)
46+
{
47+
var failedBookStorageException =
48+
new FailedBookStorageException(dbUpdateException);
49+
50+
throw CreateAndLogDependencyException(failedBookStorageException);
51+
}
3752
catch (SqlException sqlException)
3853
{
3954
var failedBookStorageException =
@@ -118,5 +133,15 @@ private BookServiceException CreateAndLogServiceException(Xeption exception)
118133

119134
return bookServiceException;
120135
}
136+
137+
private BookDependencyException CreateAndLogDependencyException(Xeption exception)
138+
{
139+
var bookDependencyException =
140+
new BookDependencyException(exception);
141+
142+
this.loggingBroker.LogError(bookDependencyException);
143+
144+
return bookDependencyException;
145+
}
121146
}
122147
}

LibraryManagement.Api/Services/Foundations/Books/BookService.cs

Lines changed: 8 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
using LibraryManagement.Api.Brokers.Loggings;
77
using LibraryManagement.Api.Brokers.Storages;
88
using LibraryManagement.Api.Models.Foundations.Books;
9-
using LibraryManagement.Api.Models.Foundations.Books.Exceptions;
10-
using Microsoft.Data.SqlClient;
11-
using Microsoft.EntityFrameworkCore;
129

1310
namespace LibraryManagement.Api.Services.Foundations.Books
1411
{
@@ -49,94 +46,17 @@ public ValueTask<Book> RetrieveBookByIdAsync(Guid bookId) =>
4946
return maybeBook;
5047
});
5148

52-
public async ValueTask<Book> ModifyBookAsync(Book book)
49+
public ValueTask<Book> ModifyBookAsync(Book book) =>
50+
TryCatch(async () =>
5351
{
54-
try
55-
{
56-
ValidateBookOnModify(book);
57-
58-
Book maybeBook =
59-
await this.storageBroker.SelectBookByIdAsync(book.BookId);
60-
61-
ValidateAgainstStorageBookOnModify(book, maybeBook);
62-
63-
return await this.storageBroker.UpdateBookAsync(book);
64-
}
65-
catch (NullBookException nullBookException)
66-
{
67-
var bookValidationException =
68-
new BookValidationException(nullBookException);
69-
70-
this.loggingBroker.LogError(bookValidationException);
71-
72-
throw bookValidationException;
73-
}
74-
catch (InvalidBookException invalidBookException)
75-
{
76-
var bookValidationException =
77-
new BookValidationException(invalidBookException);
78-
79-
this.loggingBroker.LogError(bookValidationException);
80-
81-
throw bookValidationException;
82-
}
83-
catch (NotFoundBookException notFoundBookException)
84-
{
85-
var bookValidationException =
86-
new BookValidationException(notFoundBookException);
87-
88-
this.loggingBroker.LogError(bookValidationException);
89-
90-
throw bookValidationException;
91-
}
92-
catch (SqlException sqlException)
93-
{
94-
var failedBookStorageException =
95-
new FailedBookStorageException(sqlException);
96-
97-
var bookDependencyException =
98-
new BookDependencyException(failedBookStorageException);
52+
ValidateBookOnModify(book);
9953

100-
this.loggingBroker.LogCritical(bookDependencyException);
101-
102-
throw bookDependencyException;
103-
}
104-
catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
105-
{
106-
var lockedBookException =
107-
new LockedBookException(dbUpdateConcurrencyException);
108-
109-
var bookDependencyValidationException =
110-
new BookDependencyValidationException(lockedBookException);
111-
112-
this.loggingBroker.LogError(bookDependencyValidationException);
113-
114-
throw bookDependencyValidationException;
115-
}
116-
catch (DbUpdateException dbUpdateException)
117-
{
118-
var failedBookStorageException =
119-
new FailedBookStorageException(dbUpdateException);
120-
121-
var bookDependencyException =
122-
new BookDependencyException(failedBookStorageException);
123-
124-
this.loggingBroker.LogError(bookDependencyException);
125-
126-
throw bookDependencyException;
127-
}
128-
catch (Exception exception)
129-
{
130-
var failedBookServiceException =
131-
new FailedBookServiceException(exception);
132-
133-
var bookServiceException =
134-
new BookServiceException(failedBookServiceException);
54+
Book maybeBook =
55+
await this.storageBroker.SelectBookByIdAsync(book.BookId);
13556

136-
this.loggingBroker.LogError(bookServiceException);
57+
ValidateAgainstStorageBookOnModify(book, maybeBook);
13758

138-
throw bookServiceException;
139-
}
140-
}
59+
return await this.storageBroker.UpdateBookAsync(book);
60+
});
14161
}
14262
}

0 commit comments

Comments
 (0)