Skip to content

Commit 0badb0b

Browse files
authored
Merge pull request #111 from eclipse-thingweb/immutable-exceptions
feat!: make custom exceptions immutable
2 parents 89fe292 + 9030dd6 commit 0badb0b

File tree

7 files changed

+20
-17
lines changed

7 files changed

+20
-17
lines changed

lib/src/core/definitions/extensions/json_parser.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ extension ParseField on Map<String, dynamic> {
290290
return forms;
291291
}
292292

293-
throw ValidationException(
293+
throw const ValidationException(
294294
'Missing "forms" member in Intraction Affordance',
295295
);
296296
}

lib/src/core/exceptions.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@
44
//
55
// SPDX-License-Identifier: BSD-3-Clause
66

7+
import "package:meta/meta.dart";
8+
79
export "exceptions/web_idl.dart";
810

911
/// Base class for custom exceptions defined in `dart_wot`.
12+
@immutable
1013
base class DartWotException implements Exception {
1114
/// Constructor.
12-
DartWotException(this.message);
15+
const DartWotException(this.message);
1316

1417
/// The error message of this [ValidationException].
1518
final String message;
1619

1720
/// The name of this [Exception] that will appear in the error message log.
18-
final exceptionType = "DartWotException";
21+
String get exceptionType => "DartWotException";
1922

2023
@override
2124
String toString() => "$exceptionType: $message";
@@ -24,7 +27,7 @@ base class DartWotException implements Exception {
2427
/// An [Exception] that is thrown when the validation of a definition fails.
2528
base class ValidationException extends DartWotException {
2629
/// Constructor.
27-
ValidationException(super.message, [this._validationErrors]);
30+
const ValidationException(super.message, [this._validationErrors]);
2831

2932
final List<Object>? _validationErrors;
3033

@@ -52,7 +55,7 @@ base class ValidationException extends DartWotException {
5255
/// Custom [Exception] that is thrown when the discovery process fails.
5356
final class DiscoveryException extends DartWotException {
5457
/// Creates a new [DiscoveryException] with the specified error [message].
55-
DiscoveryException(super.message);
58+
const DiscoveryException(super.message);
5659

5760
@override
5861
String get exceptionType => "DiscoveryException";

lib/src/core/exceptions/web_idl.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import "../exceptions.dart";
1313
/// [NotReadableError]: https://webidl.spec.whatwg.org/#notreadableerror
1414
final class NotReadableException extends DartWotException {
1515
/// Instantiates a new [NotReadableException] with the given [message].
16-
NotReadableException(super.message);
16+
const NotReadableException(super.message);
1717

1818
@override
1919
String get exceptionType => "NotReadableException";

lib/src/core/implementation/content_serdes.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,15 @@ class ContentSerdes {
145145
}
146146

147147
if (dataSchemaValue == null) {
148-
throw ValidationException("Expected a defined dataSchemaValue");
148+
throw const ValidationException("Expected a defined dataSchemaValue");
149149
}
150150

151151
final schema = JsonSchema.create(
152152
Map.fromEntries(filteredDataSchemaJson),
153153
schemaVersion: SchemaVersion.draft7,
154154
);
155155
if (!schema.validate(dataSchemaValue.value).isValid) {
156-
throw ValidationException("JSON Schema validation failed.");
156+
throw const ValidationException("JSON Schema validation failed.");
157157
}
158158
}
159159

lib/src/core/implementation/interaction_output.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class InteractionOutput implements scripting_api.InteractionOutput {
4747
@override
4848
Future<ByteBuffer> arrayBuffer() async {
4949
if (dataUsed) {
50-
throw NotReadableException("Data has already been read");
50+
throw const NotReadableException("Data has already been read");
5151
}
5252

5353
_dataUsed = true;
@@ -65,7 +65,7 @@ class InteractionOutput implements scripting_api.InteractionOutput {
6565
}
6666

6767
if (schema == null) {
68-
throw NotReadableException(
68+
throw const NotReadableException(
6969
"Can't convert data to a value because no DataSchema is present.",
7070
);
7171
}

lib/src/core/implementation/wot.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class WoT implements scripting_api.WoT {
9595
final thingDescription = await requestThingDescription(url);
9696

9797
if (!thingDescription.isValidDirectoryThingDescription) {
98-
throw DiscoveryException(
98+
throw const DiscoveryException(
9999
"Encountered an invalid Directory Thing Description",
100100
);
101101
}
@@ -113,7 +113,7 @@ class WoT implements scripting_api.WoT {
113113
final rawThingDescriptions = await interactionOutput.value();
114114

115115
if (rawThingDescriptions is! List<Object?>) {
116-
throw DiscoveryException(
116+
throw const DiscoveryException(
117117
"Expected an array of Thing Descriptions but received an "
118118
"invalid output instead.",
119119
);

test/core/exceptions_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,27 @@ void main() {
1111
group("DartWotException should", () {
1212
test("be indicate the respective name in its toString() method", () {
1313
expect(
14-
DartWotException("test").toString(),
14+
const DartWotException("test").toString(),
1515
"DartWotException: test",
1616
);
1717

1818
expect(
19-
ValidationException("test").toString(),
19+
const ValidationException("test").toString(),
2020
"ValidationException: test",
2121
);
2222

2323
expect(
24-
ValidationException("test", ["test", "test"]).toString(),
24+
const ValidationException("test", ["test", "test"]).toString(),
2525
"ValidationException: test\n\nErrors:\n\ntest\ntest",
2626
);
2727

2828
expect(
29-
DiscoveryException("test").toString(),
29+
const DiscoveryException("test").toString(),
3030
"DiscoveryException: test",
3131
);
3232

3333
expect(
34-
NotReadableException("test").toString(),
34+
const NotReadableException("test").toString(),
3535
"NotReadableException: test",
3636
);
3737
});

0 commit comments

Comments
 (0)