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