From eeab4f0d458b66b02035ab10e570945b447f438c Mon Sep 17 00:00:00 2001
From: patrick96
Date: Sun, 27 Oct 2019 23:19:36 +0100
Subject: [PATCH] Print error message for invalid color strings
---
include/components/config.hpp | 6 ++++++
include/events/signal_emitter.hpp | 2 +-
src/components/config.cpp | 8 +++++++-
src/components/parser.cpp | 4 ++++
src/utils/color.cpp | 1 -
5 files changed, 18 insertions(+), 3 deletions(-)
diff --git a/include/components/config.hpp b/include/components/config.hpp
index 30375a49..77447b46 100644
--- a/include/components/config.hpp
+++ b/include/components/config.hpp
@@ -105,6 +105,9 @@ class config {
return dereference(move(section), move(key), move(string_value), move(result));
} catch (const key_error& err) {
return default_value;
+ } catch (const value_error& err) {
+ m_log.err("Invalid value for \"%s.%s\", using default value (reason: %s)", section, key, err.what());
+ return default_value;
}
}
@@ -165,6 +168,9 @@ class config {
}
} catch (const key_error& err) {
break;
+ } catch (const value_error& err) {
+ m_log.err("Invalid value in list \"%s.%s\", using list as-is (reason: %s)", section, key, err.what());
+ return default_value;
}
}
diff --git a/include/events/signal_emitter.hpp b/include/events/signal_emitter.hpp
index ecae99a8..10c03ce4 100644
--- a/include/events/signal_emitter.hpp
+++ b/include/events/signal_emitter.hpp
@@ -34,7 +34,7 @@ class signal_emitter {
}
}
} catch (const std::exception& e) {
- logger::make().err(e.what());
+ logger::make().err("Signal receiver raised an exception: %s", e.what());
}
return false;
diff --git a/src/components/config.cpp b/src/components/config.cpp
index f4b48562..a47050be 100644
--- a/src/components/config.cpp
+++ b/src/components/config.cpp
@@ -201,7 +201,13 @@ chrono::duration config::convert(string&& value) const {
template <>
rgba config::convert(string&& value) const {
- return rgba{value};
+ rgba ret{value};
+
+ if (!ret.has_color()) {
+ throw value_error("\"" + value + "\" is an invalid color value.");
+ }
+
+ return ret;
}
template <>
diff --git a/src/components/parser.cpp b/src/components/parser.cpp
index 51d75743..e3ef69ca 100644
--- a/src/components/parser.cpp
+++ b/src/components/parser.cpp
@@ -224,6 +224,10 @@ rgba parser::parse_color(const string& s, rgba fallback) {
rgba ret = rgba{s};
if (!ret.has_color() || ret.m_type == rgba::ALPHA_ONLY) {
+ logger::make().warn(
+ "Invalid color in formatting tag detected: \"%s\", using fallback \"%s\". This is an issue with one of your "
+ "formatting tags. If it is not, please report this as a bug.",
+ s, static_cast(fallback));
return fallback;
}
diff --git a/src/utils/color.cpp b/src/utils/color.cpp
index db0cb1c5..15d987fd 100644
--- a/src/utils/color.cpp
+++ b/src/utils/color.cpp
@@ -58,7 +58,6 @@ rgba::rgba(string hex) {
hex = normalize_hex(hex);
if (hex.length() == 0) {
- // TODO we need a way to inform the user that their color was malformed
m_value = 0;
m_type = NONE;
} else if (hex.length() == 2) {