Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: make custom exceptions immutable #111

Merged
merged 1 commit into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/src/core/definitions/extensions/json_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ extension ParseField on Map<String, dynamic> {
return forms;
}

throw ValidationException(
throw const ValidationException(
'Missing "forms" member in Intraction Affordance',
);
}
Expand Down
11 changes: 7 additions & 4 deletions lib/src/core/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<Object>? _validationErrors;

Expand Down Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion lib/src/core/exceptions/web_idl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
4 changes: 2 additions & 2 deletions lib/src/core/implementation/content_serdes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ class ContentSerdes {
}

if (dataSchemaValue == null) {
throw ValidationException("Expected a defined dataSchemaValue");
throw const ValidationException("Expected a defined dataSchemaValue");
}

final schema = JsonSchema.create(
Map.fromEntries(filteredDataSchemaJson),
schemaVersion: SchemaVersion.draft7,
);
if (!schema.validate(dataSchemaValue.value).isValid) {
throw ValidationException("JSON Schema validation failed.");
throw const ValidationException("JSON Schema validation failed.");
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/core/implementation/interaction_output.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class InteractionOutput implements scripting_api.InteractionOutput {
@override
Future<ByteBuffer> arrayBuffer() async {
if (dataUsed) {
throw NotReadableException("Data has already been read");
throw const NotReadableException("Data has already been read");
}

_dataUsed = true;
Expand All @@ -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.",
);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/core/implementation/wot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
}
Expand All @@ -113,7 +113,7 @@ class WoT implements scripting_api.WoT {
final rawThingDescriptions = await interactionOutput.value();

if (rawThingDescriptions is! List<Object?>) {
throw DiscoveryException(
throw const DiscoveryException(
"Expected an array of Thing Descriptions but received an "
"invalid output instead.",
);
Expand Down
10 changes: 5 additions & 5 deletions test/core/exceptions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
});
Expand Down
Loading