Skip to content

Commit 16043ea

Browse files
refactor(policy): streamline policy validation logic and enhance exception interface
1 parent e8b5dbb commit 16043ea

File tree

4 files changed

+45
-191
lines changed

4 files changed

+45
-191
lines changed

lib/src/core/policy_manager.dart

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -86,54 +86,43 @@ class PolicyManager extends ChangeNotifier {
8686
final key = entry.key;
8787
final value = entry.value;
8888

89-
try {
90-
if (value == null) {
91-
LogHandler.warning(
92-
'Skipping null policy value',
93-
context: {'role': key},
94-
operation: 'policy_validation_skip',
95-
);
96-
continue;
97-
}
98-
99-
if (value is! List) {
100-
LogHandler.warning(
101-
'Skipping invalid policy value type',
102-
context: {
103-
'role': key,
104-
'expected_type': 'List',
105-
'actual_type': value.runtimeType.toString(),
106-
},
107-
operation: 'policy_validation_skip',
108-
);
109-
continue;
110-
}
111-
112-
if (value.any((item) => item is! String)) {
113-
LogHandler.warning(
114-
'Skipping policy with non-string content items',
115-
context: {'role': key},
116-
operation: 'policy_validation_skip',
117-
);
118-
continue;
119-
}
120-
121-
// Create the policy and add to valid policies
122-
final policy = Policy(
123-
roleName: key,
124-
allowedContent: value.cast<String>(),
89+
if (value == null) {
90+
LogHandler.warning(
91+
'Skipping null policy value',
92+
context: {'role': key},
93+
operation: 'policy_validation_skip',
94+
);
95+
continue;
96+
}
97+
98+
if (value is! List) {
99+
LogHandler.warning(
100+
'Skipping invalid policy value type',
101+
context: {
102+
'role': key,
103+
'expected_type': 'List',
104+
'actual_type': value.runtimeType.toString(),
105+
},
106+
operation: 'policy_validation_skip',
125107
);
126-
validPolicies[key] = policy.toJson();
127-
} catch (e, stackTrace) {
128-
LogHandler.error(
129-
'Failed to process policy',
130-
error: e,
131-
stackTrace: stackTrace,
108+
continue;
109+
}
110+
111+
if (value.any((item) => item is! String)) {
112+
LogHandler.warning(
113+
'Skipping policy with non-string content items',
132114
context: {'role': key},
133-
operation: 'policy_processing_error',
115+
operation: 'policy_validation_skip',
134116
);
135-
// Continue with other policies
117+
continue;
136118
}
119+
120+
// Create the policy and add to valid policies
121+
final policy = Policy(
122+
roleName: key,
123+
allowedContent: value.cast<String>(),
124+
);
125+
validPolicies[key] = policy.toJson();
137126
}
138127

139128
_policies = JsonHandler.parseMap(
Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,33 @@
1-
/// Base exception class for all policy SDK related errors.
1+
/// Base exception interface for all policy SDK related errors.
22
///
3-
/// This abstract class provides a common interface for all exceptions
3+
/// This abstract interface provides a common contract for all exceptions
44
/// thrown by the policy engine, ensuring consistent error handling
55
/// and messaging across the SDK.
66
abstract class IPolicySDKException implements Exception {
7-
/// Creates a new PolicySDKException with the given error message.
8-
///
9-
/// [message] should provide a clear description of what went wrong.
10-
const IPolicySDKException(this.message);
11-
127
/// The error message describing the exception.
13-
final String message;
8+
String get message;
149

1510
@override
16-
String toString() => 'PolicySDKException: $message';
11+
String toString();
1712
}
1813

19-
/// Abstract exception for detailed policy SDK errors with contextual information.
14+
/// Abstract interface for detailed policy SDK errors with contextual information.
2015
///
21-
/// This class extends [IPolicySDKException] to provide additional context for
16+
/// This interface extends [IPolicySDKException] to provide additional context for
2217
/// errors that occur within the policy engine, such as the specific key involved,
2318
/// the original error thrown, and a map of field-specific validation errors.
24-
/// Subclasses should use this to represent exceptions where more granular
19+
/// Implementations should use this to represent exceptions where more granular
2520
/// diagnostic information is valuable for debugging or reporting.
2621
abstract class IDetailPolicySDKException implements IPolicySDKException {
27-
/// Creates a new [IDetailPolicySDKException] with an error [message] and optional details.
28-
///
29-
/// [message] provides a human-readable description of the error.
30-
/// [key] optionally identifies the specific key or field related to the error.
31-
/// [originalError] optionally contains the original error object that triggered this exception.
32-
/// [errors] optionally provides a map of field-specific validation errors.
33-
const IDetailPolicySDKException(
34-
this.message, {
35-
this.key,
36-
this.originalError,
37-
this.errors,
38-
});
39-
4022
@override
41-
final String message;
23+
String get message;
4224

4325
/// The specific key or field that caused the error, if applicable.
44-
final String? key;
26+
String? get key;
4527

4628
/// The original error object that led to this exception, if available.
47-
final Object? originalError;
29+
Object? get originalError;
4830

4931
/// A map of field-specific validation errors encountered during processing, if any.
50-
final Map<String, String>? errors;
32+
Map<String, String>? get errors;
5133
}

test/core/policy_manager_test.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ class MockPolicyEvaluator implements IPolicyEvaluator {
6767

6868
class ThrowingPolicy extends Policy {
6969
const ThrowingPolicy(
70-
{required String roleName, required List<String> allowedContent})
71-
: super(roleName: roleName, allowedContent: allowedContent);
70+
{required super.roleName, required super.allowedContent});
7271

7372
static Policy fromJson(Map<String, dynamic> json) {
7473
throw StateError('Forced error in fromJson');

test/exceptions/i_policy_sdk_exceptions_test.dart

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

0 commit comments

Comments
 (0)