http: add OnException

This commit is contained in:
uwaa 2024-12-02 16:18:56 +00:00
parent 210c8de1ed
commit c675cd221e

View file

@ -50,6 +50,11 @@ public sealed class HttpServer
/// </summary>
public event Action<HttpRequest, HttpResponse>? OnResponse;
/// <summary>
/// Called when a request causes an exception.
/// </summary>
public event Action<IPEndPoint, Exception>? OnException;
/// <summary>
/// The maximum time the socket may be inactive before it is presumed dead and closed.
/// </summary>
@ -179,12 +184,12 @@ public sealed class HttpServer
await new HttpResponse(400, e.Message).WriteTo(httpStream).WaitAsync(Timeout);
}
catch { }
break;
throw;
}
catch (TimeoutException)
{
//Timeout
break;
throw;
}
catch (IOException)
{
@ -203,10 +208,11 @@ public sealed class HttpServer
}
}
}
catch (Exception)
catch (Exception e)
{
//Swallow exceptions to prevent the server from crashing.
//When debugging, use a debugger to break on exceptions.
OnException?.Invoke(endpoint, e);
}
finally
{