Skip to content

Commit 7ecda9c

Browse files
committed
docs: improve usage and error handling in README
- Updated example with try-catch blocks - Clarified StorageKey enum usage - Described custom StorageException classes
1 parent 00c21f6 commit 7ecda9c

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

README.md

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ A Dart package defining an abstract interface (`HtKVStorageService`) for key-val
66

77
* Defines a clear contract for basic key-value operations (read, write, delete) for common data types (`String`, `bool`, `int`, `double`).
88
* Includes a `clearAll` method for removing all entries.
9-
* Provides a `StorageKeys` class example to avoid magic strings.
9+
* Provides a `StorageKey` enum to avoid magic strings, promoting type safety. Use the `stringValue` getter for the actual key string.
10+
* Defines a set of custom `StorageException` subclasses (`StorageWriteException`, `StorageReadException`, `StorageDeleteException`, `StorageClearException`, `StorageKeyNotFoundException`, `StorageTypeMismatchException`) to handle specific storage errors.
1011

1112
## Getting Started 🚀
1213

@@ -67,7 +68,7 @@ dependencies:
6768

6869
```dart
6970
import 'package:ht_kv_storage_service/ht_kv_storage_service.dart';
70-
// Import your concrete implementation
71+
// import 'package:your_package/your_storage_implementation.dart';
7172
7273
Future<void> main() async {
7374
// Obtain an instance of your HtKVStorageService implementation
@@ -76,27 +77,38 @@ dependencies:
7677
// final storageService = HtKVStorageSharedPreferences(prefs);
7778
7879
// Example usage:
79-
// await storageService.writeBool(key: StorageKeys.hasSeenOnboarding, value: true);
80-
// final hasSeenOnboarding = await storageService.readBool(key: StorageKeys.hasSeenOnboarding);
81-
// print('Has seen onboarding: $hasSeenOnboarding');
80+
try {
81+
// Use the stringValue getter for the key
82+
await storageService.writeBool(
83+
key: StorageKey.hasSeenOnboarding.stringValue,
84+
value: true,
85+
);
86+
final hasSeenOnboarding = await storageService.readBool(
87+
key: StorageKey.hasSeenOnboarding.stringValue,
88+
);
89+
print('Has seen onboarding: $hasSeenOnboarding');
90+
} on StorageWriteException catch (e) {
91+
print('Failed to write: ${e.message}, Key: ${e.key}');
92+
} on StorageReadException catch (e) {
93+
print('Failed to read: ${e.message}, Key: ${e.key}');
94+
} on StorageTypeMismatchException catch (e) {
95+
print('Type mismatch: ${e.message}, Key: ${e.key}, Expected: ${e.expectedType}, Found: ${e.actualType}');
96+
} catch (e) {
97+
// Handle other potential exceptions
98+
print('An unexpected error occurred: $e');
99+
}
82100
}
83101
```
84102

85-
## Running Tests 🧪
86-
87-
This package uses [Very Good Analysis](https://pub.dev/packages/very_good_analysis) for static analysis and aims for high test coverage.
103+
## Error Handling
88104

89-
To run tests and generate coverage:
90-
91-
```sh
92-
dart pub global activate coverage 1.2.0 # Activate coverage tool if needed
93-
dart test --coverage=coverage
94-
dart pub global run coverage:format_coverage --lcov --in=coverage --out=coverage/lcov.info
95-
```
105+
The `HtKVStorageService` methods may throw specific exceptions derived from `StorageException` upon failure:
96106

97-
To view the HTML coverage report (requires `lcov` and `genhtml`):
107+
* `StorageWriteException`: Thrown by `write*` methods on failure.
108+
* `StorageReadException`: Thrown by `read*` methods on general read failure.
109+
* `StorageDeleteException`: Thrown by `delete` on failure.
110+
* `StorageClearException`: Thrown by `clearAll` on failure.
111+
* `StorageKeyNotFoundException`: May be thrown by `delete` if the key doesn't exist (implementation-dependent). `read*` methods typically return `null` or a default value instead.
112+
* `StorageTypeMismatchException`: Thrown by `read*` methods if the stored data type doesn't match the expected type.
98113

99-
```sh
100-
genhtml coverage/lcov.info -o coverage/
101-
open coverage/index.html # On macOS/Linux
102-
# start coverage/index.html # On Windows
114+
Implementations should handle these exceptions appropriately (e.g., using `try-catch` blocks).

0 commit comments

Comments
 (0)