Improve error message

This commit is contained in:
Ming Ming 2021-05-06 12:57:20 +08:00
parent ffbc543db7
commit 222170c2cc
5 changed files with 36 additions and 3 deletions

View file

@ -6,6 +6,7 @@ import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/api/api.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/exception.dart';
/// Return the preview image URL for [file]. See [getFilePreviewUrlRelative]
String getFilePreviewUrl(
@ -62,9 +63,15 @@ Future<String> exchangePassword(Account account) async {
},
);
if (response.isGood) {
final appPwdRegex = RegExp(r"<apppassword>(.*)</apppassword>");
final appPwdMatch = appPwdRegex.firstMatch(response.body);
return appPwdMatch.group(1);
try {
final appPwdRegex = RegExp(r"<apppassword>(.*)</apppassword>");
final appPwdMatch = appPwdRegex.firstMatch(response.body);
return appPwdMatch.group(1);
} catch (_) {
// this happens when the address is not the base URL and so Nextcloud
// returned the login page
throw InvalidBaseUrlException();
}
} else if (response.statusCode == 403) {
// If the client is authenticated with an app password a 403 will be
// returned

View file

@ -3,6 +3,7 @@ import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/api/api_util.dart' as api_util;
import 'package:nc_photos/exception.dart';
abstract class AppPasswordExchangeBlocEvent {
const AppPasswordExchangeBlocEvent();
@ -72,6 +73,9 @@ class AppPasswordExchangeBloc
try {
final appPwd = await api_util.exchangePassword(account);
yield AppPasswordExchangeBlocSuccess(appPwd);
} on InvalidBaseUrlException catch (e) {
_log.warning("[_exchangePassword] Invalid base url");
yield AppPasswordExchangeBlocFailure(e);
} catch (e, stacktrace) {
_log.shout("[_exchangePassword] Failed while exchanging password", e,
stacktrace);

View file

@ -45,3 +45,19 @@ class PermissionException implements Exception {
final dynamic message;
}
/// The Nextcloud base URL address is invalid
class InvalidBaseUrlException implements Exception {
InvalidBaseUrlException([this.message]);
@override
toString() {
if (message == null) {
return "InvalidBaseUrlException";
} else {
return "InvalidBaseUrlException: $message";
}
}
final dynamic message;
}

View file

@ -16,6 +16,8 @@ String toUserString(dynamic exception, BuildContext context) {
}
} else if (exception is SocketException) {
return AppLocalizations.of(context).errorDisconnected;
} else if (exception is InvalidBaseUrlException) {
return AppLocalizations.of(context).errorInvalidBaseUrl;
}
return exception.toString();
}

View file

@ -379,5 +379,9 @@
"errorLocked": "File is locked on server. Please try again later",
"@errorLocked": {
"description": "Error message when server responds with HTTP423"
},
"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"
}
}