Skip to content

Commit 987aacf

Browse files
committed
Move and refactor exception classes to AspNetCore namespace
Exception classes moved from EntityFrameworkCore to AspNetCore namespace for better organization. Added BadRequestException for HTTP 400 errors. Improved XML documentation and refactored code for clarity. Updated using directives and removed old exception namespace from BaseDbSetRepository.
1 parent 38813ff commit 987aacf

10 files changed

Lines changed: 75 additions & 49 deletions

File tree

Sources/EasyExtensions.EntityFrameworkCore/Exceptions/AccessException.cs renamed to Sources/EasyExtensions.AspNetCore/Exceptions/AccessException.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33

44
using System.Net;
55

6-
namespace EasyExtensions.EntityFrameworkCore.Exceptions
6+
namespace EasyExtensions.AspNetCore.Exceptions
77
{
88
/// <summary>
99
/// Access exception.
1010
/// </summary>
1111
/// <param name="objectName"> Object name. </param>
12-
[Serializable]
1312
public class AccessException(string objectName)
1413
: WebApiException(HttpStatusCode.Forbidden, objectName, "Access denied")
1514
{ }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2025–2026 Vadim Belov <https://belov.us>
3+
4+
using System.Net;
5+
6+
namespace EasyExtensions.AspNetCore.Exceptions
7+
{
8+
/// <summary>
9+
/// Represents an exception that is thrown when a request is unauthorized or invalid for a specified object.
10+
/// </summary>
11+
/// <param name="objectName">The name of the object associated with the unauthorized or invalid request.</param>
12+
public class BadRequestException(string objectName)
13+
: WebApiException(HttpStatusCode.BadRequest, objectName, "Unathorized")
14+
{ }
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2025–2026 Vadim Belov <https://belov.us>
3+
4+
using System.Net;
5+
6+
namespace EasyExtensions.AspNetCore.Exceptions
7+
{
8+
/// <summary>
9+
/// Represents an exception that is thrown when an attempt is made to create or add an object that already exists.
10+
/// </summary>
11+
/// <remarks>This exception corresponds to an HTTP 409 Conflict response and is typically used in web APIs
12+
/// to indicate that a resource with the specified name already exists. Use this exception to signal duplicate
13+
/// creation attempts in resource management scenarios.</remarks>
14+
/// <param name="objectName">The name of the object that caused the conflict.</param>
15+
public class DuplicateException(string objectName)
16+
: WebApiException(HttpStatusCode.Conflict, objectName, "Object already exists")
17+
{ }
18+
}

Sources/EasyExtensions.EntityFrameworkCore/Exceptions/EntityInvalidTypeException.cs renamed to Sources/EasyExtensions.AspNetCore/Exceptions/EntityInvalidTypeException.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
// SPDX-License-Identifier: MIT
22
// Copyright (c) 2025–2026 Vadim Belov <https://belov.us>
33

4-
namespace EasyExtensions.EntityFrameworkCore.Exceptions
4+
using System;
5+
6+
namespace EasyExtensions.AspNetCore.Exceptions
57
{
68
/// <summary>
7-
/// The exception that is thrown when an entity type is invalid.
9+
/// Represents an exception that is thrown when an entity is of an invalid type.
810
/// </summary>
11+
/// <param name="message">The message that describes the error.</param>
912
public class EntityInvalidTypeException(string message) : Exception(message)
1013
{
1114
/// <summary>

Sources/EasyExtensions.EntityFrameworkCore/Exceptions/EntityNotFoundException.cs renamed to Sources/EasyExtensions.AspNetCore/Exceptions/EntityNotFoundException.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
using System.Net;
55

6-
namespace EasyExtensions.EntityFrameworkCore.Exceptions
6+
namespace EasyExtensions.AspNetCore.Exceptions
77
{
88
/// <summary>
9-
/// Entity not found exception.
9+
/// Represents an exception that is thrown when a requested entity cannot be found.
1010
/// </summary>
11-
/// <param name="objectName"> Object name. </param>
12-
[Serializable]
11+
/// <param name="objectName">The name of the entity that was not found. This value is included in the exception details to identify the
12+
/// missing entity.</param>
1313
public class EntityNotFoundException(string objectName)
1414
: WebApiException(HttpStatusCode.NotFound, objectName, "Entity was not found")
1515
{ }
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2025–2026 Vadim Belov <https://belov.us>
3+
4+
using System.Net;
5+
6+
namespace EasyExtensions.AspNetCore.Exceptions
7+
{
8+
/// <summary>
9+
/// Represents an exception that is thrown when an operation is attempted without proper authorization.
10+
/// </summary>
11+
/// <remarks>This exception corresponds to an HTTP 401 Unauthorized response. It is typically used in web
12+
/// API scenarios to indicate that the caller must authenticate or does not have permission to access the specified
13+
/// resource.</remarks>
14+
/// <param name="objectName">The name of the object or resource for which access was denied.</param>
15+
public class UnauthorizedException(string objectName)
16+
: WebApiException(HttpStatusCode.Unauthorized, objectName, "Unathorized")
17+
{ }
18+
}

Sources/EasyExtensions.EntityFrameworkCore/Exceptions/WebApiException.cs renamed to Sources/EasyExtensions.AspNetCore/Exceptions/WebApiException.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@
33

44
using EasyExtensions.Abstractions;
55
using EasyExtensions.Models;
6+
using System;
7+
using System.Collections.Generic;
68
using System.Diagnostics;
79
using System.Net;
810

9-
namespace EasyExtensions.EntityFrameworkCore.Exceptions
11+
namespace EasyExtensions.AspNetCore.Exceptions
1012
{
1113
/// <summary>
12-
/// Base web api exception.
14+
/// Represents an exception that is thrown to indicate an error response from a Web API, including an associated
15+
/// HTTP status code and object name.
1316
/// </summary>
14-
/// <param name="statusCode"> HTTP status code. </param>
15-
/// <param name="objectName"> Object name. </param>
16-
/// <param name="message"> Exception message. </param>
17+
/// <remarks>Use this exception to return detailed error information from Web API actions, including a
18+
/// status code and object-specific error details. The exception can be used to generate standardized error
19+
/// responses for clients.</remarks>
20+
/// <param name="statusCode">The HTTP status code to associate with the exception. Indicates the nature of the error as defined by the HTTP
21+
/// protocol.</param>
22+
/// <param name="objectName">The name of the object or entity related to the error. Used to identify the source of the error in the response.</param>
23+
/// <param name="message">The error message that describes the reason for the exception.</param>
1724
public class WebApiException(HttpStatusCode statusCode, string objectName, string message) : Exception(message), IHttpError
1825
{
1926
/// <summary>

Sources/EasyExtensions.EntityFrameworkCore/Exceptions/DuplicateException.cs

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

Sources/EasyExtensions.EntityFrameworkCore/Exceptions/UnauthorizedException.cs

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

Sources/EasyExtensions.EntityFrameworkCore/Repository/BaseDbSetRepository.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Copyright (c) 2025–2026 Vadim Belov <https://belov.us>
33

44
using EasyExtensions.EntityFrameworkCore.Abstractions;
5-
using EasyExtensions.EntityFrameworkCore.Exceptions;
65
using Gridify;
76
using Gridify.EntityFramework;
87
using Microsoft.EntityFrameworkCore;
@@ -36,7 +35,7 @@ public BaseDbSetRepository(DbSet<TItem> db) : this(db, null) { }
3635
public virtual async Task<TItem> GetByIdAsync(int id, CancellationToken cancellationToken = default)
3736
{
3837
var found = await db.FirstOrDefaultAsync(x => x.Id == id, cancellationToken: cancellationToken);
39-
return found ?? throw new EntityNotFoundException(typeof(TItem).Name + $"[id:{id}]");
38+
return found ?? throw new InvalidOperationException(typeof(TItem).Name + $"[id:{id}]");
4039
}
4140

4241
/// <summary>
@@ -246,8 +245,7 @@ public virtual Task<int> CountAsync(Expression<Func<TItem, bool>> predicate, Can
246245
/// <exception cref="InvalidOperationException">Thrown when no entity is found that matches the predicate.</exception>
247246
public virtual async Task<TItem> FirstAsync(Expression<Func<TItem, bool>> predicate, CancellationToken cancellationToken = default)
248247
{
249-
return await db.FirstOrDefaultAsync(predicate, cancellationToken: cancellationToken)
250-
?? throw new EntityNotFoundException($"No entity found that matches the predicate: {predicate}");
248+
return await db.FirstAsync(predicate, cancellationToken: cancellationToken);
251249
}
252250

253251
/// <summary>

0 commit comments

Comments
 (0)