Fix error handling to not spam the console

This commit is contained in:
Frank A. Krueger 2018-04-15 18:05:19 -07:00
parent 7aca89139e
commit 716cea64e9
No known key found for this signature in database
GPG Key ID: 0471C67474FFE664
5 changed files with 13 additions and 16 deletions

View File

@ -100,12 +100,14 @@ namespace Ooui.AspNetCore
var token = CancellationToken.None; var token = CancellationToken.None;
System.Net.WebSockets.WebSocket webSocket = null; System.Net.WebSockets.WebSocket webSocket = null;
void Error (string m, Exception e) => activeSession?.Logger?.LogWarning (e, m);
// //
// Create a new session and let it handle everything from here // Create a new session and let it handle everything from here
// //
try { try {
webSocket = await context.WebSockets.AcceptWebSocketAsync ("ooui").ConfigureAwait (false); webSocket = await context.WebSockets.AcceptWebSocketAsync ("ooui").ConfigureAwait (false);
var session = new Ooui.WebSocketSession (webSocket, activeSession.Element, activeSession.DisposeElementAfterSession, w, h, token); var session = new Ooui.WebSocketSession (webSocket, activeSession.Element, activeSession.DisposeElementAfterSession, w, h, Error, token);
await session.RunAsync ().ConfigureAwait (false); await session.RunAsync ().ConfigureAwait (false);
} }
catch (System.Net.WebSockets.WebSocketException ex) when (ex.WebSocketErrorCode == System.Net.WebSockets.WebSocketError.ConnectionClosedPrematurely) { catch (System.Net.WebSockets.WebSocketException ex) when (ex.WebSocketErrorCode == System.Net.WebSockets.WebSocketError.ConnectionClosedPrematurely) {

View File

@ -14,9 +14,11 @@ namespace Ooui
protected readonly List<Message> queuedMessages = new List<Message> (); protected readonly List<Message> queuedMessages = new List<Message> ();
protected readonly bool disposeElementAfterSession; protected readonly bool disposeElementAfterSession;
readonly Action<string, Exception> errorLogger;
public Session (Element element, bool disposeElementAfterSession, double initialWidth, double initialHeight) public Session (Element element, bool disposeElementAfterSession, double initialWidth, double initialHeight, Action<string, Exception> errorLogger)
{ {
this.errorLogger = errorLogger;
this.element = element; this.element = element;
this.disposeElementAfterSession = disposeElementAfterSession; this.disposeElementAfterSession = disposeElementAfterSession;
this.initialWidth = initialWidth; this.initialWidth = initialWidth;
@ -85,14 +87,7 @@ namespace Ooui
protected void Error (string message, Exception ex) protected void Error (string message, Exception ex)
{ {
Console.ForegroundColor = ConsoleColor.Red; errorLogger?.Invoke (message, ex);
Console.WriteLine ("{0}: {1}", message, ex);
Console.ResetColor ();
}
protected void Info (string message)
{
Console.WriteLine (message);
} }
} }
} }

View File

@ -573,7 +573,7 @@ namespace Ooui
// Create a new session and let it handle everything from here // Create a new session and let it handle everything from here
// //
try { try {
var session = new WebSocketSession (webSocket, element, disposeElementWhenDone, w, h, serverToken); var session = new WebSocketSession (webSocket, element, disposeElementWhenDone, w, h, Error, serverToken);
await session.RunAsync ().ConfigureAwait (false); await session.RunAsync ().ConfigureAwait (false);
} }
catch (System.Net.WebSockets.WebSocketException ex) when (ex.WebSocketErrorCode == System.Net.WebSockets.WebSocketError.ConnectionClosedPrematurely) { catch (System.Net.WebSockets.WebSocketException ex) when (ex.WebSocketErrorCode == System.Net.WebSockets.WebSocketError.ConnectionClosedPrematurely) {
@ -616,7 +616,7 @@ namespace Ooui
var ops = initialSize.Split (' '); var ops = initialSize.Split (' ');
var initialWidth = double.Parse (ops[0]); var initialWidth = double.Parse (ops[0]);
var initialHeight = double.Parse (ops[1]); var initialHeight = double.Parse (ops[1]);
var g = new WebAssemblySession (sessionId, element, disposeElementWhenDone, initialWidth, initialHeight); var g = new WebAssemblySession (sessionId, element, disposeElementWhenDone, initialWidth, initialHeight, Error);
lock (globalElementSessions) { lock (globalElementSessions) {
globalElementSessions[sessionId] = g; globalElementSessions[sessionId] = g;
} }

View File

@ -9,8 +9,8 @@ namespace Ooui
readonly string id; readonly string id;
readonly Action<Message> handleElementMessageSent; readonly Action<Message> handleElementMessageSent;
public WebAssemblySession (string id, Element element, bool disposeElementAfterSession, double initialWidth, double initialHeight) public WebAssemblySession (string id, Element element, bool disposeElementAfterSession, double initialWidth, double initialHeight, Action<string, Exception> errorLogger)
: base (element, disposeElementAfterSession, initialWidth, initialHeight) : base (element, disposeElementAfterSession, initialWidth, initialHeight, errorLogger)
{ {
this.id = id; this.id = id;
handleElementMessageSent = QueueMessage; handleElementMessageSent = QueueMessage;

View File

@ -23,8 +23,8 @@ namespace Ooui
DateTime lastTransmitTime = DateTime.MinValue; DateTime lastTransmitTime = DateTime.MinValue;
readonly TimeSpan throttleInterval = TimeSpan.FromSeconds (1.0 / UI.MaxFps); readonly TimeSpan throttleInterval = TimeSpan.FromSeconds (1.0 / UI.MaxFps);
public WebSocketSession (WebSocket webSocket, Element element, bool disposeElementAfterSession, double initialWidth, double initialHeight, CancellationToken serverToken) public WebSocketSession (WebSocket webSocket, Element element, bool disposeElementAfterSession, double initialWidth, double initialHeight, Action<string, Exception> errorLogger, CancellationToken serverToken)
: base (element, disposeElementAfterSession, initialWidth, initialHeight) : base (element, disposeElementAfterSession, initialWidth, initialHeight, errorLogger)
{ {
this.webSocket = webSocket; this.webSocket = webSocket;