Skip to content

Commit 347ba87

Browse files
Modify Exception Handlers.
1 parent c526e5a commit 347ba87

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

docs.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,25 @@ The authentication mechanism for subscribers in the SwiftLink project involves t
170170

171171
- **Proceeding with Request**: Finally, if the subscriber is successfully authenticated, the behavior invokes the next delegate in the request processing pipeline, allowing the request to proceed to its intended handler.
172172

173-
This authentication mechanism ensures that only requests with a valid and active subscriber token can access certain operations within the SwiftLink application. Unauthorized requests are effectively blocked, enhancing the security of the application by ensuring that only registered and active subscribers can perform actions that require authentication.
173+
This authentication mechanism ensures that only requests with a valid and active subscriber token can access certain operations within the SwiftLink application. Unauthorized requests are effectively blocked, enhancing the security of the application by ensuring that only registered and active subscribers can perform actions that require authentication.
174+
175+
176+
## What is Back-Half Option
177+
178+
The Back-Half Option in the SwiftLink system refers to the custom path or identifier that follows the base URL of a shortened link. This custom back half allows users to personalize their shortened URLs, making them more recognizable, memorable, or relevant to the content they link to. For example, instead of a randomly generated short link like `http://swft.link/1X3YzZ`, a user could specify a back half to create a more descriptive link like `http://swft.link/mycampaign`.
179+
180+
### Implementation Overview
181+
182+
Based on the provided code snippets, the SwiftLink system includes functionality to inquire about the availability of a specific back half text through the `InquiryBackHalfQuery`. This feature ensures that custom back halves are unique and not already in use within the system.
183+
184+
- **InquiryBackHalfQuery**: This record defines a query with a single property, `BackHalfText`, representing the custom back half text that a user wants to use for their shortened link.
185+
186+
- **InquiryBackHalfValidator**: This class validates the `InquiryBackHalfQuery` to ensure that the `BackHalfText` is not null and does not exceed a maximum length of 30 characters. This validation helps maintain data integrity and prevents abuse of the system by limiting the length of custom back halves.
187+
188+
- **InquiryBackHalfHandler**: This class handles the `InquiryBackHalfQuery` by checking the database for the existence of the specified `BackHalfText` in the `Link` entities. If the back half text already exists, it returns a failure result, indicating that the back half is not available for use. Otherwise, it returns a success result, indicating that the back half is available.
189+
190+
### Usage in the System
191+
192+
The Back-Half Option feature is crucial for users who wish to create more meaningful and branded short links. By allowing users to specify a custom back half, SwiftLink enhances the usability and appeal of its short links, making them more effective for marketing, branding, and content sharing purposes.
193+
194+
The inquiry mechanism ensures that each custom back half is unique within the system, preventing conflicts and confusion that could arise from duplicate short links. This feature is part of SwiftLink's broader functionality to provide a user-friendly, efficient, and reliable link shortening service.

src/SwiftLink.Application/Common/Exceptions/SubscriberUnAuthorizedException.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
namespace SwiftLink.Application.Common.Exceptions;
2+
23
public class SubscriberUnAuthorizedException : Exception
34
{
45
public SubscriberUnAuthorizedException() : base("Token is not Valid! :(")

src/SwiftLink.Presentation/Middleware/GlobalExceptionHandling.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,18 @@ public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception e
2525
_logger.LogError(exception, "Server Error happened!");
2626

2727
var exceptionType = exception.GetType();
28-
if (_exceptionHandlers.TryGetValue(exceptionType,
29-
out var value))
28+
if (_exceptionHandlers.TryGetValue(exceptionType, out var handler))
3029
{
31-
await value.Invoke(httpContext, exception, cancellationToken);
30+
await handler.Invoke(httpContext, exception, cancellationToken);
3231
return true;
3332
}
3433

3534
httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
36-
await httpContext.Response
37-
.WriteAsJsonAsync(new ProblemDetails
38-
{
39-
Status = StatusCodes.Status500InternalServerError,
40-
Title = "Server error"
41-
}, cancellationToken);
35+
await httpContext.Response.WriteAsJsonAsync(new ProblemDetails
36+
{
37+
Status = StatusCodes.Status500InternalServerError,
38+
Title = "Server error"
39+
}, cancellationToken);
4240

4341
return false;
4442
}
@@ -61,8 +59,7 @@ private async Task HandleBusinessValidationException(HttpContext httpContext, Ex
6159
problemDetails.Extensions["errors"] = businessValidationException.Errors;
6260

6361
httpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
64-
await httpContext.Response.WriteAsJsonAsync(problemDetails,
65-
cancellationToken: cancellationToken);
62+
await httpContext.Response.WriteAsJsonAsync(problemDetails, cancellationToken);
6663
}
6764

6865
private async Task HandleSubscriberUnAuthorizedException(HttpContext httpContext, Exception exception,
@@ -76,6 +73,6 @@ await httpContext.Response.WriteAsJsonAsync(new ProblemDetails
7673
Status = StatusCodes.Status401Unauthorized,
7774
Title = "UnAuthorized User",
7875
Detail = "Token is not sent or User is unauthorized :(",
79-
}, cancellationToken: cancellationToken);
76+
}, cancellationToken);
8077
}
8178
}

0 commit comments

Comments
 (0)