Skip to content

Commit 291315f

Browse files
committed
enhance input error handling
Signed-off-by: Jiaping Zeng <jpz@amazon.com>
1 parent 8f6c270 commit 291315f

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

plugin/src/main/java/org/opensearch/ml/rest/RestMLExecuteStreamAction.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,22 @@ public MLTaskResponse read(StreamInput in) throws IOException {
217217
);
218218

219219
return Mono.fromCompletionStage(future);
220-
} catch (IOException e) {
221-
return Mono.error(new OpenSearchStatusException("Failed to parse request", RestStatus.BAD_REQUEST, e));
220+
} catch (Exception e) {
221+
log.error("Failed to parse or process request", e);
222+
return Mono.error(e);
222223
}
223-
}).doOnNext(channel::sendChunk).onErrorComplete(ex -> {
224-
// Error handling
224+
}).doOnNext(channel::sendChunk).onErrorResume(ex -> {
225+
log.error("Error occurred", ex);
225226
try {
226-
channel.sendResponse(new BytesRestResponse(channel, (Exception) ex));
227-
return true;
228-
} catch (final IOException e) {
229-
throw new UncheckedIOException(e);
227+
String errorMessage = ex instanceof IOException
228+
? "Failed to parse request: " + ex.getMessage()
229+
: "Error processing request: " + ex.getMessage();
230+
HttpChunk errorChunk = createHttpChunk("data: {\"error\": \"" + errorMessage.replace("\"", "\\\"") + "\"}\n\n", true);
231+
channel.sendChunk(errorChunk);
232+
} catch (Exception e) {
233+
log.error("Failed to send error chunk", e);
230234
}
235+
return Mono.empty();
231236
}).subscribe();
232237
};
233238

0 commit comments

Comments
 (0)