Skip to content

Commit 9122d0f

Browse files
feat(exceptions): introduce structured exception handling for policy SDK with detailed context
1 parent b7c6bb7 commit 9122d0f

File tree

7 files changed

+190
-116
lines changed

7 files changed

+190
-116
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/// Base exception class for all policy SDK related errors.
2+
///
3+
/// This abstract class provides a common interface for all exceptions
4+
/// thrown by the policy engine, ensuring consistent error handling
5+
/// and messaging across the SDK.
6+
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+
12+
/// The error message describing the exception.
13+
final String message;
14+
15+
@override
16+
String toString() => 'PolicySDKException: $message';
17+
}
18+
19+
/// Abstract exception for detailed policy SDK errors with contextual information.
20+
///
21+
/// This class extends [IPolicySDKException] to provide additional context for
22+
/// errors that occur within the policy engine, such as the specific key involved,
23+
/// the original error thrown, and a map of field-specific validation errors.
24+
/// Subclasses should use this to represent exceptions where more granular
25+
/// diagnostic information is valuable for debugging or reporting.
26+
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+
40+
@override
41+
final String message;
42+
43+
/// The specific key or field that caused the error, if applicable.
44+
final String? key;
45+
46+
/// The original error object that led to this exception, if available.
47+
final Object? originalError;
48+
49+
/// A map of field-specific validation errors encountered during processing, if any.
50+
final Map<String, String>? errors;
51+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import 'package:flutter_policy_engine/src/exceptions/i_policy_sdk_exceptions.dart';
2+
3+
/// Exception thrown when JSON parsing fails during policy evaluation.
4+
///
5+
/// This exception is raised when the policy engine encounters malformed
6+
/// or invalid JSON data that cannot be parsed into the expected format.
7+
/// It provides detailed context about the parsing failure including
8+
/// the specific key that failed, the original error, and any additional
9+
/// validation errors.
10+
class JsonParseException implements IDetailPolicySDKException {
11+
@override
12+
final String message;
13+
14+
/// The specific JSON key that caused the parsing failure, if applicable.
15+
@override
16+
final String? key;
17+
18+
/// The original error object from the JSON parsing library.
19+
@override
20+
final Object? originalError;
21+
22+
/// A map of field-specific validation errors encountered during parsing.
23+
@override
24+
final Map<String, String>? errors;
25+
26+
/// Creates a new JsonParseException.
27+
///
28+
/// [message] should describe the parsing failure.
29+
/// [key] optionally specifies which JSON key caused the failure.
30+
/// [originalError] optionally provides the original parsing error.
31+
/// [errors] optionally provides a map of field-specific validation errors.
32+
JsonParseException(
33+
this.message, {
34+
this.key,
35+
this.originalError,
36+
this.errors,
37+
});
38+
39+
@override
40+
String toString() {
41+
final buffer = StringBuffer('JsonParseException: $message');
42+
if (key != null) {
43+
buffer.write(' (key: $key)');
44+
}
45+
if (originalError != null) {
46+
buffer.write(' (original: $originalError)');
47+
}
48+
if (errors != null && errors!.isNotEmpty) {
49+
buffer.write(' (${errors!.length} total errors)');
50+
}
51+
return buffer.toString();
52+
}
53+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import 'package:flutter_policy_engine/src/exceptions/i_policy_sdk_exceptions.dart';
2+
3+
/// Exception thrown when JSON serialization fails during policy operations.
4+
///
5+
/// This exception is raised when the policy engine cannot serialize
6+
/// policy objects or data structures to JSON format. This typically
7+
/// occurs when policy objects contain non-serializable data types
8+
/// or circular references.
9+
class JsonSerializeException implements IDetailPolicySDKException {
10+
@override
11+
final String message;
12+
13+
/// The specific object key that caused the serialization failure, if applicable.
14+
@override
15+
final String? key;
16+
17+
/// The original error object from the JSON serialization library.
18+
@override
19+
final Object? originalError;
20+
21+
/// A map of field-specific serialization errors encountered.
22+
@override
23+
final Map<String, String>? errors;
24+
25+
/// Creates a new JsonSerializeException.
26+
///
27+
/// [message] should describe the serialization failure.
28+
/// [key] optionally specifies which object key caused the failure.
29+
/// [originalError] optionally provides the original serialization error.
30+
/// [errors] optionally provides a map of field-specific serialization errors.
31+
JsonSerializeException(
32+
this.message, {
33+
this.key,
34+
this.originalError,
35+
this.errors,
36+
});
37+
38+
@override
39+
String toString() {
40+
final buffer = StringBuffer('JsonSerializeException: $message');
41+
if (key != null) {
42+
buffer.write(' (key: $key)');
43+
}
44+
if (originalError != null) {
45+
buffer.write(' (original: $originalError)');
46+
}
47+
if (errors != null && errors!.isNotEmpty) {
48+
buffer.write(' (${errors!.length} total errors)');
49+
}
50+
return buffer.toString();
51+
}
52+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import 'package:flutter_policy_engine/src/exceptions/i_policy_sdk_exceptions.dart';
2+
3+
/// Exception thrown when a policy engine operation is attempted before initialization.
4+
///
5+
/// This exception indicates that a policy-related operation was invoked
6+
/// before the policy engine or evaluator was properly initialized. This
7+
/// typically occurs if you attempt to evaluate policies, check permissions,
8+
/// or perform other policy operations before calling the required
9+
/// initialization or setup methods.
10+
///
11+
/// Example:
12+
/// ```dart
13+
/// if (!_isInitialized) {
14+
/// throw PolicyNotInitializedException('Policy engine must be initialized before use.');
15+
/// }
16+
/// ```
17+
class PolicyNotInitializedException implements IPolicySDKException {
18+
/// A message describing the initialization error.
19+
@override
20+
final String message;
21+
22+
/// Creates a new [PolicyNotInitializedException] with the given [message].
23+
const PolicyNotInitializedException(this.message);
24+
25+
@override
26+
String toString() {
27+
final buffer = StringBuffer('PolicyNotInitializedException: $message');
28+
return buffer.toString();
29+
}
30+
}

lib/src/exceptions/policy_sdk_exceptions.dart

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

lib/src/utils/json_handler.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import 'package:flutter_policy_engine/src/exceptions/policy_sdk_exceptions.dart';
1+
import 'package:flutter_policy_engine/src/exceptions/json_parse_exception.dart';
2+
import 'package:flutter_policy_engine/src/exceptions/json_serialize_exception.dart';
23
import 'package:flutter_policy_engine/src/utils/log_handler.dart';
34

45
/// Utility for type-safe JSON conversions with generic support.

test/utils/json_handler_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import 'package:flutter_policy_engine/src/exceptions/policy_sdk_exceptions.dart';
1+
import 'package:flutter_policy_engine/src/exceptions/json_parse_exception.dart';
2+
import 'package:flutter_policy_engine/src/exceptions/json_serialize_exception.dart';
23
import 'package:flutter_test/flutter_test.dart';
34
import 'package:flutter_policy_engine/src/utils/json_handler.dart';
45

0 commit comments

Comments
 (0)