Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions changelogs/fragments/11543.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feat:
- Enhance error handling for chat ([#11543](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/11543))
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
OpenSearchClient,
} from '../../../../../core/server';
import { MLAgentRouter } from './ml_agent_router';
import { MLClientError } from './ml_client_error';

/**
* Generic ML client detector with caching for performance
Expand Down Expand Up @@ -118,6 +119,13 @@ export class GenericMLRouter implements MLAgentRouter {
context
);

if (mlResponse.status && mlResponse.status >= 400) {
const mlClientError = new MLClientError(mlResponse.body);
mlClientError.statusCode = mlResponse.status;
mlClientError.statusText = mlResponse.statusText;
throw mlClientError;
}

// Handle response based on type
if (typeof mlResponse === 'object' && 'body' in mlResponse) {
return typeof mlResponse.body === 'string' ? JSON.parse(mlResponse.body) : mlResponse.body;
Expand Down
9 changes: 9 additions & 0 deletions src/plugins/chat/server/routes/ml_routes/ml_client_error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

export class MLClientError extends Error {
public statusCode?: number;
public statusText?: string;
}
Loading