Skip to content

Commit ddb2b4f

Browse files
iammukeshmjarvis
andauthored
fix: Add logging to session creation exception in GenerateTokenCommandHandler (#1182)
Fixes #1179 The catch block was swallowing exceptions silently. While the behavior is intentional (session creation failure shouldn't block login), the exception should be logged for debugging purposes. Changes: - Added ILogger<GenerateTokenCommandHandler> dependency - Log warning when session creation fails with exception details Co-authored-by: jarvis <jarvis@codewithmukesh.com>
1 parent 66390d6 commit ddb2b4f

4 files changed

Lines changed: 112 additions & 2 deletions

File tree

.vscode/launch.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Launch API",
6+
"type": "coreclr",
7+
"request": "launch",
8+
"preLaunchTask": "build",
9+
"program": "${workspaceFolder}/src/Playground/Playground.Api/bin/Debug/net10.0/FSH.Playground.Api.dll",
10+
"args": [],
11+
"cwd": "${workspaceFolder}/src/Playground/Playground.Api",
12+
"stopAtEntry": false,
13+
"env": {
14+
"ASPNETCORE_ENVIRONMENT": "Development"
15+
}
16+
}
17+
]
18+
}

.vscode/settings.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"dotnet.dotnetPath": "/home/jarvis/.dotnet/dotnet",
3+
"omnisharp.dotnetPath": "/home/jarvis/.dotnet",
4+
"omnisharp.sdkPath": "/home/jarvis/.dotnet/sdk/10.0.102",
5+
"omnisharp.useModernNet": true,
6+
"editor.formatOnSave": true,
7+
"files.exclude": {
8+
"**/bin": true,
9+
"**/obj": true
10+
}
11+
}

.vscode/tasks.json

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/src/FSH.Framework.slnx",
11+
"/property:GenerateFullPaths=true",
12+
"/consoleloggerparameters:NoSummary"
13+
],
14+
"problemMatcher": "$msCompile",
15+
"group": {
16+
"kind": "build",
17+
"isDefault": true
18+
}
19+
},
20+
{
21+
"label": "test",
22+
"command": "dotnet",
23+
"type": "process",
24+
"args": [
25+
"test",
26+
"${workspaceFolder}/src/FSH.Framework.slnx"
27+
],
28+
"problemMatcher": "$msCompile",
29+
"group": "test"
30+
},
31+
{
32+
"label": "run (Aspire)",
33+
"command": "dotnet",
34+
"type": "process",
35+
"args": [
36+
"run",
37+
"--project",
38+
"${workspaceFolder}/src/Playground/FSH.Playground.AppHost"
39+
],
40+
"problemMatcher": "$msCompile",
41+
"group": "none"
42+
},
43+
{
44+
"label": "run (API only)",
45+
"command": "dotnet",
46+
"type": "process",
47+
"args": [
48+
"run",
49+
"--project",
50+
"${workspaceFolder}/src/Playground/Playground.Api"
51+
],
52+
"problemMatcher": "$msCompile",
53+
"group": "none"
54+
},
55+
{
56+
"label": "clean",
57+
"command": "dotnet",
58+
"type": "process",
59+
"args": [
60+
"clean",
61+
"${workspaceFolder}/src/FSH.Framework.slnx"
62+
],
63+
"problemMatcher": "$msCompile"
64+
},
65+
{
66+
"label": "restore",
67+
"command": "dotnet",
68+
"type": "process",
69+
"args": [
70+
"restore",
71+
"${workspaceFolder}/src/FSH.Framework.slnx"
72+
],
73+
"problemMatcher": "$msCompile"
74+
}
75+
]
76+
}

src/Modules/Identity/Modules.Identity/Features/v1/Tokens/TokenGeneration/GenerateTokenCommandHandler.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using FSH.Framework.Eventing.Outbox;
1010
using FSH.Framework.Shared.Multitenancy;
1111
using FSH.Modules.Identity.Contracts.Events;
12+
using Microsoft.Extensions.Logging;
1213

1314
namespace FSH.Modules.Identity.Features.v1.Tokens.TokenGeneration;
1415

@@ -22,6 +23,7 @@ public sealed class GenerateTokenCommandHandler
2223
private readonly IOutboxStore _outboxStore;
2324
private readonly IMultiTenantContextAccessor<AppTenantInfo> _multiTenantContextAccessor;
2425
private readonly ISessionService _sessionService;
26+
private readonly ILogger<GenerateTokenCommandHandler> _logger;
2527

2628
public GenerateTokenCommandHandler(
2729
IIdentityService identityService,
@@ -30,7 +32,8 @@ public GenerateTokenCommandHandler(
3032
IRequestContext requestContext,
3133
IOutboxStore outboxStore,
3234
IMultiTenantContextAccessor<AppTenantInfo> multiTenantContextAccessor,
33-
ISessionService sessionService)
35+
ISessionService sessionService,
36+
ILogger<GenerateTokenCommandHandler> logger)
3437
{
3538
_identityService = identityService;
3639
_tokenService = tokenService;
@@ -39,6 +42,7 @@ public GenerateTokenCommandHandler(
3942
_outboxStore = outboxStore;
4043
_multiTenantContextAccessor = multiTenantContextAccessor;
4144
_sessionService = sessionService;
45+
_logger = logger;
4246
}
4347

4448
public async ValueTask<TokenResponse> Handle(
@@ -99,10 +103,11 @@ await _sessionService.CreateSessionAsync(
99103
token.RefreshTokenExpiresAt,
100104
cancellationToken);
101105
}
102-
catch (Exception)
106+
catch (Exception ex)
103107
{
104108
// Session creation is non-critical - don't fail the login
105109
// This can happen if migrations haven't been applied yet
110+
_logger.LogWarning(ex, "Failed to create user session for user {UserId}. Login will continue without session tracking.", subject);
106111
}
107112

108113
// 3) Audit token issuance with a fingerprint (never raw token)

0 commit comments

Comments
 (0)