From 22c8adca67923ecd290510d1d11b4eda02275be2 Mon Sep 17 00:00:00 2001 From: Jaap Aarts Date: Wed, 12 Apr 2023 21:41:07 +0200 Subject: [PATCH] Make default error messages better (#367) --- lib/api/concrexit_api_repository.dart | 2 ++ lib/api/exceptions.dart | 42 ++++++++++++++++++++------- lib/blocs/member_list_cubit.dart | 2 +- pubspec.yaml | 2 +- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/lib/api/concrexit_api_repository.dart b/lib/api/concrexit_api_repository.dart index 1a4ac3d8d..50168b2ab 100644 --- a/lib/api/concrexit_api_repository.dart +++ b/lib/api/concrexit_api_repository.dart @@ -95,6 +95,8 @@ class ConcrexitApiRepository implements ApiRepository { throw ApiException.notAllowed; case 404: throw ApiException.notFound; + case 500: + throw ApiException.internalServerError; default: throw ApiException.unknownError; } diff --git a/lib/api/exceptions.dart b/lib/api/exceptions.dart index 03385d6f5..e9ef333f0 100644 --- a/lib/api/exceptions.dart +++ b/lib/api/exceptions.dart @@ -12,12 +12,13 @@ class ApiException implements Exception { static const notAllowed = _NotAllowedException(); static const notLoggedIn = _NotLoggedInException(); static const noInternet = _NoInternetException(); + static const internalServerError = _InternalServerException(); - const ApiException._([this._message]); + const ApiException._(this._message); factory ApiException.message(String message) => _MessageException(message); - final String? _message; - String get message => _message ?? 'An unknown error occurred.'; + final String _message; + String get message => _message; bool get isMessage => this is _MessageException; @@ -35,27 +36,42 @@ class ApiException implements Exception { /// Without specifying `notFound`, the default message would also be used for /// [ApiException.notFound]. String getMessage({ + String? unknown, String? notFound, String? notAllowed, - String? unknown, + String? notLoggedIn, + String? noInternet, + String? serverError, }) { - if (this is _NotFoundException) return notFound ?? message; - if (this is _NotAllowedException) return notAllowed ?? message; - if (this is _UnknownException) return unknown ?? message; - return message; + switch (runtimeType) { + case _UnknownException: + return unknown ?? message; + case _NotFoundException: + return notFound ?? message; + case _NotAllowedException: + return notAllowed ?? message; + case _NotLoggedInException: + return notLoggedIn ?? message; + case _NoInternetException: + return notAllowed ?? message; + case _InternalServerException: + return serverError ?? message; + default: + return message; + } } } class _UnknownException extends ApiException { - const _UnknownException() : super._(); + const _UnknownException() : super._('An unknown error occurred.'); } class _NotFoundException extends ApiException { - const _NotFoundException() : super._(); + const _NotFoundException() : super._('Could not find this.'); } class _NotAllowedException extends ApiException { - const _NotAllowedException() : super._(); + const _NotAllowedException() : super._('You are not allowed to view this.'); } class _NotLoggedInException extends ApiException { @@ -66,6 +82,10 @@ class _NoInternetException extends ApiException { const _NoInternetException() : super._('Could not connect to the server.'); } +class _InternalServerException extends ApiException { + const _InternalServerException() : super._('Server error.'); +} + class _MessageException extends ApiException { const _MessageException(String message) : super._(message); } diff --git a/lib/blocs/member_list_cubit.dart b/lib/blocs/member_list_cubit.dart index 3a2b26922..bebd06074 100644 --- a/lib/blocs/member_list_cubit.dart +++ b/lib/blocs/member_list_cubit.dart @@ -97,7 +97,7 @@ class MemberListCubit extends Cubit { isDone: isDone, )); } on ApiException catch (exception) { - emit(MemberListState.failure(message: exception.getMessage())); + emit(MemberListState.failure(message: exception.message)); } } diff --git a/pubspec.yaml b/pubspec.yaml index c2fa0f7a4..482fcfcb3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -14,7 +14,7 @@ publish_to: 'none' # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion. # Read more about iOS versioning at # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html -version: 3.4.0 +version: 3.4.0+1 environment: sdk: '>=2.17.0 <3.0.0'