From 51a04659c580e646287b771dc1b3f5dcd94475e3 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sun, 19 Mar 2023 12:19:32 +0200 Subject: [PATCH] Exceptions - Added new `ErrorType` enum member `INVALID_FORMATTING` Formatting - All formatting functions will now throw a `BirchwoodException` instead of a `StringException` on invalid formatting parameters --- source/birchwood/client/exceptions.d | 8 +++++++- source/birchwood/protocol/formatting.d | 11 +++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/source/birchwood/client/exceptions.d b/source/birchwood/client/exceptions.d index ae5a1a6..b3f1cd8 100644 --- a/source/birchwood/client/exceptions.d +++ b/source/birchwood/client/exceptions.d @@ -56,7 +56,13 @@ public enum ErrorType * If the final encoded IRC message * is too long to send to the server */ - COMMAND_TOO_LONG + COMMAND_TOO_LONG, + + /** + * If invalid parameters are passed + * to any of the text formatting functions + */ + INVALID_FORMATTING } /** diff --git a/source/birchwood/protocol/formatting.d b/source/birchwood/protocol/formatting.d index d1a3f27..d41c913 100644 --- a/source/birchwood/protocol/formatting.d +++ b/source/birchwood/protocol/formatting.d @@ -3,8 +3,7 @@ */ module birchwood.protocol.formatting; -// FIXME: Remove below import and start using BirchwoodException -import std.string; +import birchwood.client.exceptions; // Reset character; resets all formatting enum reset_code = '\x0F'; @@ -52,7 +51,7 @@ char generate_color_control_char(string color) { } else if (color.length == 2) { return ascii_color_code; } else { - throw new StringException("Invalid color code (must be either two ASCII digits or a hexadecimal code of the form RRGGBB)"); + throw new BirchwoodException(ErrorType.INVALID_FORMATTING, "Invalid color code (must be either two ASCII digits or a hexadecimal code of the form RRGGBB)"); } } @@ -64,7 +63,7 @@ string set_foreground(string color) { } else if (color.length == 2) { control_char[0] = ascii_color_code; } else { - throw new StringException("Invalid color code (must be either two ASCII digits or a hexadecimal code of the form RRGGBB)"); + throw new BirchwoodException(ErrorType.INVALID_FORMATTING, "Invalid color code (must be either two ASCII digits or a hexadecimal code of the form RRGGBB)"); } return control_char.idup ~ color; } @@ -73,14 +72,14 @@ string set_foreground(string color) { string set_foreground_background(string fg, string bg) { char[1] control_char; if (fg.length != bg.length) { - throw new StringException("Invalid color code (cannot mix hex and ASCII)"); + throw new BirchwoodException(ErrorType.INVALID_FORMATTING, "Invalid color code (cannot mix hex and ASCII)"); } if (fg.length == 6) { control_char[0] = hex_color_code; } else if (fg.length == 2) { control_char[0] = ascii_color_code; } else { - throw new StringException("Invalid color code (must be either two ASCII digits or a hexadecimal code of the form RRGGBB)"); + throw new BirchwoodException(ErrorType.INVALID_FORMATTING, "Invalid color code (must be either two ASCII digits or a hexadecimal code of the form RRGGBB)"); } return control_char.idup ~ fg ~ "," ~ bg; }