From 07791ce7db211eaf91b7f940dc888afe9f5e8275 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Wed, 27 Sep 2023 14:49:53 +0200 Subject: [PATCH] refactor(neon): Cleanup NeonError Signed-off-by: jld3103 --- packages/neon/neon/lib/src/widgets/error.dart | 86 ++++++++----------- 1 file changed, 37 insertions(+), 49 deletions(-) diff --git a/packages/neon/neon/lib/src/widgets/error.dart b/packages/neon/neon/lib/src/widgets/error.dart index fe445ba6..3131c77e 100644 --- a/packages/neon/neon/lib/src/widgets/error.dart +++ b/packages/neon/neon/lib/src/widgets/error.dart @@ -112,59 +112,47 @@ class NeonError extends StatelessWidget { @internal static NeonExceptionDetails getDetails(final dynamic error) { - if (error is String) { - return NeonExceptionDetails( - getText: (final _) => error, - ); - } - - if (error is NeonException) { - return error.details; - } - - if (error is DynamiteApiException) { - if (error.statusCode == 401) { + switch (error) { + case String(): return NeonExceptionDetails( - getText: (final context) => AppLocalizations.of(context).errorCredentialsForAccountNoLongerMatch, - isUnauthorized: true, + getText: (final _) => error, ); - } - - if (error.statusCode >= 500 && error.statusCode <= 599) { + case NeonException(): + return error.details; + case DynamiteApiException(): + if (error.statusCode == 401) { + return NeonExceptionDetails( + getText: (final context) => AppLocalizations.of(context).errorCredentialsForAccountNoLongerMatch, + isUnauthorized: true, + ); + } + if (error.statusCode >= 500 && error.statusCode <= 599) { + return NeonExceptionDetails( + getText: (final context) => AppLocalizations.of(context).errorServerHadAProblemProcessingYourRequest, + ); + } + case SocketException(): return NeonExceptionDetails( - getText: (final context) => AppLocalizations.of(context).errorServerHadAProblemProcessingYourRequest, + getText: (final context) => error.address != null + ? AppLocalizations.of(context).errorUnableToReachServerAt(error.address!.host) + : AppLocalizations.of(context).errorUnableToReachServer, + ); + case ClientException(): + return NeonExceptionDetails( + getText: (final context) => error.uri != null + ? AppLocalizations.of(context).errorUnableToReachServerAt(error.uri!.host) + : AppLocalizations.of(context).errorUnableToReachServer, + ); + case HttpException(): + return NeonExceptionDetails( + getText: (final context) => error.uri != null + ? AppLocalizations.of(context).errorUnableToReachServerAt(error.uri!.host) + : AppLocalizations.of(context).errorUnableToReachServer, + ); + case TimeoutException(): + return NeonExceptionDetails( + getText: (final context) => AppLocalizations.of(context).errorConnectionTimedOut, ); - } - } - - if (error is SocketException) { - return NeonExceptionDetails( - getText: (final context) => error.address != null - ? AppLocalizations.of(context).errorUnableToReachServerAt(error.address!.host) - : AppLocalizations.of(context).errorUnableToReachServer, - ); - } - - if (error is ClientException) { - return NeonExceptionDetails( - getText: (final context) => error.uri != null - ? AppLocalizations.of(context).errorUnableToReachServerAt(error.uri!.host) - : AppLocalizations.of(context).errorUnableToReachServer, - ); - } - - if (error is HttpException) { - return NeonExceptionDetails( - getText: (final context) => error.uri != null - ? AppLocalizations.of(context).errorUnableToReachServerAt(error.uri!.host) - : AppLocalizations.of(context).errorUnableToReachServer, - ); - } - - if (error is TimeoutException) { - return NeonExceptionDetails( - getText: (final context) => AppLocalizations.of(context).errorConnectionTimedOut, - ); } return NeonExceptionDetails(