-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add global exception handler to log error messages
- Loading branch information
1 parent
d1ec76f
commit af266e7
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
namespace CTF.Host.Extensions; | ||
|
||
public static class HostEcsBuilderExtensions | ||
{ | ||
public static IEcsBuilder EnableExceptionHandler(this IEcsBuilder builder) | ||
{ | ||
string[] events = | ||
[ | ||
"OnGameModeInit", | ||
"OnGameModeExit", | ||
"OnPlayerCommandText", | ||
"OnPlayerConnect", | ||
"OnPlayerDisconnect", | ||
"OnDialogResponse", | ||
"OnPlayerText", | ||
"OnPlayerUpdate", | ||
"OnPlayerDeath", | ||
"OnPlayerTakeDamage", | ||
"OnPlayerGiveDamage", | ||
"OnRconLoginAttempt", | ||
"OnRconCommand", | ||
"OnPlayerKeyStateChange", | ||
"OnPlayerPickUpPickup", | ||
"OnPlayerSpawn", | ||
"OnPlayerRequestClass", | ||
"OnPlayerRequestSpawn" | ||
]; | ||
|
||
foreach (string @event in events) | ||
{ | ||
builder.UseMiddleware<GlobalExceptionHandler>(@event); | ||
} | ||
|
||
return builder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace CTF.Host.Middlewares; | ||
|
||
public class GlobalExceptionHandler( | ||
ILogger<GlobalExceptionHandler> logger, | ||
EventDelegate next) | ||
{ | ||
public object Invoke(EventContext context) | ||
{ | ||
try | ||
{ | ||
return next(context); | ||
} | ||
catch (Exception exception) | ||
{ | ||
logger.LogError(exception, "Exception occurred: {Message}", exception.Message); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters