Skip to content

Commit 4fa79bb

Browse files
committed
fake_api [nfc]: Check for confusion in httpException use
1 parent 314ce48 commit 4fa79bb

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

test/api/fake_api.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,24 @@ class FakeApiConnection extends ApiConnection {
241241
}) {
242242
assert(isOpen);
243243

244+
// The doc on [http.BaseClient.send] goes further than the following
245+
// condition, suggesting that any exception thrown there should be an
246+
// [http.ClientException]. But from the upstream implementation, in the
247+
// actual live app, we already get TlsException and SocketException,
248+
// without them getting wrapped in http.ClientException as that specifies.
249+
// So naturally our tests need to simulate those too.
250+
if (httpException is ApiRequestException) {
251+
throw FlutterError.fromParts([
252+
ErrorSummary('FakeApiConnection.prepare was passed an ApiRequestException.'),
253+
ErrorDescription(
254+
'The `httpException` parameter to FakeApiConnection.prepare describes '
255+
'an exception for the underlying HTTP request to throw. '
256+
'In the actual app, that will never be a Zulip-specific exception '
257+
'like an ApiRequestException.'),
258+
ErrorHint('Try using the `apiException` parameter instead.')
259+
]);
260+
}
261+
244262
if (apiException != null) {
245263
assert(httpException == null
246264
&& httpStatus == null && json == null && body == null);

test/api/fake_api_test.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:test/scaffolding.dart';
55
import 'package:zulip/api/exception.dart';
66

77
import '../fake_async.dart';
8+
import '../stdlib_checks.dart';
89
import 'exception_checks.dart';
910
import 'fake_api.dart';
1011

@@ -34,6 +35,15 @@ void main() {
3435
..cause.identicalTo(exception));
3536
});
3637

38+
test('error message on prepare API exception as "HTTP exception"', () async {
39+
final connection = FakeApiConnection();
40+
final exception = ZulipApiException(routeName: 'someRoute',
41+
httpStatus: 456, code: 'SOME_ERROR',
42+
data: {'foo': ['bar']}, message: 'Something failed');
43+
check(() => connection.prepare(httpException: exception))
44+
.throws<Error>().asString.contains('apiException');
45+
});
46+
3747
test('prepare API exception', () async {
3848
final connection = FakeApiConnection();
3949
final exception = ZulipApiException(routeName: 'someRoute',

test/stdlib_checks.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ extension NullableMapChecks<K, V> on Subject<Map<K, V>?> {
2525
}
2626
}
2727

28+
extension ErrorChecks on Subject<Error> {
29+
Subject<String> get asString => has((x) => x.toString(), 'toString'); // TODO(checks): what's a good convention for this?
30+
}
31+
2832
/// Convert [object] to a pure JSON-like value.
2933
///
3034
/// The result is similar to `jsonDecode(jsonEncode(object))`, but without

0 commit comments

Comments
 (0)