Skip to content

Commit

Permalink
not wrap 401 error for datasource client
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongnansu committed Sep 26, 2022
1 parent 6af9845 commit 82a8d3a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/core/server/opensearch/legacy/cluster_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const noop = () => undefined;
* OpenSearch JS client.
* @param options Options that affect the way we call the API and process the result.
*/
export const callAPI = async (
const callAPI = async (
client: Client,
endpoint: string,
clientParams: Record<string, any> = {},
Expand Down
54 changes: 51 additions & 3 deletions src/plugins/data_source/server/legacy/configure_legacy_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
*/

import { Client } from 'elasticsearch';
// eslint-disable-next-line @osd/eslint/no-restricted-paths
import { callAPI } from '../../../../../src/core/server/opensearch/legacy/cluster_client';
import { Headers, LegacyAPICaller, Logger, SavedObject } from '../../../../../src/core/server';
import { get } from 'lodash';
import {
Headers,
LegacyAPICaller,
LegacyCallAPIOptions,
LegacyOpenSearchErrorHelpers,
Logger,
SavedObject,
} from '../../../../../src/core/server';
import {
AuthType,
DataSourceAttributes,
Expand Down Expand Up @@ -87,6 +93,48 @@ const getRootClient = (
}
};

/**
* Calls the OpenSearch API endpoint with the specified parameters.
* @param client Raw OpenSearch JS client instance to use.
* @param endpoint Name of the API endpoint to call.
* @param clientParams Parameters that will be directly passed to the
* OpenSearch JS client.
* @param options Options that affect the way we call the API and process the result.
* make wrap401Errors default to false, because we don't want browser login pop-up
*/
const callAPI = async (
client: Client,
endpoint: string,
clientParams: Record<string, any> = {},
options: LegacyCallAPIOptions = { wrap401Errors: false }
) => {
const clientPath = endpoint.split('.');
const api: any = get(client, clientPath);
if (!api) {
throw new Error(`called with an invalid endpoint: ${endpoint}`);
}

const apiContext = clientPath.length === 1 ? client : get(client, clientPath.slice(0, -1));
try {
return await new Promise((resolve, reject) => {
const request = api.call(apiContext, clientParams);
if (options.signal) {
options.signal.addEventListener('abort', () => {
request.abort();
reject(new Error('Request was aborted'));
});
}
return request.then(resolve, reject);
});
} catch (err) {
if (!options.wrap401Errors || err.statusCode !== 401) {
throw err;
}

throw LegacyOpenSearchErrorHelpers.decorateNotAuthorizedError(err);
}
};

/**
* Wrapper to expose API that allow calling the OpenSearch API endpoint with the specified
* parameters, using legacy client.
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data_source/server/lib/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export class DataSourceConfigError extends OsdError {
? error.output.payload.message
: error.message;
super(messagePrefix + messageContent);
// Cast all 5xx error returned by saveObjectClient to 500, 400 for both savedObject client
// 4xx errors, and other errors
// Cast all 5xx error returned by saveObjectClient to 500.
// Case both savedObject client 4xx errors, and other errors to 400
this.statusCode = SavedObjectsErrorHelpers.isOpenSearchUnavailableError(error) ? 500 : 400;
}
}

0 comments on commit 82a8d3a

Please sign in to comment.