Skip to content

Commit 6484a56

Browse files
committed
feat(client): parse nested error messages from API responses
Updates the `ErrorInterceptor` to correctly parse the standardized nested error structure (`{"error": {"message": "..."}}`) now sent by the `ht_api` backend. The `_extractErrorMessage` method now prioritizes checking for this nested format, ensuring that specific, user-friendly error messages are extracted and propagated as the exception message. The previous logic for flat error messages is retained as a fallback for broader compatibility.
1 parent 0b56d92 commit 6484a56

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

lib/src/interceptors/error_interceptor.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,21 @@ class ErrorInterceptor extends Interceptor {
8989
if (responseData == null) return null;
9090

9191
if (responseData is Map) {
92-
// Common patterns: look for 'message', 'error', 'detail' keys
92+
// New Pattern: Check for the nested {"error": {"message": "..."}}
93+
// This is the standard format for ht_api.
94+
if (responseData.containsKey('error') && responseData['error'] is Map) {
95+
final errorMap = responseData['error'] as Map;
96+
if (errorMap.containsKey('message') && errorMap['message'] is String) {
97+
return errorMap['message'] as String;
98+
}
99+
}
100+
101+
// Fallback to common flat patterns: 'message', 'error', 'detail' keys.
93102
if (responseData.containsKey('message') &&
94103
responseData['message'] is String) {
95104
return responseData['message'] as String;
96105
}
97-
if (responseData.containsKey('error') &&
98-
responseData['error'] is String) {
106+
if (responseData.containsKey('error') && responseData['error'] is String) {
99107
return responseData['error'] as String;
100108
}
101109
if (responseData.containsKey('detail') &&

0 commit comments

Comments
 (0)