no more streaming

This commit is contained in:
Para Dox
2025-06-01 21:56:27 +07:00
parent a454a57ff9
commit e046526ce9

View File

@@ -273,17 +273,45 @@ class RPCProxy {
requestId, requestId,
error: error.message, error: error.message,
stack: error.stack, stack: error.stack,
streamEndpoint: this.streamEndpoint,
compareEndpoint: this.compareEndpoint,
}, 'Error handling request'); }, 'Error handling request');
if (!res.headersSent && !clientClosed) { // Always try to send an error response if possible
res.status(500).json({ if (!res.headersSent && !res.writableEnded) {
jsonrpc: '2.0', try {
error: { // Send a proper JSON-RPC error response
code: -32603, res.status(502).json({
message: 'Internal error', jsonrpc: '2.0',
}, error: {
id: requestBody.id, code: -32603,
}); message: 'Internal error: Unable to connect to upstream RPC endpoints',
data: {
error: error.message,
streamEndpoint: this.streamEndpoint,
compareEndpoint: this.compareEndpoint,
}
},
id: requestBody.id || null,
});
logger.info({
requestId,
sentErrorResponse: true,
}, 'Sent error response to client');
} catch (sendError) {
logger.error({
requestId,
error: sendError.message,
}, 'Failed to send error response to client');
}
} else {
logger.warn({
requestId,
headersSent: res.headersSent,
writableEnded: res.writableEnded,
clientClosed,
}, 'Cannot send error response - headers already sent or connection closed');
} }
} }
} }
@@ -325,14 +353,36 @@ class RPCProxy {
streamEndpoint: this.streamEndpoint, streamEndpoint: this.streamEndpoint,
}, 'Making upstream request'); }, 'Making upstream request');
const response = await client.post('/', requestBody, { let response;
responseType: 'stream', try {
headers: { response = await client.post('/', requestBody, {
'Content-Type': 'application/json', responseType: 'stream',
'Accept-Encoding': acceptEncoding, // Forward client's encoding preference headers: {
}, 'Content-Type': 'application/json',
validateStatus: (status) => true, // Don't throw on any status 'Accept-Encoding': acceptEncoding, // Forward client's encoding preference
}); },
validateStatus: (status) => true, // Don't throw on any status
});
} catch (upstreamError) {
// Log the specific error details
logger.error({
requestId,
endpoint: 'stream',
error: upstreamError.message,
code: upstreamError.code,
streamEndpoint: this.streamEndpoint,
errno: upstreamError.errno,
syscall: upstreamError.syscall,
address: upstreamError.address,
port: upstreamError.port,
}, 'Failed to connect to upstream endpoint');
// Re-throw with more context
const enhancedError = new Error(`Failed to connect to upstream RPC endpoint at ${this.streamEndpoint}: ${upstreamError.message}`);
enhancedError.code = upstreamError.code;
enhancedError.originalError = upstreamError;
throw enhancedError;
}
upstreamResponse = response; upstreamResponse = response;
statusCode = response.status; statusCode = response.status;
@@ -521,6 +571,7 @@ class RPCProxy {
error: error.message, error: error.message,
code: error.code, code: error.code,
statusCode: error.response?.status, statusCode: error.response?.status,
streamEndpoint: this.streamEndpoint,
}, 'Stream request failed'); }, 'Stream request failed');
// Clean up upstream response if it exists // Clean up upstream response if it exists