fix(apps): consolidate FormzInput validators and fix login screen
- Move FormzInput validators to core/form_inputs/form_inputs.dart - Fix login_screen.dart syntax error (missing 'class' keyword) - Remove unused _isLoading field - Fix unnecessary const keywords - Update login_cubit and register_cubit imports - Remove duplicate FormzInput definitions from register_cubit - Add Toast and Banner UI feedback system - Remove legacy login/register screens (login_code, login_email, login_password, register_step2) - Remove unused warning_banner widget - Update tests for new error messages and DI setup
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
abstract class ApiException implements Exception {
|
||||
final String message;
|
||||
final int? statusCode;
|
||||
@@ -6,12 +8,58 @@ abstract class ApiException implements Exception {
|
||||
|
||||
factory ApiException.fromDioError(Object error) {
|
||||
if (error is ApiException) return error;
|
||||
return ServerException('Request failed: ${error.toString()}');
|
||||
}
|
||||
}
|
||||
if (error is DioException) {
|
||||
final response = error.response;
|
||||
final statusCode = response?.statusCode;
|
||||
final data = response?.data;
|
||||
|
||||
class NetworkException extends ApiException {
|
||||
const NetworkException(super.message);
|
||||
String detail;
|
||||
if (data is Map<String, dynamic>) {
|
||||
detail =
|
||||
(data['detail'] ?? data['message'] ?? data['error'])?.toString() ??
|
||||
'请求失败';
|
||||
} else {
|
||||
detail = '请求失败';
|
||||
}
|
||||
|
||||
final localized = _localizeError(detail, statusCode);
|
||||
|
||||
if (statusCode == 401) {
|
||||
return UnauthorizedException(localized);
|
||||
}
|
||||
if (statusCode == 422) {
|
||||
return ValidationException(
|
||||
localized,
|
||||
errors: data,
|
||||
statusCode: statusCode,
|
||||
);
|
||||
}
|
||||
return ServerException(localized, statusCode: statusCode);
|
||||
}
|
||||
return const ServerException('网络错误');
|
||||
}
|
||||
|
||||
static String _localizeError(String detail, int? statusCode) {
|
||||
if (statusCode == 401) {
|
||||
return '邮箱或密码错误';
|
||||
}
|
||||
if (statusCode == 403) {
|
||||
return '没有权限执行此操作';
|
||||
}
|
||||
if (statusCode == 404) {
|
||||
return '请求的资源不存在';
|
||||
}
|
||||
if (statusCode == 429) {
|
||||
return '请求过于频繁,请稍后再试';
|
||||
}
|
||||
if (statusCode != null && statusCode >= 500) {
|
||||
return '服务器错误,请稍后再试';
|
||||
}
|
||||
if (detail.contains('credentials') || detail.contains('password')) {
|
||||
return '邮箱或密码错误';
|
||||
}
|
||||
return detail;
|
||||
}
|
||||
}
|
||||
|
||||
class ServerException extends ApiException {
|
||||
@@ -19,7 +67,7 @@ class ServerException extends ApiException {
|
||||
}
|
||||
|
||||
class UnauthorizedException extends ApiException {
|
||||
const UnauthorizedException([super.message = 'Authentication required'])
|
||||
const UnauthorizedException([super.message = '请重新登录'])
|
||||
: super(statusCode: 401);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user