Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/api/exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ class ZulipApiException extends HttpException {
required this.code,
required this.data,
required super.message,
}) : assert(400 <= httpStatus && httpStatus <= 499);
}) : assert(400 <= httpStatus && httpStatus <= 499),
assert(!data.containsKey('result')
&& !data.containsKey('code')
&& !data.containsKey('msg'));

@override
String toString() {
Expand Down
19 changes: 19 additions & 0 deletions test/example_data.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:convert';
import 'dart:math';

import 'package:zulip/api/exception.dart';
import 'package:zulip/api/model/events.dart';
import 'package:zulip/api/model/initial_snapshot.dart';
import 'package:zulip/api/model/model.dart';
Expand All @@ -18,10 +19,28 @@ void _checkPositive(int? value, String description) {
assert(value == null || value > 0, '$description should be positive');
}

////////////////////////////////////////////////////////////////
// Error objects.
//

Object nullCheckError() {
try { null!; } catch (e) { return e; } // ignore: null_check_always_fails
}

/// The error the server gives when the client's credentials
/// (API key together with email and realm URL) are no longer valid.
///
/// This isn't really documented, but comes from experiment and from
/// reading the server implementation. See:
/// https://github.com/zulip/zulip-flutter/pull/1183#discussion_r1945865983
/// https://chat.zulip.org/#narrow/channel/378-api-design/topic/general.20handling.20HTTP.20status.20code.20401/near/2090024
ZulipApiException apiExceptionUnauthorized({String routeName = 'someRoute'}) {
return ZulipApiException(
routeName: routeName,
httpStatus: 401, code: 'UNAUTHORIZED',
data: {}, message: 'Invalid API key');
}

////////////////////////////////////////////////////////////////
// Realm-wide (or server-wide) metadata.
//
Expand Down
18 changes: 3 additions & 15 deletions test/model/actions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:checks/checks.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:zulip/api/exception.dart';
import 'package:zulip/model/actions.dart';
import 'package:zulip/model/store.dart';
import 'package:zulip/notifications/receive.dart';
Expand Down Expand Up @@ -102,13 +101,7 @@ void main() {
check(testBinding.globalStore).accountIds.single.equals(eg.selfAccount.id);
const unregisterDelay = Duration(seconds: 5);
assert(unregisterDelay > TestGlobalStore.removeAccountDuration);
final exception = ZulipApiException(
httpStatus: 401,
code: 'UNAUTHORIZED',
data: {"result": "error", "msg": "Invalid API key", "code": "UNAUTHORIZED"},
routeName: 'removeEtcEtcToken',
message: 'Invalid API key',
);
final exception = eg.apiExceptionUnauthorized(routeName: 'removeEtcEtcToken');
final newConnection = separateConnection()
..prepare(delay: unregisterDelay, exception: exception);

Expand Down Expand Up @@ -170,14 +163,9 @@ void main() {
test('connection closed if request errors', () => awaitFakeAsync((async) async {
await prepare(ackedPushToken: '123');

final exception = eg.apiExceptionUnauthorized(routeName: 'removeEtcEtcToken');
final newConnection = separateConnection()
..prepare(exception: ZulipApiException(
httpStatus: 401,
code: 'UNAUTHORIZED',
data: {"result": "error", "msg": "Invalid API key", "code": "UNAUTHORIZED"},
routeName: 'removeEtcEtcToken',
message: 'Invalid API key',
));
..prepare(exception: exception);
final future = unregisterToken(testBinding.globalStore, eg.selfAccount.id);
async.elapse(Duration.zero);
await future;
Expand Down