Files
sendico/frontend/pweb/lib/utils/error_handler.dart

70 lines
2.9 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:flutter/material.dart';
import 'package:pshared/api/responses/error/connectivity.dart';
import 'package:pshared/api/responses/error/server.dart';
import 'package:pshared/config/constants.dart';
import 'package:pweb/generated/i18n/app_localizations.dart';
import 'package:pweb/services/accounts.dart';
class ErrorHandler {
/// A mapping of server-side error codes to localized user-friendly messages.
/// Update these keys to match the 'ErrorResponse.Error' field in your Go code.
static Map<String, String> getErrorMessagesLocs(AppLocalizations locs) {
return {
'account_not_verified': locs.errorAccountNotVerified,
'unauthorized': locs.errorLoginUnauthorized,
'verification_token_not_found': locs.errorVerificationTokenNotFound,
'internal_error': locs.errorInternalError,
'data_conflict': locs.errorDataConflict,
'access_denied': locs.errorAccessDenied,
'broken_payload': locs.errorBrokenPayload,
'invalid_argument': locs.errorInvalidArgument,
'broken_reference': locs.errorBrokenReference,
'invalid_query_parameter': locs.errorInvalidQueryParameter,
'not_implemented': locs.errorNotImplemented,
'license_required': locs.errorLicenseRequired,
'not_found': locs.errorNotFound,
'name_missing': locs.errorNameMissing,
'email_missing': locs.errorEmailMissing,
'password_missing': locs.errorPasswordMissing,
'email_not_registered': locs.errorEmailNotRegistered,
'duplicate_email': locs.errorDuplicateEmail,
};
}
static Map<String, String> getErrorMessages(BuildContext context) {
return getErrorMessagesLocs(AppLocalizations.of(context)!);
}
/// Determine which handler to use based on the runtime type of [e].
/// If no match is found, just return the errors string representation.
static String handleError(BuildContext context, Object e) {
return handleErrorLocs(AppLocalizations.of(context)!, e);
}
static String handleErrorLocs(AppLocalizations locs, Object e) {
final errorHandlers = <Type, String Function(Object)>{
ErrorResponse: (ex) => _handleErrorResponseLocs(locs, ex as ErrorResponse),
ConnectivityError: (ex) => _handleConnectivityErrorLocs(locs, ex as ConnectivityError),
InvalidCredentialsException: (_) => locs.errorLoginUnauthorized,
DuplicateAccountException: (_) => locs.errorAccountExists,
};
return errorHandlers[e.runtimeType]?.call(e) ?? e.toString();
}
static String _handleErrorResponseLocs(AppLocalizations locs, ErrorResponse e) {
final errorMessages = getErrorMessagesLocs(locs);
// Return the localized message if we recognize the error key, else use the raw details
return errorMessages[e.error] ?? e.details;
}
/// Handler for connectivity issues.
static String _handleConnectivityErrorLocs(AppLocalizations locs, ConnectivityError e) {
return locs.connectivityError(Constants.serviceUrl);
}
}