From 716cea64e95fb4754ecce3ee81a0eaa734361016 Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Sun, 15 Apr 2018 18:05:19 -0700 Subject: [PATCH] Fix error handling to not spam the console --- Ooui.AspNetCore/WebSocketHandler.cs | 4 +++- Ooui/Session.cs | 13 ++++--------- Ooui/UI.cs | 4 ++-- Ooui/WebAssemblySession.cs | 4 ++-- Ooui/WebSocketSession.cs | 4 ++-- 5 files changed, 13 insertions(+), 16 deletions(-) diff --git a/Ooui.AspNetCore/WebSocketHandler.cs b/Ooui.AspNetCore/WebSocketHandler.cs index 9f88522..ac71f02 100644 --- a/Ooui.AspNetCore/WebSocketHandler.cs +++ b/Ooui.AspNetCore/WebSocketHandler.cs @@ -100,12 +100,14 @@ namespace Ooui.AspNetCore var token = CancellationToken.None; 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 // try { 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); } catch (System.Net.WebSockets.WebSocketException ex) when (ex.WebSocketErrorCode == System.Net.WebSockets.WebSocketError.ConnectionClosedPrematurely) { diff --git a/Ooui/Session.cs b/Ooui/Session.cs index e873370..0f7864c 100644 --- a/Ooui/Session.cs +++ b/Ooui/Session.cs @@ -14,9 +14,11 @@ namespace Ooui protected readonly List queuedMessages = new List (); protected readonly bool disposeElementAfterSession; + readonly Action errorLogger; - public Session (Element element, bool disposeElementAfterSession, double initialWidth, double initialHeight) + public Session (Element element, bool disposeElementAfterSession, double initialWidth, double initialHeight, Action errorLogger) { + this.errorLogger = errorLogger; this.element = element; this.disposeElementAfterSession = disposeElementAfterSession; this.initialWidth = initialWidth; @@ -85,14 +87,7 @@ namespace Ooui protected void Error (string message, Exception ex) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine ("{0}: {1}", message, ex); - Console.ResetColor (); - } - - protected void Info (string message) - { - Console.WriteLine (message); + errorLogger?.Invoke (message, ex); } } } diff --git a/Ooui/UI.cs b/Ooui/UI.cs index 395c817..e232702 100644 --- a/Ooui/UI.cs +++ b/Ooui/UI.cs @@ -573,7 +573,7 @@ namespace Ooui // Create a new session and let it handle everything from here // 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); } 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 initialWidth = double.Parse (ops[0]); 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) { globalElementSessions[sessionId] = g; } diff --git a/Ooui/WebAssemblySession.cs b/Ooui/WebAssemblySession.cs index 8f72a39..825b254 100644 --- a/Ooui/WebAssemblySession.cs +++ b/Ooui/WebAssemblySession.cs @@ -9,8 +9,8 @@ namespace Ooui readonly string id; readonly Action handleElementMessageSent; - public WebAssemblySession (string id, Element element, bool disposeElementAfterSession, double initialWidth, double initialHeight) - : base (element, disposeElementAfterSession, initialWidth, initialHeight) + public WebAssemblySession (string id, Element element, bool disposeElementAfterSession, double initialWidth, double initialHeight, Action errorLogger) + : base (element, disposeElementAfterSession, initialWidth, initialHeight, errorLogger) { this.id = id; handleElementMessageSent = QueueMessage; diff --git a/Ooui/WebSocketSession.cs b/Ooui/WebSocketSession.cs index 4ce3ac6..fae402d 100644 --- a/Ooui/WebSocketSession.cs +++ b/Ooui/WebSocketSession.cs @@ -23,8 +23,8 @@ namespace Ooui DateTime lastTransmitTime = DateTime.MinValue; readonly TimeSpan throttleInterval = TimeSpan.FromSeconds (1.0 / UI.MaxFps); - public WebSocketSession (WebSocket webSocket, Element element, bool disposeElementAfterSession, double initialWidth, double initialHeight, CancellationToken serverToken) - : base (element, disposeElementAfterSession, initialWidth, initialHeight) + public WebSocketSession (WebSocket webSocket, Element element, bool disposeElementAfterSession, double initialWidth, double initialHeight, Action errorLogger, CancellationToken serverToken) + : base (element, disposeElementAfterSession, initialWidth, initialHeight, errorLogger) { this.webSocket = webSocket;