fix: handle backend connection errors on build (#7387)

* Changed polling build error handling

* Awaited pollBuildEvents for error to be thrown

* fix: enhance error handling and logging in get_flow_events_response

* fix: introduce custom exception for job queue not found error

* fix: replace ValueError with custom JobQueueNotFoundError in get_flow_events_response

* ⚡️ Speed up method `JobQueueService.get_queue_data` by 1,704% in PR #7387 (`fix/be_connection_errors`) (#7388)

To optimize the `JobQueueService` implementation for better runtime and memory efficiency, we can implement some optimizations focused mainly on avoiding redundant checks and streamlining the handling of job queues. The most significant improvement that can be made without altering the functionality would be to remove redundant error logging and to optimize our coroutine handling.

Here is the optimized version.



### Key Changes.
1. **Removed Redundant Logging**: The error logging inside the `get_queue_data` method was removed. In a production system where performance is critical, it’s often better to rely on exception handling rather than logging each potential error before raising the exception, especially for common errors which can be anticipated by the system. Instead, we directly raise `JobQueueNotFoundError`.
   
2. **Streamlined Exception Handling**: Replaced the dictionary lookup and manual check with a try-except block, simplifying the code and ensuring that the KeyError is handled efficiently. This avoids checking the condition and then looking up the dictionary again.

3. **Retained Essential Functionality**: All core functionalities and method signatures have been maintained, ensuring that the refactored code behaves identically to before. This includes maintaining the `self._closed` check to prevent operations on a closed service and handling of exceptions to provide appropriate error messages.

By focusing on reducing redundant operations like extra logging and double-checking conditions, we improve the software’s runtime performance and maintain cleaner code.

Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com>

* fix: improve error handling in JobQueueService by raising JobQueueNotFoundError with original exception context

* fix: update error message for job not found in test cases

* fix: enhance error handling in cancel_build by adding JobQueueNotFoundError exception

* fix: improve error handling in cancel_build by logging JobQueueNotFoundError details

---------

Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org>
Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com>
This commit is contained in:
Lucas Oliveira 2025-04-01 16:04:54 -03:00 • committed by GitHub
commit e58f764f3d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 56 additions and 27 deletions

View file

@ -184,7 +184,11 @@ async function pollBuildEvents(
});
if (!response.ok) {
throw new Error("Error polling build events");
const errorData = await response.json().catch(() => ({}));
throw new Error(
errorData.detail ||
"Langflow was not able to connect to the server. Please make sure your connection is working properly.",
);
}
const data = await response.json();
@ -362,7 +366,7 @@ export async function buildFlowVertices({
onGetOrderSuccess,
onValidateNodes,
};
return pollBuildEvents(
return await pollBuildEvents(
eventsUrl,
buildResults,
verticesStartTimeMs,
@ -377,7 +381,8 @@ export async function buildFlowVertices({
return;
}
onBuildError!("Error Building Flow", [
(error as Error).message || "An unexpected error occurred",
(error as Error).message ||
"Langflow was not able to connect to the server. Please make sure your connection is working properly.",
]);
throw error;
}