Skip to content

Commit 8b0d5be

Browse files
authored
Merge pull request #107 from eclipse-thingweb/exception-simplification
feat!: simplify custom exceptions
2 parents c357e26 + 9878db5 commit 8b0d5be

23 files changed

+213
-210
lines changed

lib/core.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
library core;
1111

1212
export "src/core/definitions.dart";
13+
export "src/core/exceptions.dart";
1314
export "src/core/implementation.dart";
1415
export "src/core/scripting_api.dart";

lib/src/core/definitions.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,4 @@ export "definitions/security/psk_security_scheme.dart";
4444
export "definitions/security/security_scheme.dart";
4545
export "definitions/thing_description.dart";
4646
export "definitions/thing_model.dart";
47-
export "definitions/validation/thing_description_schema.dart"
48-
show ThingDescriptionValidationException, thingDescriptionSchema;
49-
export "definitions/validation/validation_exception.dart";
47+
export "definitions/validation/thing_description_schema.dart";

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import "package:collection/collection.dart";
88
import "package:curie/curie.dart";
99

10+
import "../../exceptions.dart";
1011
import "../additional_expected_response.dart";
1112
import "../data_schema.dart";
1213
import "../expected_response.dart";
@@ -26,7 +27,6 @@ import "../security/oauth2_security_scheme.dart";
2627
import "../security/psk_security_scheme.dart";
2728
import "../security/security_scheme.dart";
2829
import "../thing_description.dart";
29-
import "../validation/validation_exception.dart";
3030
import "../version_info.dart";
3131

