Skip to content

Commit a19e78b

Browse files
committed
api: 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 9ddfdce commit a19e78b

File tree

7 files changed

+42
-5
lines changed

7 files changed

+42
-5
lines changed

assets/l10n/app_en.arb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@
4646
"num": {"type": "int", "example": "4"}
4747
}
4848
},
49+
"errorNetworkRequestFailed": "Network request failed",
50+
"@errorNetworkRequestFailed": {
51+
"description": "Message for error dialog when a network request fails."
52+
},
53+
"errorMalformedResponse": "Server gave malformed response; HTTP status {httpStatus}",
54+
"@errorMalformedResponse": {
55+
"description": "Message for error dialog when an API call fails because we could not parse it.",
56+
"placeholders": {
57+
"httpStatus": {"type": "int", "example": "200"}
58+
}
59+
},
60+
"errorRequestFailed": "Network request failed: HTTP status {httpStatus}",
61+
"@errorRequestFailed": {
62+
"description": "Message for error dialog when an API call fails.",
63+
"placeholders": {
64+
"httpStatus": {"type": "int", "example": "500"}
65+
}
66+
},
4967
"userRoleOwner": "Owner",
5068
"@userRoleOwner": {
5169
"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.errorNetworkRequestFailed;
9496
}
9597
throw NetworkException(routeName: routeName, cause: e, message: message);
9698
}

lib/api/exception.dart

Lines changed: 7 additions & 3 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.
@@ -14,7 +16,7 @@ sealed class ApiRequestException implements Exception {
1416
/// A user-facing description of the error.
1517
///
1618
/// For [ZulipApiException] this is supplied by the server as the `message`
17-
/// property in the JSON response, and is translated into the user's language.
19+
/// property in the JSON response.
1820
final String message;
1921

2022
ApiRequestException({required this.routeName, required this.message});
@@ -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+
.errorRequestFailed(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+
.errorMalformedResponse(httpStatus));
119123
}

test/api/core_test.dart

Lines changed: 5 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,12 @@ 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+
checkRequest((foo: 'bar'), it()
161+
..message
162+
.equals(zulipLocalizations.errorNetworkRequestFailed));
159163
});
160164

161165
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)