Skip to content

Commit 3d2ed60

Browse files
committed
switched exception from custom to build in one
1 parent 94aedf3 commit 3d2ed60

File tree

4 files changed

+13
-14
lines changed

4 files changed

+13
-14
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Xml.Linq;
2+
3+
namespace NorthwindCRUD.Constants
4+
{
5+
public class StringTemplates
6+
{
7+
public const string InvalidEntityMessage = "{0} with id {1} does not exist!";
8+
}
9+
}

NorthwindCRUD/Controllers/ProductsController.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ namespace NorthwindCRUD.Controllers
33
using AutoMapper;
44
using Microsoft.AspNetCore.Authorization;
55
using Microsoft.AspNetCore.Mvc;
6-
using NorthwindCRUD.Exceptions;
76
using NorthwindCRUD.Models.DbModels;
87
using NorthwindCRUD.Models.Dtos;
98
using NorthwindCRUD.Services;
@@ -156,7 +155,7 @@ public ActionResult<ProductDto> Create(ProductDto model)
156155

157156
return BadRequest(ModelState);
158157
}
159-
catch (InvalidEntityIdException exception)
158+
catch (InvalidOperationException exception)
160159
{
161160
return StatusCode(400, exception.Message);
162161
}

NorthwindCRUD/Exceptions/InvalidEntityIdException.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

NorthwindCRUD/Services/ProductService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace NorthwindCRUD.Services
22
{
33
using AutoMapper;
4-
using NorthwindCRUD.Exceptions;
4+
using NorthwindCRUD.Constants;
55
using NorthwindCRUD.Helpers;
66
using NorthwindCRUD.Models.DbModels;
77

@@ -48,12 +48,12 @@ public ProductDb Create(ProductDb model)
4848
{
4949
if(this.dataContext.Categories.FirstOrDefault(c => c.CategoryId == model.CategoryId) == null)
5050
{
51-
throw new InvalidEntityIdException(nameof(model.Category), model.CategoryId.ToString());
51+
throw new InvalidOperationException(string.Format(StringTemplates.InvalidEntityMessage, nameof(model.Category), model.CategoryId.ToString()));
5252
}
5353

5454
if (this.dataContext.Suppliers.FirstOrDefault(c => c.SupplierId == model.SupplierId) == null)
5555
{
56-
throw new InvalidEntityIdException(nameof(model.Supplier), model.SupplierId.ToString());
56+
throw new InvalidOperationException(string.Format(StringTemplates.InvalidEntityMessage, nameof(model.Supplier), model.SupplierId.ToString()));
5757
}
5858

5959
var id = IdGenerator.CreateDigitsId();

0 commit comments

Comments
 (0)