Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions app/lib/backend/http/http_pool_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import 'package:http/http.dart' as http;
import 'package:http/io_client.dart';
import 'package:pool/pool.dart';

import 'package:omi/utils/logger.dart';

class HttpPoolManager {
static final HttpPoolManager instance = HttpPoolManager._();

Expand Down Expand Up @@ -73,6 +75,8 @@ class HttpPoolManager {
lastError = e;
} on http.ClientException catch (e) {
lastError = e;
} on HandshakeException catch (e) {
lastError = e;
} catch (e) {
lastError = e;
rethrow;
Expand All @@ -84,14 +88,29 @@ class HttpPoolManager {
}

if (lastResponse != null) return lastResponse;

// Return a synthetic error response for network errors instead of throwing,
// so callers that don't expect exceptions won't crash.
if (lastError is SocketException ||
lastError is TimeoutException ||
lastError is http.ClientException ||
lastError is HandshakeException) {
return http.Response('', 503, reasonPhrase: 'Network error: $lastError');
}

throw lastError ?? Exception('Request failed with unknown error');
}

Future<http.StreamedResponse> sendStreaming(
http.BaseRequest request, {
Duration timeout = const Duration(minutes: 5),
}) {
return _client.send(request).timeout(timeout);
}) async {
try {
return await _client.send(request).timeout(timeout);
} on SocketException catch (e) {
Logger.debug('HttpPoolManager sendStreaming SocketException: $e');
return http.StreamedResponse(const Stream.empty(), 503, reasonPhrase: 'Socket error: $e');
}
}

void dispose() {
Expand Down