3232
const _validTdContextValues = [
@@ -638,7 +638,9 @@ List<ContextEntry> _parseContext(
638638
final value = entry.value;
639639

640640
if (value is! String) {
641-
throw ContextValidationException(value.runtimeType);
641+
throw ValidationException(
642+
"Excepted either a String or a Map<String, String> "
643+
"as @context entry, got ${value.runtimeType} instead.");
642644
}
643645

644646
if (!key.startsWith("@") && Uri.tryParse(value) != null) {
@@ -649,16 +651,6 @@ List<ContextEntry> _parseContext(
649651
}
650652
}
651653

652-
throw ContextValidationException(json.runtimeType);
653-
}
654-
655-
/// Custom [ValidationException] that is thrown for an invalid [ContextEntry].
656-
class ContextValidationException extends ValidationException {
657-
/// Creates a new [ContextValidationException] indicating the invalid
658-
/// [runtimeType].
659-
ContextValidationException(Type runtimeType)
660-
: super(
661-
"Excepted either a String or a Map<String, String> "
662-
"as @context entry, got $runtimeType instead.",
663-
);
654+
throw ValidationException("Excepted either a String or a Map<String, String> "
655+
"as @context entry, got ${json.runtimeType} instead.");
664656
}

lib/src/core/definitions/operation_type.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
//
55
// SPDX-License-Identifier: BSD-3-Clause
66

7+
import "../exceptions.dart";
78
import "interaction_affordances/interaction_affordance.dart";
8-
import "validation/validation_exception.dart";
99

1010
/// Enumeration for the possible WoT operation types.
1111
///

lib/src/core/definitions/thing_description.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import "package:curie/curie.dart";
88
import "package:meta/meta.dart";
99

10+
import "../exceptions.dart";
1011
import "additional_expected_response.dart";
1112
import "data_schema.dart";
1213
import "extensions/json_parser.dart";
@@ -61,8 +62,8 @@ class ThingDescription {
6162
if (validate) {
6263
final validationResult = thingDescriptionSchema.validate(json);
6364
if (!validationResult.isValid) {
64-
throw ThingDescriptionValidationException(
65-
json,
65+
throw ValidationException(
66+
"Validation of Thing Description failed.",
6667
validationResult.errors,
6768
);
6869
}

lib/src/core/definitions/validation/thing_description_schema.dart

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,9 @@
66

77
import "package:json_schema/json_schema.dart";
88

9-
import "validation_exception.dart";
10-
119
/// JSON Schema definition used for validating Thing Descriptions.
1210
final thingDescriptionSchema = JsonSchema.create(_rawThingDescriptionSchema);
1311

14-
/// [Exception] that is thrown when a Thing Description is identified as being
15-
/// invalid.
16-
class ThingDescriptionValidationException extends ValidationException {
17-
/// Constructor.
18-
ThingDescriptionValidationException(
19-
this.rawThingDescription,
20-
this._validationErrors,
21-
) : super("Validation of Thing Description failed");
22-
23-
final List<ValidationError> _validationErrors;
24-
25-
/// The raw Thing Description that is invalid.
26-
Map<String, dynamic> rawThingDescription;
27-
28-
@override
29-
String toString() {
30-
return "ThingDescriptionValidationException: $message.\n\n"
31-
'Errors:\n\n ${_validationErrors.join("\n")}';
32-
}
33-
}
34-
3512
final Map<String, dynamic> _rawThingDescriptionSchema = <String, dynamic>{
3613
"title": "Thing Description",
3714
"version": "1.1-09-November-2023",

lib/src/core/definitions/validation/validation_exception.dart

Lines changed: 0 additions & 19 deletions
This file was deleted.

lib/src/core/exceptions.dart

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2024 Contributors to the Eclipse Foundation. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
//
5+
// SPDX-License-Identifier: BSD-3-Clause
6+
7+
/// Base class for custom exceptions defined in `dart_wot`.
8+
base class DartWotException implements Exception {
9+
/// Constructor.
10+
DartWotException(this.message);
11+
12+
/// The error message of this [ValidationException].
13+
final String message;
14+
15+
/// The name of this [Exception] that will appear in the error message log.
16+
final exceptionType = "DartWotException";
17+
18+
@override
19+
String toString() => "$exceptionType: $message";
20+
}
21+
22+
/// An [Exception] that is thrown when the validation of a definition fails.
23+
base class ValidationException extends DartWotException {
24+
/// Constructor.
25+
ValidationException(super.message, [this._validationErrors]);
26+
27+
final List<Object>? _validationErrors;
28+
29+
@override
30+
String get exceptionType => "ValidationException";
31+
32+
@override
33+
String toString() {
34+
final String formattedValidationErrors;
35+
36+
final validationErrors = _validationErrors;
37+
if (validationErrors != null) {
38+
formattedValidationErrors = [
39+
"\n\nErrors:\n",
40+
...validationErrors,
41+
].join("\n");
42+
} else {
43+
formattedValidationErrors = "";
44+
}
45+
46+
return "$exceptionType: $message$formattedValidationErrors";
47+
}
48+
}
49+
50+
/// Custom [Exception] that is thrown when the discovery process fails.
51+
final class DiscoveryException extends DartWotException {
52+
/// Creates a new [DiscoveryException] with the specified error [message].
53+
DiscoveryException(super.message);
54+
55+
@override
56+
String get exceptionType => "DiscoveryException";
57+
}

lib/src/core/implementation.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export "implementation/augmented_form.dart";
1111
export "implementation/codecs/content_codec.dart";
1212
export "implementation/content.dart";
1313
export "implementation/content_serdes.dart";
14-
export "implementation/discovery_exception.dart";
1514
export "implementation/protocol_interfaces/protocol_client.dart";
1615
export "implementation/protocol_interfaces/protocol_client_factory.dart";
1716
export "implementation/protocol_interfaces/protocol_server.dart";

lib/src/core/implementation/augmented_form.dart

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import "package:meta/meta.dart";
1010
import "package:uri/uri.dart";
1111

1212
import "../definitions.dart";
13+
import "../exceptions.dart";
1314

1415
/// A [Form] augmented with information from its asscociated [_thingDescription]
1516
/// and [_interactionAffordance].
@@ -166,20 +167,3 @@ final class AugmentedForm implements Form {
166167
}
167168
}
168169
}
169-
170-
/// This [Exception] is thrown when [URI variables] are being used in the [Form]
171-
/// of a TD but no (valid) values were provided.
172-
///
173-
/// [URI variables]: https://www.w3.org/TR/wot-thing-description11/#form-uriVariables
174-
class UriVariableException implements Exception {
175-
/// Constructor.
176-
UriVariableException(this.message);
177-
178-
/// The error [message].
179-
final String message;
180-
181-
@override
182-
String toString() {
183-
return "UriVariableException: $message";
184-
}
185-
}

0 commit comments

Comments
 (0)