Skip to content

Commit 5e9074b

Browse files
CODE RUB: Implement TryCatch
1 parent bc1f43c commit 5e9074b

File tree

2 files changed

+100
-64
lines changed

2 files changed

+100
-64
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
}

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

Lines changed: 5 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
// Free To Use To Build Reliable Library Management Solutions
44
//-----------------------------------------------------------
55

6-
using EFxceptions.Models.Exceptions;
76
using LibraryManagement.Api.Brokers.Loggings;
87
using LibraryManagement.Api.Brokers.Storages;
98
using LibraryManagement.Api.Models.Foundations.Books;
10-
using LibraryManagement.Api.Models.Foundations.Books.Exceptions;
11-
using Microsoft.Data.SqlClient;
129

1310
namespace LibraryManagement.Api.Services.Foundations.Books
1411
{
@@ -25,68 +22,12 @@ public BookService(
2522
this.loggingBroker = loggingBroker;
2623
}
2724

28-
public async ValueTask<Book> AddBookAsync(Book book)
25+
public ValueTask<Book> AddBookAsync(Book book) =>
26+
TryCatch(async () =>
2927
{
30-
try
31-
{
32-
ValidateBookOnAdd(book);
28+
ValidateBookOnAdd(book);
3329

34-
return await this.storageBroker.InsertBookAsync(book);
35-
}
36-
catch (NullBookException nullBookException)
37-
{
38-
var bookValidationException =
39-
new BookValidationException(nullBookException);
40-
41-
this.loggingBroker.LogError(bookValidationException);
42-
43-
throw bookValidationException;
44-
}
45-
catch (InvalidBookException invalidBookException)
46-
{
47-
var bookValidationException =
48-
new BookValidationException(invalidBookException);
49-
50-
this.loggingBroker.LogError(bookValidationException);
51-
52-
throw bookValidationException;
53-
}
54-
catch (SqlException sqlException)
55-
{
56-
var failedBookStorageException =
57-
new FailedBookStorageException(sqlException);
58-
59-
var bookDependencyException =
60-
new BookDependencyException(failedBookStorageException);
61-
62-
this.loggingBroker.LogCritical(bookDependencyException);
63-
64-
throw bookDependencyException;
65-
}
66-
catch (DuplicateKeyException duplicateKeyException)
67-
{
68-
var alreadyExistsBookException =
69-
new AlreadyExistsBookException(duplicateKeyException);
70-
71-
var bookDependencyValidationException =
72-
new BookDependencyValidationException(alreadyExistsBookException);
73-
74-
this.loggingBroker.LogError(bookDependencyValidationException);
75-
76-
throw bookDependencyValidationException;
77-
}
78-
catch (Exception exception)
79-
{
80-
var failedBookServiceException =
81-
new FailedBookServiceException(exception);
82-
83-
var bookServiceException =
84-
new BookServiceException(failedBookServiceException);
85-
86-
this.loggingBroker.LogError(bookServiceException);
87-
88-
throw bookServiceException;
89-
}
90-
}
30+
return await this.storageBroker.InsertBookAsync(book);
31+
});
9132
}
9233
}

0 commit comments

Comments
 (0)