mirror of
https://github.com/rzuasti/oott.git
synced 2026-07-08 19:21:54 +02:00
Break the 372-line frontend API client into focused files under lib/utils/api/: error mapping (api_error.dart), Dio setup behind a buildDio factory (dio_config.dart), and device/scanner/notification endpoints as extensions in part files. Collapse the five near-identical scanner-status methods via a generic _getModel helper and de-duplicate the pagination logic via a shared _paginate helper. The BackendAPI singleton and dioErrorToUserMessage remain importable from oott_api.dart unchanged, so no call sites are affected. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
41 lines
1.5 KiB
Dart
41 lines
1.5 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
String dioErrorToUserMessage(Object error) {
|
|
if (error is TypeError || error is FormatException) {
|
|
debugPrint('Backend response shape error: $error');
|
|
return 'Unexpected response shape from backend.';
|
|
}
|
|
if (error is! DioException) {
|
|
debugPrint('Non-Dio error from backend call: $error');
|
|
return 'Unexpected error.';
|
|
}
|
|
switch (error.type) {
|
|
case DioExceptionType.connectionTimeout:
|
|
case DioExceptionType.sendTimeout:
|
|
case DioExceptionType.receiveTimeout:
|
|
return 'Backend did not respond in time.';
|
|
case DioExceptionType.connectionError:
|
|
return 'Cannot reach backend. Check the base URL and your network.';
|
|
case DioExceptionType.badCertificate:
|
|
return 'Backend TLS certificate could not be verified.';
|
|
case DioExceptionType.cancel:
|
|
return 'Request canceled.';
|
|
case DioExceptionType.badResponse:
|
|
final status = error.response?.statusCode;
|
|
if (status == 401 || status == 403) {
|
|
return 'Authentication failed. Check your API key in Settings.';
|
|
}
|
|
if (status == 404) {
|
|
return 'Not found.';
|
|
}
|
|
if (status != null && status >= 500 && status < 600) {
|
|
return 'Backend error (status $status). Please try again.';
|
|
}
|
|
return 'Unexpected response from backend (status ${status ?? 'unknown'}).';
|
|
case DioExceptionType.unknown:
|
|
debugPrint('Unknown Dio error: ${error.message}');
|
|
return 'Unexpected error contacting backend.';
|
|
}
|
|
}
|