Skip to content

Commit 32834bf

Browse files
authored
feat: 'JsonCacheTry' throws 'JsonCacheException' with stack trace (#110)
1 parent 6bc4cad commit 32834bf

8 files changed

+106
-55
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- made class `JsonCacheTry` to throw `JsonCacheException` with associated
13+
stack trace — [109](https://github.com/dartoos-dev/json_cache/issues/109).
14+
15+
- `JsonCacheException` prefixes error messages with 'JsonCacheException: '.
16+
17+
- bump up dependencies.
18+
1019
## [1.4.2] - 2022-09-10
1120

1221
### Fixed

lib/src/json_cache_exception.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// An exception to indicate cache operation failures.
22
class JsonCacheException<T extends Object> implements Exception {
3-
/// Sets [extra] as aditional information and [exception] as the original
3+
/// Sets [extra] as the aditional information and [exception] as the original
44
/// exception.
55
const JsonCacheException({required this.extra, this.exception});
66

@@ -15,7 +15,8 @@ class JsonCacheException<T extends Object> implements Exception {
1515

1616
/// Returns [extra] along with the original exception message.
1717
@override
18-
String toString() => '$extra$_separator$_originalExceptionMessage';
18+
String toString() =>
19+
'JsonCacheException: $extra$_separator$_originalExceptionMessage';
1920

2021
/// Returns '\n' if [exception] is set; otherwise, the empty string ''.
2122
String get _separator => exception != null ? '\n' : '';

lib/src/json_cache_try.dart

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ class JsonCacheTry implements JsonCache {
2121
Future<void> clear() async {
2222
try {
2323
await _wrapped.clear();
24-
} on Exception catch (ex) {
25-
throw JsonCacheException(
26-
extra: 'Error clearing cached data.',
27-
exception: ex,
24+
} on Exception catch (ex, st) {
25+
Error.throwWithStackTrace(
26+
JsonCacheException(
27+
extra: 'Error clearing cached data.',
28+
exception: ex,
29+
),
30+
st,
2831
);
2932
}
3033
}
@@ -36,10 +39,13 @@ class JsonCacheTry implements JsonCache {
3639
Future<void> refresh(String key, Map<String, dynamic> value) async {
3740
try {
3841
await _wrapped.refresh(key, value);
39-
} on Exception catch (ex) {
40-
throw JsonCacheException(
41-
extra: "Error refreshing cached data at index '$key'.",
42-
exception: ex,
42+
} on Exception catch (ex, st) {
43+
Error.throwWithStackTrace(
44+
JsonCacheException(
45+
extra: "Error refreshing cached data at index '$key'.",
46+
exception: ex,
47+
),
48+
st,
4349
);
4450
}
4551
}
@@ -51,10 +57,13 @@ class JsonCacheTry implements JsonCache {
5157
Future<void> remove(String key) async {
5258
try {
5359
await _wrapped.remove(key);
54-
} on Exception catch (ex) {
55-
throw JsonCacheException(
56-
extra: "Error removing cached data at index '$key'.",
57-
exception: ex,
60+
} on Exception catch (ex, st) {
61+
Error.throwWithStackTrace(
62+
JsonCacheException(
63+
extra: "Error removing cached data at index '$key'.",
64+
exception: ex,
65+
),
66+
st,
5867
);
5968
}
6069
}
@@ -66,10 +75,13 @@ class JsonCacheTry implements JsonCache {
6675
Future<Map<String, dynamic>?> value(String key) async {
6776
try {
6877
return await _wrapped.value(key);
69-
} on Exception catch (ex) {
70-
throw JsonCacheException(
71-
extra: "Error retrieving cached data at index '$key'.",
72-
exception: ex,
78+
} on Exception catch (ex, st) {
79+
Error.throwWithStackTrace(
80+
JsonCacheException(
81+
extra: "Error retrieving cached data at index '$key'.",
82+
exception: ex,
83+
),
84+
st,
7385
);
7486
}
7587
}
@@ -81,10 +93,13 @@ class JsonCacheTry implements JsonCache {
8193
Future<bool> contains(String key) async {
8294
try {
8395
return await _wrapped.contains(key);
84-
} on Exception catch (ex) {
85-
throw JsonCacheException(
86-
extra: "Error checking for cached data at index '$key'.",
87-
exception: ex,
96+
} on Exception catch (ex, st) {
97+
Error.throwWithStackTrace(
98+
JsonCacheException(
99+
extra: "Error checking for cached data at index '$key'.",
100+
exception: ex,
101+
),
102+
st,
88103
);
89104
}
90105
}

pubspec.lock

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,42 +145,42 @@ packages:
145145
name: flutter_secure_storage
146146
url: "https://pub.dartlang.org"
147147
source: hosted
148-
version: "6.0.0"
148+
version: "6.1.0"
149149
flutter_secure_storage_linux:
150150
dependency: transitive
151151
description:
152152
name: flutter_secure_storage_linux
153153
url: "https://pub.dartlang.org"
154154
source: hosted
155-
version: "1.1.1"
155+
version: "1.1.2"
156156
flutter_secure_storage_macos:
157157
dependency: transitive
158158
description:
159159
name: flutter_secure_storage_macos
160160
url: "https://pub.dartlang.org"
161161
source: hosted
162-
version: "1.1.1"
162+
version: "1.1.2"
163163
flutter_secure_storage_platform_interface:
164164
dependency: transitive
165165
description:
166166
name: flutter_secure_storage_platform_interface
167167
url: "https://pub.dartlang.org"
168168
source: hosted
169-
version: "1.0.0"
169+
version: "1.0.1"
170170
flutter_secure_storage_web:
171171
dependency: transitive
172172
description:
173173
name: flutter_secure_storage_web
174174
url: "https://pub.dartlang.org"
175175
source: hosted
176-
version: "1.0.2"
176+
version: "1.1.1"
177177
flutter_secure_storage_windows:
178178
dependency: transitive
179179
description:
180180
name: flutter_secure_storage_windows
181181
url: "https://pub.dartlang.org"
182182
source: hosted
183-
version: "1.1.2"
183+
version: "1.1.3"
184184
flutter_test:
185185
dependency: "direct dev"
186186
description: flutter
@@ -212,6 +212,13 @@ packages:
212212
url: "https://pub.dartlang.org"
213213
source: hosted
214214
version: "2.2.3"
215+
hive_test:
216+
dependency: "direct dev"
217+
description:
218+
name: hive_test
219+
url: "https://pub.dartlang.org"
220+
source: hosted
221+
version: "1.0.1"
215222
http_multi_server:
216223
dependency: transitive
217224
description:
@@ -302,7 +309,7 @@ packages:
302309
name: mutex
303310
url: "https://pub.dartlang.org"
304311
source: hosted
305-
version: "3.0.0"
312+
version: "3.0.1"
306313
node_preamble:
307314
dependency: transitive
308315
description:

pubspec.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ dependencies:
1313
encrypted_shared_preferences: ^3.0.1
1414
flutter:
1515
sdk: flutter
16-
flutter_secure_storage: ^6.0.0
16+
flutter_secure_storage: ^6.1.0
1717
hive: ^2.2.3
1818
localstorage: ^4.0.0+1
19-
mutex: ^3.0.0
19+
mutex: ^3.0.1
2020
shared_preferences: ^2.0.15
2121

2222
dev_dependencies:
2323
flutter_test:
2424
sdk: flutter
25+
hive_test: ^1.0.1
2526
lint: ^1.8.2
2627
mocktail: ^0.3.0
2728
test: ^1.18.2

test/json_cache_exception_test.dart

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,43 @@ import 'package:json_cache/json_cache.dart';
33

44
void main() {
55
group('JsonCacheException', () {
6-
test('Defined Original Exception', () {
7-
const errorMessage = 'A caching operation failure has occurred';
8-
final originalException = Exception(errorMessage);
9-
const extraInfo = 'Error while reading from cache';
6+
group('with original exception:', () {
7+
const errorMsg = 'A caching operation failure has occurred';
8+
final exception = Exception(errorMsg);
9+
const extra = 'Error while reading from cache';
1010
final jsonCacheException = JsonCacheException(
11-
extra: extraInfo,
12-
exception: originalException,
11+
extra: extra,
12+
exception: exception,
1313
);
14-
expect(identical(jsonCacheException.extra, extraInfo), true);
15-
expect(identical(jsonCacheException.exception, originalException), true);
16-
expect(jsonCacheException.toString(), '$extraInfo\n$originalException');
14+
test('must keep "extra" instance', () {
15+
expect(jsonCacheException.extra, same(extra));
16+
});
17+
test('must keep original exception instance', () {
18+
expect(jsonCacheException.exception, same(exception));
19+
});
20+
test('"toString()" must produce a prettily formated message', () {
21+
expect(
22+
jsonCacheException.toString(),
23+
'JsonCacheException: $extra\n$exception',
24+
);
25+
});
1726
});
27+
});
1828

19-
test('Undefined Original Exception', () {
20-
const extraInfo = 'Error while writing to cache';
21-
const jsonCacheException = JsonCacheException(extra: extraInfo);
22-
expect(identical(jsonCacheException.extra, extraInfo), true);
23-
expect(jsonCacheException.exception, null);
24-
expect(jsonCacheException.toString(), extraInfo);
29+
group('without original exception:', () {
30+
const extra = 'Error while writing to cache';
31+
const jsonCacheException = JsonCacheException(extra: extra);
32+
test('must keep "extra" instance', () {
33+
expect(jsonCacheException.extra, same(extra));
34+
});
35+
test('original exception instance must be "null"', () {
36+
expect(jsonCacheException.exception, isNull);
37+
});
38+
test('"toString()" must prefix "extra" with "JsonCacheException: "', () {
39+
expect(
40+
jsonCacheException.toString(),
41+
equals('JsonCacheException: $extra'),
42+
);
2543
});
2644
});
2745
}

test/json_cache_hive_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import 'package:flutter_test/flutter_test.dart';
22
import 'package:hive/hive.dart';
3+
import 'package:hive_test/hive_test.dart';
34
import 'package:json_cache/src/json_cache_hive.dart';
45

56
void main() {
6-
setUp(() {
7-
Hive.init('test');
7+
setUp(() async {
8+
await setUpTestHive();
89
});
910
tearDown(() async {
10-
await Hive.deleteFromDisk();
11-
await Hive.close();
11+
await tearDownTestHive();
1212
});
1313
group('JsonCacheHive', () {
1414
test('clear, value, refresh', () async {

test/json_cache_try_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void main() {
1313
throwsAssertionError,
1414
);
1515
});
16-
test('clear', () async {
16+
test('clear should throw "JsonCacheException"', () async {
1717
final JsonCacheTry jsonCacheTry = JsonCacheTry(jsonCacheMock);
1818
// Stub the 'clear' method.
1919
when(() => jsonCacheMock.clear()).thenThrow(Exception('Cache Failure'));
@@ -27,7 +27,7 @@ void main() {
2727
// Check if the interaction occurred only once.
2828
verify(() => jsonCacheMock.clear()).called(1);
2929
});
30-
test('contains', () async {
30+
test('contains should throw "JsonCacheException"', () async {
3131
final JsonCacheTry jsonCacheTry = JsonCacheTry(jsonCacheMock);
3232
// Stub the 'contains' method.
3333
when(() => jsonCacheMock.contains('aKey'))
@@ -42,7 +42,7 @@ void main() {
4242
// Check if the interaction occurred only once.
4343
verify(() => jsonCacheMock.contains('aKey')).called(1);
4444
});
45-
test('refresh', () async {
45+
test('refresh should throw "JsonCacheException"', () async {
4646
final JsonCacheTry jsonCacheTry = JsonCacheTry(jsonCacheMock);
4747
// Stub the 'refresh' method.
4848
when(() => jsonCacheMock.refresh('aKey', {}))
@@ -57,7 +57,7 @@ void main() {
5757
// Check if the interaction occurred only once.
5858
verify(() => jsonCacheMock.refresh('aKey', {})).called(1);
5959
});
60-
test('remove', () async {
60+
test('remove should throw "JsonCacheException"', () async {
6161
final JsonCacheTry jsonCacheTry = JsonCacheTry(jsonCacheMock);
6262
// Stub the 'remove' method.
6363
when(() => jsonCacheMock.remove('aKey'))
@@ -72,7 +72,7 @@ void main() {
7272
// Check if the interaction occurred only once.
7373
verify(() => jsonCacheMock.remove('aKey')).called(1);
7474
});
75-
test('value', () async {
75+
test('value should throw "JsonCacheException"', () async {
7676
final JsonCacheTry jsonCacheTry = JsonCacheTry(jsonCacheMock);
7777
// Stub the 'value' method.
7878
when(() => jsonCacheMock.value('aKey'))

0 commit comments

Comments
 (0)