Skip to content

Commit 8f6cc45

Browse files
committed
core: Add translations for exceptions related to api requests
Also hook up some other test suites that require localization contexts for tests to pass given this change.
1 parent 91b0459 commit 8f6cc45

File tree

7 files changed

+48
-4
lines changed

7 files changed

+48
-4
lines changed

assets/l10n/app_en.arb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
"@aboutPageTapToView": {
1616
"description": "Item subtitle in About Zulip page to navigate to Licenses page"
1717
},
18+
"apiConnectionNetworkRequestFailed": "Network request failed",
19+
"@apiConnectionNetworkRequestFailed": {
20+
"description": "Message for error dialog when a network request fails."
21+
},
1822
"chooseAccountPageTitle": "Choose account",
1923
"@chooseAccountPageTitle": {
2024
"description": "Title for ChooseAccountPage"
@@ -49,6 +53,26 @@
4953
}
5054
}
5155
},
56+
"serverExceptionMalformedResponse": "Server gave malformed response; HTTP status {httpStatus}",
57+
"@serverExceptionMalformedResponse": {
58+
"description": "Message for error dialog when an API call fails because we could not parse it.",
59+
"placeholders": {
60+
"httpStatus": {
61+
"type": "int",
62+
"example": "500"
63+
}
64+
}
65+
},
66+
"serverExceptionRequestFailed": "Network request failed: HTTP status {httpStatus}",
67+
"@serverExceptionRequestFailed": {
68+
"description": "Message for error dialog when an API call fails.",
69+
"placeholders": {
70+
"httpStatus": {
71+
"type": "int",
72+
"example": "500"
73+
}
74+
}
75+
},
5276
"userRoleOwner": "Owner",
5377
"@userRoleOwner": {
5478
"description": "Label for UserRole.owner"

lib/api/core.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'dart:io';
44
import 'package:http/http.dart' as http;
55

66
import '../log.dart';
7+
import '../model/localizations.dart';
78
import 'exception.dart';
89

910
/// A value for an API request parameter, to use directly without JSON encoding.
@@ -90,7 +91,8 @@ class ApiConnection {
9091
} else if (e is TlsException) {
9192
message = e.message;
9293
} else {
93-
message = 'Network request failed';
94+
final zulipLocalizations = GlobalLocalizations.zulipLocalizations;
95+
message = zulipLocalizations.apiConnectionNetworkRequestFailed;
9496
}
9597
throw NetworkException(routeName: routeName, cause: e, message: message);
9698
}

lib/api/exception.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
import '../model/localizations.dart';
3+
24
/// Some kind of error from a Zulip API network request.
35
sealed class ApiRequestException implements Exception {
46
/// The name of the Zulip API route for the request.
@@ -94,7 +96,8 @@ class Server5xxException extends ServerException {
9496
required super.httpStatus,
9597
required super.data,
9698
}) : assert(500 <= httpStatus && httpStatus <= 599),
97-
super(message: 'Network request failed: HTTP status $httpStatus'); // TODO(i18n)
99+
super(message: GlobalLocalizations.zulipLocalizations
100+
.serverExceptionRequestFailed(httpStatus));
98101
}
99102

100103
/// An error where the server's response doesn't match the Zulip API.
@@ -115,5 +118,6 @@ class MalformedServerResponseException extends ServerException {
115118
required super.routeName,
116119
required super.httpStatus,
117120
required super.data,
118-
}) : super(message: 'Server gave malformed response; HTTP status $httpStatus'); // TODO(i18n)
121+
}) : super(message: GlobalLocalizations.zulipLocalizations
122+
.serverExceptionMalformedResponse(httpStatus));
119123
}

test/api/core_test.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:io';
33

44
import 'package:checks/checks.dart';
55
import 'package:checks/context.dart';
6+
import 'package:flutter_gen/gen_l10n/zulip_localizations.dart';
67
import 'package:http/http.dart' as http;
78
import 'package:test/scaffolding.dart';
89
import 'package:zulip/api/core.dart';
@@ -153,9 +154,13 @@ void main() {
153154
..which(condition));
154155
}
155156

157+
final zulipLocalizations = lookupZulipLocalizations(ZulipLocalizations.supportedLocales.first);
156158
checkRequest(http.ClientException('Oops'), it()..message.equals('Oops'));
157159
checkRequest(const TlsException('Oops'), it()..message.equals('Oops'));
158-
checkRequest((foo: 'bar'), it()..message.equals('Network request failed'));
160+
final expectedMessage = zulipLocalizations.apiConnectionNetworkRequestFailed;
161+
checkRequest((foo: 'bar'), it()
162+
..message
163+
.equals(expectedMessage));
159164
});
160165

161166
test('API 4xx errors, well formed', () async {

test/widgets/action_sheet_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:checks/checks.dart';
22
import 'package:flutter/material.dart';
33
import 'package:flutter/services.dart';
4+
import 'package:flutter_gen/gen_l10n/zulip_localizations.dart';
45
import 'package:flutter_test/flutter_test.dart';
56
import 'package:zulip/api/model/model.dart';
67
import 'package:zulip/api/route/messages.dart';
@@ -48,6 +49,8 @@ Future<void> setupToMessageActionSheet(WidgetTester tester, {
4849

4950
await tester.pumpWidget(
5051
MaterialApp(
52+
localizationsDelegates: ZulipLocalizations.localizationsDelegates,
53+
supportedLocales: ZulipLocalizations.supportedLocales,
5154
home: GlobalStoreWidget(
5255
child: PerAccountStoreWidget(
5356
accountId: eg.selfAccount.id,

test/widgets/autocomplete_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:checks/checks.dart';
22
import 'package:flutter/material.dart';
3+
import 'package:flutter_gen/gen_l10n/zulip_localizations.dart';
34
import 'package:flutter_test/flutter_test.dart';
45
import 'package:zulip/api/model/model.dart';
56
import 'package:zulip/api/route/messages.dart';
@@ -40,6 +41,8 @@ Future<Finder> setupToComposeInput(WidgetTester tester, {
4041

4142
await tester.pumpWidget(
4243
MaterialApp(
44+
localizationsDelegates: ZulipLocalizations.localizationsDelegates,
45+
supportedLocales: ZulipLocalizations.supportedLocales,
4346
home: GlobalStoreWidget(
4447
child: PerAccountStoreWidget(
4548
accountId: eg.selfAccount.id,

test/widgets/content_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:io';
33
import 'package:checks/checks.dart';
44
import 'package:flutter/foundation.dart';
55
import 'package:flutter/material.dart';
6+
import 'package:flutter_gen/gen_l10n/zulip_localizations.dart';
67
import 'package:flutter_test/flutter_test.dart';
78
import 'package:url_launcher/url_launcher.dart';
89
import 'package:zulip/api/core.dart';
@@ -68,6 +69,8 @@ void main() {
6869
addTearDown(testBinding.reset);
6970

7071
await tester.pumpWidget(GlobalStoreWidget(child: MaterialApp(
72+
localizationsDelegates: ZulipLocalizations.localizationsDelegates,
73+
supportedLocales: ZulipLocalizations.supportedLocales,
7174
home: PerAccountStoreWidget(accountId: eg.selfAccount.id,
7275
child: BlockContentList(
7376
nodes: parseContent(html).nodes)))));

0 commit comments

Comments
 (0)