65 lines
2.0 KiB
Dart
65 lines
2.0 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import '../api_error.dart';
|
|
import '../marianumcloud/talk/talk_error.dart';
|
|
import '../webuntis/webuntis_error.dart';
|
|
import 'app_exception.dart';
|
|
import 'network_exception.dart';
|
|
import 'parse_exception.dart';
|
|
import 'talk_exception.dart';
|
|
import 'webuntis_exception.dart';
|
|
|
|
const String _defaultFallback = 'Etwas ist schiefgelaufen. Bitte versuche es erneut.';
|
|
|
|
String errorToUserMessage(Object? error, {String fallback = _defaultFallback}) {
|
|
if (error == null) return fallback;
|
|
if (error is AppException) return error.userMessage;
|
|
|
|
if (error is TalkError) return TalkException(error).userMessage;
|
|
if (error is WebuntisError) return WebuntisException(error).userMessage;
|
|
|
|
if (error is SocketException) {
|
|
return const NetworkException().userMessage;
|
|
}
|
|
if (error is TimeoutException) {
|
|
return NetworkException.timeout().userMessage;
|
|
}
|
|
if (error is http.ClientException) {
|
|
return const NetworkException().userMessage;
|
|
}
|
|
if (error is HandshakeException) {
|
|
return 'Sichere Verbindung konnte nicht hergestellt werden.';
|
|
}
|
|
if (error is FormatException) {
|
|
return const ParseException().userMessage;
|
|
}
|
|
if (error is ApiError) {
|
|
return _stripDioPrefix(error.message);
|
|
}
|
|
|
|
return fallback;
|
|
}
|
|
|
|
String? errorToTechnicalDetails(Object? error) {
|
|
if (error == null) return null;
|
|
if (error is AppException) return error.technicalDetails ?? error.toString();
|
|
if (error is TalkError) return TalkException(error).technicalDetails;
|
|
if (error is WebuntisError) return WebuntisException(error).technicalDetails;
|
|
return error.toString();
|
|
}
|
|
|
|
bool errorAllowsRetry(Object? error) {
|
|
if (error == null) return true;
|
|
if (error is AppException) return error.allowRetry;
|
|
return true;
|
|
}
|
|
|
|
String _stripDioPrefix(String raw) {
|
|
// ApiError messages embed full request URIs; only surface the first line.
|
|
final firstLine = raw.split('\n').first.trim();
|
|
return firstLine.isEmpty ? _defaultFallback : firstLine;
|
|
}
|