Skip to content

Commit

Permalink
Make default error messages better (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
JAicewizard authored Apr 12, 2023
1 parent 5edbfd1 commit 22c8adc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
2 changes: 2 additions & 0 deletions lib/api/concrexit_api_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
42 changes: 31 additions & 11 deletions lib/api/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand All @@ -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);
}
2 changes: 1 addition & 1 deletion lib/blocs/member_list_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class MemberListCubit extends Cubit<MemberListState> {
isDone: isDone,
));
} on ApiException catch (exception) {
emit(MemberListState.failure(message: exception.getMessage()));
emit(MemberListState.failure(message: exception.message));
}
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 22c8adc

Please sign in to comment.