Skip to content

Commit 1271c42

Browse files
mohammad-t/improve_exceptions_to_string_function (#246)
- improve exceptions to string function
1 parent 76d11c9 commit 1271c42

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed
Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
1+
import 'dart:developer' as dev;
2+
13
import 'package:flutter_deriv_api/api/models/base_exception_model.dart';
24

3-
/// Base exception class
5+
/// Base exception class for all API exceptions.
46
class APIBaseException implements Exception {
57
/// Initializes
68
APIBaseException({
79
required this.baseExceptionModel,
810
});
911

10-
/// Exception code
12+
/// Exception code and message model from API response.
1113
final BaseExceptionModel? baseExceptionModel;
1214

13-
/// The exception's message
14-
String? get message => baseExceptionModel!.message;
15+
/// The exception's message (if any).
16+
String? get message => baseExceptionModel?.message;
1517

16-
/// The exception's code
17-
String? get code => baseExceptionModel!.code;
18+
/// The exception's code (if any).
19+
String? get code => baseExceptionModel?.code;
1820

1921
/// The exception's details.
20-
Map<String, dynamic>? get details => baseExceptionModel!.details;
22+
Map<String, dynamic>? get details => baseExceptionModel?.details;
2123

2224
@override
23-
String toString() =>
24-
'$runtimeType(code: ${baseExceptionModel!.code}, message: ${baseExceptionModel!.message})';
25+
String toString() {
26+
dev.log(
27+
'$runtimeType(code: $code, message: $message)',
28+
);
29+
30+
return message ?? '$runtimeType: API unknown error.';
31+
}
2532
}

0 commit comments

Comments
 (0)