Skip to content

Commit 56e39b8

Browse files
committed
api: Include stack trace when reporting MalformedServerResponseException
Fixes: zulip#1083
1 parent e524e6b commit 56e39b8

File tree

5 files changed

+12
-3
lines changed

5 files changed

+12
-3
lines changed

lib/api/core.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class ApiConnection {
177177
Error.throwWithStackTrace(
178178
MalformedServerResponseException(
179179
routeName: routeName, httpStatus: httpStatus, data: json,
180-
causeException: exception),
180+
causeException: exception, causeStackTrace: stackTrace),
181181
stackTrace);
182182
}
183183
}

lib/api/exception.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,18 @@ class MalformedServerResponseException extends ServerException {
149149
/// in order to preserve the underlying exception's stack trace, which
150150
/// may be more informative than the exception object itself.
151151
final Object? causeException;
152+
final StackTrace? causeStackTrace;
152153

153154
MalformedServerResponseException({
154155
required super.routeName,
155156
required super.httpStatus,
156157
required super.data,
157158
this.causeException,
158-
}) : super(message: causeException == null
159+
this.causeStackTrace
160+
}) : assert((causeException == null && causeStackTrace == null) ||
161+
(causeException != null && causeStackTrace != null),
162+
'causeException and causeStackTrace must either both be null or both be non-null'),
163+
super(message: causeException == null
159164
? GlobalLocalizations.zulipLocalizations
160165
.errorMalformedResponse(httpStatus)
161166
: GlobalLocalizations.zulipLocalizations

test/api/core_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,8 @@ void main() {
425425
} catch (e, st) {
426426
check(e).isA<MalformedServerResponseException>()
427427
..causeException.isA<DistinctiveError>()
428-
..message.contains("something is wrong");
428+
..message.contains("something is wrong")
429+
..causeStackTrace.toString().contains("distinctivelyNamedFromJson");
429430
check(st.toString()).contains("distinctivelyNamedFromJson");
430431
}
431432
});

test/api/exception_checks.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ extension Server5xxExceptionChecks on Subject<Server5xxException> {
2828

2929
extension MalformedServerResponseExceptionChecks on Subject<MalformedServerResponseException> {
3030
Subject<Object?> get causeException => has((e) => e.causeException, 'causeException');
31+
Subject<String> get causeStackTrace => has((e) => e.causeStackTrace.toString(), 'causeStackTrace');
3132
}

test/api/exception_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ void main() {
2525
});
2626

2727
// NetworkException.toString: see "API network errors" test in core_test.dart
28+
29+
// MalformedServerResponseException: see "malformed API success responses: exception preserves details" test in core_test.dart
2830
}

0 commit comments

Comments
 (0)