Skip to content

Commit 6ca5678

Browse files
committed
Exception Handling
Implement basic exception handling to trace errors + Return correct status + Return messages to display on UI
1 parent 1940a32 commit 6ca5678

48 files changed

Lines changed: 984 additions & 340 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Controllers/CustomerController.cs

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using Microsoft.AspNetCore.Mvc;
44
using NorthWindAPI.Controllers.Models.Requests;
55
using NorthWindAPI.Controllers.Models.Responses;
6+
using NorthWindAPI.Infrastructure.Exceptions.Base;
7+
using NorthWindAPI.Infrastructure.Exceptions.Repository;
68
using NorthWindAPI.Services.Interfaces;
79
using NorthWindAPI.Services.ResponseDto;
810

@@ -26,14 +28,16 @@ public class CustomerController(ICustomerService customerService, IMapper mapper
2628
[ProducesResponseType(typeof(UnauthorizedResult), StatusCodes.Status401Unauthorized)]
2729
public async Task<ActionResult<IEnumerable<CustomerResponse>>> All()
2830
{
29-
var customers = await _customerService.ListCustomers();
30-
if (customers == null || !customers.Any())
31+
try
3132
{
32-
return NotFound();
33+
var customers = await _customerService.ListCustomers();
34+
var response = _mapper.Map<IEnumerable<CustomerResponse>>(customers);
35+
return response.ToList();
36+
}
37+
catch(CustomerNotFoundException ex)
38+
{
39+
return NotFound(ex.Message);
3340
}
34-
35-
var response = _mapper.Map<IEnumerable<CustomerResponse>>(customers);
36-
return response.ToList();
3741
}
3842

3943
/// <summary>
@@ -47,14 +51,16 @@ public async Task<ActionResult<IEnumerable<CustomerResponse>>> All()
4751
[ProducesResponseType(typeof(UnauthorizedResult), StatusCodes.Status401Unauthorized)]
4852
public async Task<ActionResult<CustomerResponse>> Find(string id)
4953
{
50-
var customer = await _customerService.FindCustomer(id);
51-
if (customer == null)
54+
try
5255
{
53-
return NotFound();
56+
var customer = await _customerService.FindCustomer(id);
57+
var response = _mapper.Map<CustomerResponse>(customer);
58+
return response;
59+
}
60+
catch(CustomerNotFoundException ex)
61+
{
62+
return NotFound(ex.Message);
5463
}
55-
56-
var response = _mapper.Map<CustomerResponse>(customer);
57-
return response;
5864
}
5965

6066
/// <summary>
@@ -67,14 +73,16 @@ public async Task<ActionResult<CustomerResponse>> Find(string id)
6773
[ProducesResponseType(typeof(UnauthorizedResult), StatusCodes.Status401Unauthorized)]
6874
public async Task<ActionResult<RegionsResponse>> Regions()
6975
{
70-
var customerRegions = await _customerService.CustomerRegions();
71-
if (customerRegions == null || !customerRegions.Any())
76+
try
7277
{
73-
return NotFound();
78+
var customerRegions = await _customerService.CustomerRegions();
79+
RegionsResponse response = new() { Regions = customerRegions };
80+
return response;
81+
}
82+
catch(CustomerNotFoundException ex)
83+
{
84+
return NotFound(ex.Message);
7485
}
75-
76-
RegionsResponse response = new() { Regions = customerRegions };
77-
return response;
7886
}
7987

8088
/// <summary>
@@ -89,23 +97,21 @@ public async Task<ActionResult<RegionsResponse>> Regions()
8997
[HttpPost]
9098
public async Task<ActionResult<CustomerResponse>> Create(CustomerRequest newCustomer)
9199
{
92-
var customer = new CustomerDto();
93100
try
94101
{
95-
customer = await _customerService.ProcessNewCustomer(newCustomer);
102+
var customer = await _customerService.ProcessNewCustomer(newCustomer);
96103

97-
if (customer == null)
98-
{
99-
return BadRequest();
100-
}
104+
var response = _mapper.Map<CustomerResponse>(customer);
105+
return response;
101106
}
102-
catch
107+
catch(CustomerNotCreatedException ex)
103108
{
104-
return Forbid();
109+
return BadRequest(ex.Message);
110+
}
111+
catch(Exception ex)
112+
{
113+
return Forbid(ex.Message);
105114
}
106-
107-
var response = _mapper.Map<CustomerResponse>(customer);
108-
return response;
109115
}
110116

111117
/// <summary>
@@ -121,26 +127,30 @@ public async Task<ActionResult<CustomerResponse>> Create(CustomerRequest newCust
121127
[ProducesResponseType(typeof(UnauthorizedResult), StatusCodes.Status401Unauthorized)]
122128
public async Task<ActionResult<CustomerResponse>> Update(string id, CustomerRequest existingCustomer)
123129
{
124-
var customer = new CustomerDto();
125-
126130
try
127131
{
128132
var toUpdate = _mapper.Map<CustomerDto>(existingCustomer);
133+
var customer = await _customerService.Update(id, toUpdate);
129134

130-
if (id != toUpdate.Id)
131-
{
132-
return BadRequest();
133-
}
134-
135-
customer = await _customerService.Update(id, toUpdate);
135+
var response = _mapper.Map<CustomerResponse>(customer);
136+
return response;
136137
}
137-
catch
138+
catch(CustomerNotFoundException ex)
138139
{
139-
return Forbid();
140+
return NotFound(ex.Message);
141+
}
142+
catch(CustomerNotUpdatedException ex)
143+
{
144+
return BadRequest(ex.Message);
145+
}
146+
catch(EntityIdentifierMismatchException ex)
147+
{
148+
return BadRequest(ex.Message);
149+
}
150+
catch(Exception ex)
151+
{
152+
return Forbid(ex.Message);
140153
}
141-
142-
var response = _mapper.Map<CustomerResponse>(customer);
143-
return response;
144154
}
145155
}
146156
}

