Skip to content

Commit 1f88b85

Browse files
Refactor error handling in ApplicationBuilderExtensions
1 parent e85c109 commit 1f88b85

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

src/Web/Grand.Web.Common/Infrastructure/ApplicationBuilderExtensions.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,17 @@ public static void UseGrandExceptionHandler(this WebApplication application)
5959
return;
6060
}
6161

62-
try
62+
if (DataSettingsManager.DatabaseIsInstalled())
6363
{
64-
//check whether database is installed
65-
if (DataSettingsManager.DatabaseIsInstalled())
66-
{
67-
var logger = context.RequestServices.GetRequiredService<ILoggerFactory>()
68-
.CreateLogger("UseExceptionHandler");
69-
//log error
70-
logger.LogError(exception, exception.Message);
71-
}
72-
}
73-
finally
74-
{
75-
//rethrow the exception to show the error page
76-
throw exception;
64+
var logger = context.RequestServices.GetRequiredService<ILoggerFactory>()
65+
.CreateLogger("UseExceptionHandler");
66+
// Log the error
67+
logger.LogError(exception, exception.Message);
7768
}
69+
70+
// Set the response status code and message without throwing
71+
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
72+
await context.Response.WriteAsync("An unexpected error occurred.");
7873
});
7974
});
8075
}

0 commit comments

Comments
 (0)