From 9030dd691fd84fa9ccf51ec250bd1db897c51f93 Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Sat, 27 Jan 2024 23:47:33 +0100 Subject: [PATCH] feat!: make custom exceptions immutable --- lib/src/core/definitions/extensions/json_parser.dart | 2 +- lib/src/core/exceptions.dart | 11 +++++++---- lib/src/core/exceptions/web_idl.dart | 2 +- lib/src/core/implementation/content_serdes.dart | 4 ++-- lib/src/core/implementation/interaction_output.dart | 4 ++-- lib/src/core/implementation/wot.dart | 4 ++-- test/core/exceptions_test.dart | 10 +++++----- 7 files changed, 20 insertions(+), 17 deletions(-) diff --git a/lib/src/core/definitions/extensions/json_parser.dart b/lib/src/core/definitions/extensions/json_parser.dart index 518ff80f..3e4f1e29 100644 --- a/lib/src/core/definitions/extensions/json_parser.dart +++ b/lib/src/core/definitions/extensions/json_parser.dart @@ -290,7 +290,7 @@ extension ParseField on Map { return forms; } - throw ValidationException( + throw const ValidationException( 'Missing "forms" member in Intraction Affordance', ); } diff --git a/lib/src/core/exceptions.dart b/lib/src/core/exceptions.dart index 58fa74f9..60d5fb2b 100644 --- a/lib/src/core/exceptions.dart +++ b/lib/src/core/exceptions.dart @@ -4,18 +4,21 @@ // // SPDX-License-Identifier: BSD-3-Clause +import "package:meta/meta.dart"; + export "exceptions/web_idl.dart"; /// Base class for custom exceptions defined in `dart_wot`. +@immutable base class DartWotException implements Exception { /// Constructor. - DartWotException(this.message); + const DartWotException(this.message); /// The error message of this [ValidationException]. final String message; /// The name of this [Exception] that will appear in the error message log. - final exceptionType = "DartWotException"; + String get exceptionType => "DartWotException"; @override String toString() => "$exceptionType: $message"; @@ -24,7 +27,7 @@ base class DartWotException implements Exception { /// An [Exception] that is thrown when the validation of a definition fails. base class ValidationException extends DartWotException { /// Constructor. - ValidationException(super.message, [this._validationErrors]); + const ValidationException(super.message, [this._validationErrors]); final List? _validationErrors; @@ -52,7 +55,7 @@ base class ValidationException extends DartWotException { /// Custom [Exception] that is thrown when the discovery process fails. final class DiscoveryException extends DartWotException { /// Creates a new [DiscoveryException] with the specified error [message]. - DiscoveryException(super.message); + const DiscoveryException(super.message); @override String get exceptionType => "DiscoveryException"; diff --git a/lib/src/core/exceptions/web_idl.dart b/lib/src/core/exceptions/web_idl.dart index e41a8db8..6357df79 100644 --- a/lib/src/core/exceptions/web_idl.dart +++ b/lib/src/core/exceptions/web_idl.dart @@ -13,7 +13,7 @@ import "../exceptions.dart"; /// [NotReadableError]: https://webidl.spec.whatwg.org/#notreadableerror final class NotReadableException extends DartWotException { /// Instantiates a new [NotReadableException] with the given [message]. - NotReadableException(super.message); + const NotReadableException(super.message); @override String get exceptionType => "NotReadableException"; diff --git a/lib/src/core/implementation/content_serdes.dart b/lib/src/core/implementation/content_serdes.dart index 5b64ac36..178f2a6f 100644 --- a/lib/src/core/implementation/content_serdes.dart +++ b/lib/src/core/implementation/content_serdes.dart @@ -145,7 +145,7 @@ class ContentSerdes { } if (dataSchemaValue == null) { - throw ValidationException("Expected a defined dataSchemaValue"); + throw const ValidationException("Expected a defined dataSchemaValue"); } final schema = JsonSchema.create( @@ -153,7 +153,7 @@ class ContentSerdes { schemaVersion: SchemaVersion.draft7, ); if (!schema.validate(dataSchemaValue.value).isValid) { - throw ValidationException("JSON Schema validation failed."); + throw const ValidationException("JSON Schema validation failed."); } } diff --git a/lib/src/core/implementation/interaction_output.dart b/lib/src/core/implementation/interaction_output.dart index 1912b0ea..b0a65298 100644 --- a/lib/src/core/implementation/interaction_output.dart +++ b/lib/src/core/implementation/interaction_output.dart @@ -47,7 +47,7 @@ class InteractionOutput implements scripting_api.InteractionOutput { @override Future arrayBuffer() async { if (dataUsed) { - throw NotReadableException("Data has already been read"); + throw const NotReadableException("Data has already been read"); } _dataUsed = true; @@ -65,7 +65,7 @@ class InteractionOutput implements scripting_api.InteractionOutput { } if (schema == null) { - throw NotReadableException( + throw const NotReadableException( "Can't convert data to a value because no DataSchema is present.", ); } diff --git a/lib/src/core/implementation/wot.dart b/lib/src/core/implementation/wot.dart index 0001ba1b..70fd7183 100644 --- a/lib/src/core/implementation/wot.dart +++ b/lib/src/core/implementation/wot.dart @@ -95,7 +95,7 @@ class WoT implements scripting_api.WoT { final thingDescription = await requestThingDescription(url); if (!thingDescription.isValidDirectoryThingDescription) { - throw DiscoveryException( + throw const DiscoveryException( "Encountered an invalid Directory Thing Description", ); } @@ -113,7 +113,7 @@ class WoT implements scripting_api.WoT { final rawThingDescriptions = await interactionOutput.value(); if (rawThingDescriptions is! List) { - throw DiscoveryException( + throw const DiscoveryException( "Expected an array of Thing Descriptions but received an " "invalid output instead.", ); diff --git a/test/core/exceptions_test.dart b/test/core/exceptions_test.dart index 812ee427..81098ec0 100644 --- a/test/core/exceptions_test.dart +++ b/test/core/exceptions_test.dart @@ -11,27 +11,27 @@ void main() { group("DartWotException should", () { test("be indicate the respective name in its toString() method", () { expect( - DartWotException("test").toString(), + const DartWotException("test").toString(), "DartWotException: test", ); expect( - ValidationException("test").toString(), + const ValidationException("test").toString(), "ValidationException: test", ); expect( - ValidationException("test", ["test", "test"]).toString(), + const ValidationException("test", ["test", "test"]).toString(), "ValidationException: test\n\nErrors:\n\ntest\ntest", ); expect( - DiscoveryException("test").toString(), + const DiscoveryException("test").toString(), "DiscoveryException: test", ); expect( - NotReadableException("test").toString(), + const NotReadableException("test").toString(), "NotReadableException: test", ); });