From 95bf374cc2a415051f63aef655cb8400fffcf903 Mon Sep 17 00:00:00 2001 From: Lars Kiesow Date: Tue, 13 Feb 2024 10:58:52 +0100 Subject: [PATCH] Introduce Error Handling This patch introduces basic error handling. This ensures that errors are being logged and that the display reflects the error instead of just showing an inactive state. --- assets/index.js | 40 ++++++++++++++++++++++++++-------------- main.go | 7 +++++++ opencast-ca-display.yml | 5 +++++ 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/assets/index.js b/assets/index.js index 2e71d0f..d664013 100644 --- a/assets/index.js +++ b/assets/index.js @@ -11,25 +11,37 @@ fetch('/config') setInterval(updateTimer, 2000); }) +/** + * Update the view + */ +function updateView(active) { + // Update text + document.getElementById('text').innerText = active.text; + + // Update colors + const body = document.getElementsByTagName('body')[0]; + body.style.backgroundColor = active.background; + body.style.color = active.color; + + // Update logo + document.getElementById('logo').src = active.image.replace(/\s/g, ''); +} + /** * Check for capture agent status */ function updateTimer() { fetch('/status') - .then(response => response.json()) - .then(capturing => { + .then(response => { + if (!response.ok) { + const active = config.unknown; + active.text = response.statusText; + updateView(active); + throw Error(response.statusText); + } + return response.json() + }).then(capturing => { console.debug('capturing', capturing) - const active = capturing ? config.capturing : config.idle; - - // Update text - document.getElementById('text').innerText = active.text; - - // Update colors - const body = document.getElementsByTagName('body')[0]; - body.style.backgroundColor = active.background; - body.style.color = active.color; - - // Update logo - document.getElementById('logo').src = active.image.replace(/\s/g, ''); + updateView(capturing ? config.capturing : config.idle); }) } diff --git a/main.go b/main.go index a74a8fa..208bb44 100644 --- a/main.go +++ b/main.go @@ -57,6 +57,7 @@ type Config struct { Display struct { Capturing DisplayConfig `json:"capturing"` Idle DisplayConfig `json:"idle"` + Unknown DisplayConfig `json:"unknown"` } Listen string @@ -120,9 +121,15 @@ func setupRouter() *gin.Engine { req.SetBasicAuth(config.Opencast.Username, config.Opencast.Password) resp, err := client.Do(req) if err != nil { + log.Println(err) c.JSON(http.StatusBadGateway, nil) return } + if resp.StatusCode != 200 { + log.Println(resp) + c.JSON(resp.StatusCode, nil) + return + } bodyText, err := ioutil.ReadAll(resp.Body) s := string(bodyText) var result AgentStateResult diff --git a/opencast-ca-display.yml b/opencast-ca-display.yml index db6ca93..0d727fb 100644 --- a/opencast-ca-display.yml +++ b/opencast-ca-display.yml @@ -110,6 +110,11 @@ display: bdf+Ja02rbN0Tm63v7bb1/63qhYAAAAAAAAAAAAAAAAAAAAAAAAAAIDY8w9T Gxe/25sMhQAAAABJRU5ErkJggg== + unknown: + text: Unknown + color: white + background: black + # IP address and port to bind to listen: 127.0.0.1:8080