Skip to content

Commit 5e87a7f

Browse files
CODE RUB: Implement TryCatch
1 parent ff64fd0 commit 5e87a7f

File tree

2 files changed

+97
-64
lines changed

2 files changed

+97
-64
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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.Readers;
8+
using LibraryManagement.Api.Models.Foundations.Readers.Exceptions;
9+
using Microsoft.Data.SqlClient;
10+
using Xeptions;
11+
12+
namespace LibraryManagement.Api.Services.Foundations.Readers
13+
{
14+
public partial class ReaderService
15+
{
16+
private delegate ValueTask<Reader> ReturningReaderFunction();
17+
18+
private async ValueTask<Reader> TryCatch(ReturningReaderFunction returningReaderFunction)
19+
{
20+
try
21+
{
22+
return await returningReaderFunction();
23+
}
24+
catch (NullReaderException nullReaderException)
25+
{
26+
throw CreateAndLogValidationException(nullReaderException);
27+
}
28+
catch (InvalidReaderException invalidReaderException)
29+
{
30+
throw CreateAndLogValidationException(invalidReaderException);
31+
}
32+
catch (SqlException sqlException)
33+
{
34+
var failedReaderStorageException =
35+
new FailedReaderStorageException(sqlException);
36+
37+
throw CreateAndLogCriticalDependencyException(failedReaderStorageException);
38+
}
39+
catch (DuplicateKeyException duplicateKeyException)
40+
{
41+
var alreadyExistsReaderException =
42+
new AlreadyExistsReaderException(duplicateKeyException);
43+
44+
throw CreateAndLogDependencyValidationException(alreadyExistsReaderException);
45+
}
46+
catch (Exception exception)
47+
{
48+
var failedReaderServiceException =
49+
new FailedReaderServiceException(exception);
50+
51+
throw CreateAndLogServiceException(failedReaderServiceException);
52+
}
53+
}
54+
55+
private ReaderValidationException CreateAndLogValidationException(Xeption exception)
56+
{
57+
var readerValidationException =
58+
new ReaderValidationException(exception);
59+
60+
this.loggingBroker.LogError(readerValidationException);
61+
62+
return readerValidationException;
63+
}
64+
65+
private ReaderDependencyException CreateAndLogCriticalDependencyException(Xeption exception)
66+
{
67+
var readerDependencyException = new ReaderDependencyException(exception);
68+
this.loggingBroker.LogCritical(readerDependencyException);
69+
70+
return readerDependencyException;
71+
}
72+
73+
private ReaderDependencyValidationException CreateAndLogDependencyValidationException(
74+
Xeption exception)
75+
{
76+
var readerDependencyValidationException =
77+
new ReaderDependencyValidationException(exception);
78+
79+
this.loggingBroker.LogError(readerDependencyValidationException);
80+
81+
return readerDependencyValidationException;
82+
}
83+
84+
private ReaderServiceException CreateAndLogServiceException(Xeption exception)
85+
{
86+
var readerServiceException = new ReaderServiceException(exception);
87+
this.loggingBroker.LogError(readerServiceException);
88+
89+
return readerServiceException;
90+
}
91+
}
92+
}

LibraryManagement.Api/Services/Foundations/Readers/ReaderService.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.Readers;
10-
using LibraryManagement.Api.Models.Foundations.Readers.Exceptions;
11-
using Microsoft.Data.SqlClient;
129

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

28-
public async ValueTask<Reader> AddReaderAsync(Reader reader)
25+
public ValueTask<Reader> AddReaderAsync(Reader reader) =>
26+
TryCatch(async () =>
2927
{
30-
try
31-
{
32-
ValidateReaderOnAdd(reader);
28+
ValidateReaderOnAdd(reader);
3329

34-
return await this.storageBroker.InsertReaderAsync(reader);
35-
}
36-
catch (NullReaderException nullReaderException)
37-
{
38-
var readerValidationException =
39-
new ReaderValidationException(nullReaderException);
40-
41-
this.loggingBroker.LogError(readerValidationException);
42-
43-
throw readerValidationException;
44-
}
45-
catch (InvalidReaderException invalidReaderException)
46-
{
47-
var readerValidationException =
48-
new ReaderValidationException(invalidReaderException);
49-
50-
this.loggingBroker.LogError(readerValidationException);
51-
52-
throw readerValidationException;
53-
}
54-
catch (SqlException sqlException)
55-
{
56-
var failedReaderStorageException =
57-
new FailedReaderStorageException(sqlException);
58-
59-
var readerDependencyException =
60-
new ReaderDependencyException(failedReaderStorageException);
61-
62-
this.loggingBroker.LogCritical(readerDependencyException);
63-
64-
throw readerDependencyException;
65-
}
66-
catch (DuplicateKeyException duplicateKeyException)
67-
{
68-
var alreadyExistsReaderException =
69-
new AlreadyExistsReaderException(duplicateKeyException);
70-
71-
var readerDependencyValidationException =
72-
new ReaderDependencyValidationException(alreadyExistsReaderException);
73-
74-
this.loggingBroker.LogError(readerDependencyValidationException);
75-
76-
throw readerDependencyValidationException;
77-
}
78-
catch (Exception exception)
79-
{
80-
var failedReaderServiceException =
81-
new FailedReaderServiceException(exception);
82-
83-
var readerServiceException =
84-
new ReaderServiceException(failedReaderServiceException);
85-
86-
this.loggingBroker.LogError(readerServiceException);
87-
88-
throw readerServiceException;
89-
}
90-
}
30+
return await this.storageBroker.InsertReaderAsync(reader);
31+
});
9132
}
9233
}

0 commit comments

Comments
 (0)