From 5068fc8a01063dd20a07dfa3a4ff9a111f0c2bb6 Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Sat, 8 May 2021 03:22:21 +0800 Subject: [PATCH] Improve error message for wrong password --- lib/api/api_util.dart | 5 +++-- lib/bloc/app_password_exchange.dart | 10 ++++++++-- lib/l10n/app_en.arb | 4 ++++ lib/widget/connect.dart | 17 +++++++++++++---- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/lib/api/api_util.dart b/lib/api/api_util.dart index a0f5df62..c64bfc20 100644 --- a/lib/api/api_util.dart +++ b/lib/api/api_util.dart @@ -80,8 +80,9 @@ Future exchangePassword(Account account) async { } else { _log.severe( "[exchangePassword] Failed while requesting app password: $response"); - throw HttpException( - "Failed communicating with server: ${response.statusCode}"); + throw ApiException( + response: response, + message: "Failed communicating with server: ${response.statusCode}"); } } diff --git a/lib/bloc/app_password_exchange.dart b/lib/bloc/app_password_exchange.dart index 6a5434f6..28196eb8 100644 --- a/lib/bloc/app_password_exchange.dart +++ b/lib/bloc/app_password_exchange.dart @@ -77,8 +77,14 @@ class AppPasswordExchangeBloc _log.warning("[_exchangePassword] Invalid base url"); yield AppPasswordExchangeBlocFailure(e); } catch (e, stacktrace) { - _log.shout("[_exchangePassword] Failed while exchanging password", e, - stacktrace); + if (e is ApiException && e.response.statusCode == 401) { + // wrong password, normal + _log.warning( + "[_exchangePassword] Server response 401, wrong password?"); + } else { + _log.shout("[_exchangePassword] Failed while exchanging password", e, + stacktrace); + } yield AppPasswordExchangeBlocFailure(e); } } diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 3662bd4d..41d02aa1 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -383,5 +383,9 @@ "errorInvalidBaseUrl": "Unable to communicate. Please make sure the address is the base URL of your Nextcloud instance", "@errorInvalidBaseUrl": { "description": "Error message when the base URL is invalid" + }, + "errorWrongPassword": "Unable to authenticate. Please double check the username and password", + "@errorWrongPassword": { + "description": "Error message when the username or password is wrong" } } \ No newline at end of file diff --git a/lib/widget/connect.dart b/lib/widget/connect.dart index 23d9acb2..b3288f0d 100644 --- a/lib/widget/connect.dart +++ b/lib/widget/connect.dart @@ -6,6 +6,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:logging/logging.dart'; import 'package:nc_photos/account.dart'; import 'package:nc_photos/bloc/app_password_exchange.dart'; +import 'package:nc_photos/exception.dart'; import 'package:nc_photos/exception_util.dart' as exception_util; import 'package:nc_photos/k.dart' as k; import 'package:nc_photos/snack_bar_manager.dart'; @@ -95,10 +96,18 @@ class _ConnectState extends State { _log.info("[_onStateChange] Account is good: $newAccount"); Navigator.of(context).pop(newAccount); } else if (state is AppPasswordExchangeBlocFailure) { - SnackBarManager().showSnackBar(SnackBar( - content: Text(exception_util.toUserString(state.exception, context)), - duration: k.snackBarDurationNormal, - )); + if (state.exception is ApiException && + (state.exception as ApiException).response.statusCode == 401) { + SnackBarManager().showSnackBar(SnackBar( + content: Text(AppLocalizations.of(context).errorWrongPassword), + duration: k.snackBarDurationNormal, + )); + } else { + SnackBarManager().showSnackBar(SnackBar( + content: Text(exception_util.toUserString(state.exception, context)), + duration: k.snackBarDurationNormal, + )); + } Navigator.of(context).pop(null); } }