improve api server error handling

This commit is contained in:
2024-11-14 06:01:42 +01:00
parent 1480485438
commit 81b8426b41

View File

@@ -85,7 +85,7 @@ public class ApiServer(string apiUrl) : IApiServer
protected void ListenerCallback(IAsyncResult result)
{
HttpListenerContext context;
HttpListenerContext? context;
// Skip if not lisstening anymore
if (!httpListener.IsListening)
@@ -96,15 +96,20 @@ public class ApiServer(string apiUrl) : IApiServer
{
context = httpListener.EndGetContext(result);
}
catch (HttpListenerException)
catch (Exception ex)
{
return;
Log.ErrorFormat("Error at getting context: ", ex.ToString());
context = null;
}
// Immitatly listen for new request
if (AllowMultipleRequests)
Receive();
// Cancel if we don't have a context
if (context is null)
return;
// Check context
OnCheckContext?.Invoke(this, new(context));
try
@@ -113,7 +118,7 @@ public class ApiServer(string apiUrl) : IApiServer
}
catch (Exception ex)
{
Log.Error(ex.ToString());
Log.ErrorFormat("Error at checking context: ", ex.ToString());
if (DebugMode)
throw;
}