Skip to content

Commit e298e72

Browse files
feat: integrate PolicySDKException for clarity in role management
1 parent c14a2f2 commit e298e72

File tree

3 files changed

+20
-25
lines changed

3 files changed

+20
-25
lines changed

lib/flutter_policy_engine.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export 'src/core/policy_manager.dart';
44
export 'src/widgets/policy_widget.dart';
55
export 'src/core/policy_provider.dart';
66
export 'src/models/role.dart';
7+
export 'src/exceptions/policy_sdk_exception.dart';

lib/src/core/policy_manager.dart

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:flutter_policy_engine/src/core/interfaces/i_policy_evaluator.dar
33
import 'package:flutter_policy_engine/src/core/interfaces/i_policy_storage.dart';
44
import 'package:flutter_policy_engine/src/core/memory_policy_storage.dart';
55
import 'package:flutter_policy_engine/src/core/role_evaluator.dart';
6+
import 'package:flutter_policy_engine/src/exceptions/policy_sdk_exception.dart';
67
import 'package:flutter_policy_engine/src/models/role.dart';
78
import 'package:flutter_policy_engine/src/utils/external_asset_handler.dart';
89
import 'package:flutter_policy_engine/src/utils/json_handler.dart';
@@ -66,9 +67,7 @@ class PolicyManager extends ChangeNotifier {
6667
/// are JSON representations of [Role] objects.
6768
///
6869
/// Throws:
69-
/// - [JsonParseException] if policy parsing fails completely
70-
/// - [FormatException] if the JSON data is malformed
71-
/// - [ArgumentError] if policy parsing fails
70+
/// - [PolicySDKException] if initialization fails for any reason, including malformed JSON, parsing errors, or storage issues.
7271
Future<void> initialize(Map<String, dynamic> jsonPolicies) async {
7372
try {
7473
LogHandler.info(
@@ -171,8 +170,10 @@ class PolicyManager extends ChangeNotifier {
171170
operation: 'policy_manager_initialize_error',
172171
);
173172

174-
// Re-throw to allow caller to handle the error
175-
rethrow;
173+
throw PolicySDKException(
174+
'Failed to initialize policy manager',
175+
exception: e is Exception ? e : null,
176+
);
176177
}
177178
}
178179

@@ -236,20 +237,8 @@ class PolicyManager extends ChangeNotifier {
236237
///
237238
/// ## Throws
238239
///
239-
/// * [ArgumentError] - If [assetPath] is empty or null
240-
///
241-
/// ## Dependencies
242-
///
243-
/// This method depends on:
244-
/// * [ExternalAssetHandler] - For loading and parsing the asset file
245-
/// * [initialize] - For processing the loaded policy data
246-
/// * [LogHandler] - For error logging and debugging
247-
///
240+
/// * [PolicySDKException] - If initialization fails for any reason, including malformed JSON, parsing errors, or storage issues.
248241
Future<void> initializeFromJsonAssets(String assetPath) async {
249-
if (assetPath.isEmpty) {
250-
throw ArgumentError('Asset path cannot be empty');
251-
}
252-
253242
try {
254243
final assetHandler = ExternalAssetHandler(assetPath: assetPath);
255244
final jsonPolicies = await assetHandler.loadAssets();
@@ -368,6 +357,11 @@ class PolicyManager extends ChangeNotifier {
368357
context: {'asset_path': assetPath},
369358
operation: 'policy_manager_initialize_from_json_assets_error',
370359
);
360+
361+
throw PolicySDKException(
362+
'Failed to initialize policy manager from JSON assets',
363+
exception: e is Exception ? e : null,
364+
);
371365
}
372366
}
373367

@@ -402,11 +396,11 @@ class PolicyManager extends ChangeNotifier {
402396
/// [role] must not be null and should have a valid name.
403397
///
404398
/// Throws:
405-
/// - [ArgumentError] if [role] is null or has an invalid name
399+
/// - [PolicySDKException] if [role] is null or has an invalid name
406400
/// - Storage-related exceptions if persistence fails
407401
Future<void> addRole(Role role) async {
408402
if (role.name.isEmpty) {
409-
throw ArgumentError('Role name cannot be empty');
403+
throw PolicySDKException('Role name cannot be empty');
410404
}
411405

412406
_roles[role.name] = role;
@@ -428,11 +422,11 @@ class PolicyManager extends ChangeNotifier {
428422
/// [roleName] must not be null or empty.
429423
///
430424
/// Throws:
431-
/// - [ArgumentError] if [roleName] is null or empty
425+
/// - [PolicySDKException] if [roleName] is null or empty
432426
/// - Storage-related exceptions if persistence fails
433427
Future<void> removeRole(String roleName) async {
434428
if (roleName.isEmpty) {
435-
throw ArgumentError('Role name cannot be empty');
429+
throw PolicySDKException('Role name cannot be empty');
436430
}
437431

438432
_roles.remove(roleName);
@@ -454,11 +448,11 @@ class PolicyManager extends ChangeNotifier {
454448
/// [role] must not be null and should have a valid name.
455449
///
456450
/// Throws:
457-
/// - [ArgumentError] if [roleName] is null/empty or [role] is null/invalid
451+
/// - [PolicySDKException] if [roleName] is null/empty or [role] is null/invalid
458452
/// - Storage-related exceptions if persistence fails
459453
Future<void> updateRole(String roleName, Role role) async {
460454
if (roleName.isEmpty && role.name.isEmpty) {
461-
throw ArgumentError('Role name cannot be empty');
455+
throw PolicySDKException('Role name cannot be empty');
462456
}
463457

464458
_roles[roleName] = role;

lib/src/exceptions/policy_sdk_exception.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class PolicySDKException implements IPolicySDKException {
3333
/// - [exception]: An optional underlying exception that caused this error
3434
PolicySDKException(
3535
this.message, {
36-
required this.exception,
36+
this.exception,
3737
});
3838

3939
/// A descriptive message explaining the error that occurred.

0 commit comments

Comments
 (0)