-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathStudentService.Exceptions.cs
More file actions
144 lines (122 loc) · 5.39 KB
/
StudentService.Exceptions.cs
File metadata and controls
144 lines (122 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// -----------------------------------------------------------------------
// Copyright (c) Signature Chess Club & MumsWhoCode. All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.Linq;
using System.Threading.Tasks;
using EFxceptions.Models.Exceptions;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using SCMS.Services.Api.Models.Foundations.Students;
using SCMS.Services.Api.Models.Foundations.Students.Exceptions;
using Xeptions;
namespace SCMS.Services.Api.Services.Foundations.Students
{
public partial class StudentService
{
private delegate ValueTask<Student> ReturningStudentFunction();
private delegate IQueryable<Student> ReturningStudentsFunction();
private async ValueTask<Student> TryCatch(ReturningStudentFunction returningStudentFunction)
{
try
{
return await returningStudentFunction();
}
catch (NullStudentException nullStudentException)
{
throw CreateAndLogValidationException(nullStudentException);
}
catch (InvalidStudentException invalidStudentException)
{
throw CreateAndLogValidationException(invalidStudentException);
}
catch (SqlException sqlException)
{
var failedStudentStorageException =
new FailedStudentStorageException(sqlException);
throw CreateAndLogCriticalDependencyException(
failedStudentStorageException);
}
catch (DuplicateKeyException duplicateKeyException)
{
var alreadyExistsStudentException =
new AlreadyExistsStudentException(duplicateKeyException);
throw CreateAndLogDependencyValidationException(
alreadyExistsStudentException);
}
catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException)
{
var invalidStudentReferenceException =
new InvalidStudentReferenceException(
foreignKeyConstraintConflictException);
throw CreateAndLogDependencyValidationException(
invalidStudentReferenceException);
}
catch (DbUpdateException dbUpdateException)
{
var failedStudentStorageException =
new FailedStudentStorageException(
dbUpdateException);
throw CreateAndLogDependencyException(
failedStudentStorageException);
}
catch (Exception exception)
{
var failedStudentServiceException =
new FailedStudentServiceException(
exception);
throw CreateAndLogServiceException(
failedStudentServiceException);
}
}
private IQueryable<Student> TryCatch(ReturningStudentsFunction returningStudentsFunction)
{
try
{
return returningStudentsFunction();
}
catch (SqlException sqlException)
{
var failedStudentStorageException =
new FailedStudentStorageException(sqlException);
throw CreateAndLogCriticalDependencyException(failedStudentStorageException);
}
catch(Exception exception)
{
var failedStudentServiceException =
new FailedStudentServiceException(exception);
throw CreateAndLogServiceException(failedStudentServiceException);
}
}
private StudentValidationException CreateAndLogValidationException(Xeption exception)
{
var studentValidationException = new StudentValidationException(exception);
this.loggingBroker.LogError(studentValidationException);
return studentValidationException;
}
private StudentDependencyException CreateAndLogCriticalDependencyException(Xeption exception)
{
var studentDependencyException = new StudentDependencyException(exception);
this.loggingBroker.LogCritical(studentDependencyException);
return studentDependencyException;
}
private StudentDependencyValidationException CreateAndLogDependencyValidationException(Xeption exception)
{
var studentDependencyValidationException = new StudentDependencyValidationException(exception);
this.loggingBroker.LogError(studentDependencyValidationException);
return studentDependencyValidationException;
}
private StudentDependencyException CreateAndLogDependencyException(Xeption exception)
{
var studentDependencyException = new StudentDependencyException(exception);
this.loggingBroker.LogError(studentDependencyException);
return studentDependencyException;
}
private StudentServiceException CreateAndLogServiceException(Xeption exception)
{
var studentServiceException = new StudentServiceException(exception);
this.loggingBroker.LogError(studentServiceException);
return studentServiceException;
}
}
}