Controllers/DashboardController.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.AspNetCore.Authorization;
33
using Microsoft.AspNetCore.Mvc;
44
using NorthWindAPI.Controllers.Models.Responses;
5+
using NorthWindAPI.Infrastructure.Exceptions.Repository;
56
using NorthWindAPI.Views.Interfaces;
67

78
namespace NorthWindAPI.Controllers
@@ -28,17 +29,31 @@ ILogger<DashboardController> logger
2829
[HttpGet]
2930
public async Task<ActionResult<ChartsResponse>> Charts()
3031
{
31-
var response = new ChartsResponse()
32+
try
3233
{
33-
Totals = await _dashboardView.GetTotals(),
34-
Revenue = await _dashboardView.RevenueTotals(),
35-
Categories = await _dashboardView.CategoryRatios(),
36-
CategoryRevenue = await _dashboardView.CategoryRevenue(),
37-
PendingShipments = await _dashboardView.PendingShipments(),
38-
CategoryHeatmap = await _dashboardView.CategoryHeatmap()
39-
};
40-
41-
return response;
34+
var response = new ChartsResponse()
35+
{
36+
Totals = await _dashboardView.GetTotals(),
37+
Revenue = await _dashboardView.RevenueTotals(),
38+
Categories = await _dashboardView.CategoryRatios(),
39+
CategoryRevenue = await _dashboardView.CategoryRevenue(),
40+
PendingShipments = await _dashboardView.PendingShipments(),
41+
CategoryHeatmap = await _dashboardView.CategoryHeatmap()
42+
};
43+
return response;
44+
}
45+
catch (OrderNotFoundException ex)
46+
{
47+
return NotFound(ex.Message);
48+
}
49+
catch (ProductNotFoundException ex)
50+
{
51+
return NotFound(ex.Message);
52+
}
53+
catch (CustomerNotFoundException ex)
54+
{
55+
return NotFound(ex.Message);
56+
}
4257
}
4358
}
4459
}

0 commit comments

Comments
 (0)