@@ -4,6 +4,7 @@ import 'dart:convert';
4
4
import 'package:flutter/foundation.dart' ;
5
5
import 'package:http/http.dart' as http;
6
6
import 'package:zulip/api/core.dart' ;
7
+ import 'package:zulip/api/exception.dart' ;
7
8
import 'package:zulip/model/store.dart' ;
8
9
9
10
import '../example_data.dart' as eg;
@@ -211,27 +212,47 @@ class FakeApiConnection extends ApiConnection {
211
212
212
213
/// Prepare the HTTP response for the next request.
213
214
///
214
- /// If `httpException` is null, the next request will produce an [http.Response]
215
+ /// If `httpException` and `apiException` are both null, then
216
+ /// the next request will produce an [http.Response]
215
217
/// with the given `httpStatus` , defaulting to 200. The body of the response
216
218
/// will be `body` if non-null, or `jsonEncode(json)` if `json` is non-null,
217
219
/// or else ''. The `body` and `json` parameters must not both be non-null.
218
220
///
219
- /// If `httpException` is non-null, then
221
+ /// If `httpException` is non-null, then `apiException` ,
220
222
/// `httpStatus` , `body` , and `json` must all be null, and the next request
221
223
/// will throw the given exception within the HTTP client layer,
222
224
/// causing the API request to throw a [NetworkException]
223
225
/// wrapping the given exception.
224
226
///
225
- /// In either case, the next request will complete a duration of `delay`
227
+ /// If `apiException` is non-null, then `httpException` ,
228
+ /// `httpStatus` , `body` , and `json` must all be null, and the next request
229
+ /// will throw an exception equivalent to the given exception
230
+ /// (except [ApiRequestException.routeName] , which is ignored).
231
+ ///
232
+ /// In each case, the next request will complete a duration of `delay`
226
233
/// after being started.
227
234
void prepare ({
228
235
Object ? httpException,
236
+ ZulipApiException ? apiException,
229
237
int ? httpStatus,
230
238
Map <String , dynamic >? json,
231
239
String ? body,
232
240
Duration delay = Duration .zero,
233
241
}) {
234
242
assert (isOpen);
243
+
244
+ if (apiException != null ) {
245
+ assert (httpException == null
246
+ && httpStatus == null && json == null && body == null );
247
+ httpStatus = apiException.httpStatus;
248
+ json = {
249
+ 'result' : 'error' ,
250
+ 'code' : apiException.code,
251
+ 'msg' : apiException.message,
252
+ ...apiException.data,
253
+ };
254
+ }
255
+
235
256
client.prepare (
236
257
exception: httpException,
237
258
httpStatus: httpStatus, json: json, body: body,
0 commit comments