Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] [Bug]added error handling for api calls #409

Merged
merged 1 commit into from
Oct 22, 2024
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
130 changes: 100 additions & 30 deletions server/routes/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,18 @@
context,
request,
response
): Promise<IOpenSearchDashboardsResponse<any | ResponseError>> => {

Check warning on line 38 in server/routes/query.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
const retVal = await service.describeSQLQuery(context, request);
return response.ok({
body: retVal,
});
if (retVal.data.ok) {
return response.ok({
body: retVal,
});
} else {
return response.custom({
body: retVal.data.body,
statusCode: retVal.data.statusCode,
});
}
}
);

Expand All @@ -50,7 +57,7 @@
body: schema.any(),
query: schema.object({
dataSourceMDSId: schema.maybe(schema.string({ defaultValue: '' })),
}),

Check warning on line 60 in server/routes/query.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
},
},
async (
Expand All @@ -72,7 +79,7 @@
body: schema.any(),
query: schema.object({
dataSourceMDSId: schema.maybe(schema.string({ defaultValue: '' })),
}),

Check warning on line 82 in server/routes/query.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
},
},
async (
Expand All @@ -81,13 +88,20 @@
response
): Promise<IOpenSearchDashboardsResponse<any | ResponseError>> => {
const retVal = await service.describeSQLCsv(context, request);
return response.ok({
body: retVal,
});
if (retVal.data.ok) {
return response.ok({
body: retVal,
});
} else {
return response.custom({
body: retVal.data.body,
statusCode: retVal.data.statusCode,
});
}
}
);

server.post(

Check warning on line 104 in server/routes/query.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
{
path: ROUTE_PATH_PPL_CSV,
validate: {
Expand All @@ -103,9 +117,16 @@
response
): Promise<IOpenSearchDashboardsResponse<any | ResponseError>> => {
const retVal = await service.describePPLCsv(context, request);
return response.ok({
body: retVal,
});
if (retVal.data.ok) {
return response.ok({
body: retVal,
});
} else {
return response.custom({
body: retVal.data.body,

Check warning on line 126 in server/routes/query.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
statusCode: retVal.data.statusCode,
});
}
}
);

Expand All @@ -124,10 +145,17 @@
request,
response
): Promise<IOpenSearchDashboardsResponse<any | ResponseError>> => {
const retVal = await service.describeSQLJson(context, request);

Check warning on line 148 in server/routes/query.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
return response.ok({
body: retVal,
});
if (retVal.data.ok) {
return response.ok({
body: retVal,
});
} else {
return response.custom({
body: retVal.data.body,
statusCode: retVal.data.statusCode,
});
}
}
);

Expand All @@ -139,7 +167,7 @@
query: schema.object({
dataSourceMDSId: schema.maybe(schema.string({ defaultValue: '' })),
}),
},

Check warning on line 170 in server/routes/query.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
},
async (
context,
Expand All @@ -147,14 +175,21 @@
response
): Promise<IOpenSearchDashboardsResponse<any | ResponseError>> => {
const retVal = await service.describePPLJson(context, request);
return response.ok({
body: retVal,
});
if (retVal.data.ok) {
return response.ok({
body: retVal,
});
} else {
return response.custom({
body: retVal.data.body,
statusCode: retVal.data.statusCode,
});
}
}
);

server.post(
{

Check warning on line 192 in server/routes/query.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
path: ROUTE_PATH_SQL_TEXT,
validate: {
body: schema.any(),
Expand All @@ -169,9 +204,16 @@
response
): Promise<IOpenSearchDashboardsResponse<any | ResponseError>> => {
const retVal = await service.describeSQLText(context, request);
return response.ok({
body: retVal,
});
if (retVal.data.ok) {
return response.ok({
body: retVal,
});
} else {
return response.custom({
body: retVal.data.body,
statusCode: retVal.data.statusCode,

Check warning on line 214 in server/routes/query.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
});
}
}
);

Expand All @@ -191,9 +233,16 @@
response
): Promise<IOpenSearchDashboardsResponse<any | ResponseError>> => {
const retVal = await service.describePPLText(context, request);
return response.ok({
body: retVal,
});
if (retVal.data.ok) {

Check warning on line 236 in server/routes/query.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
return response.ok({
body: retVal,
});
} else {
return response.custom({
body: retVal.data.body,
statusCode: retVal.data.statusCode,
});
}
}
);

Expand All @@ -213,9 +262,16 @@
response
): Promise<IOpenSearchDashboardsResponse<any | ResponseError>> => {
const retVal = await service.describeSQLAsyncQuery(context, request);
return response.ok({
body: retVal,
});
if (retVal.data.ok) {
return response.ok({
body: retVal,
});
} else {
return response.custom({
body: retVal.data.body,
statusCode: retVal.data.statusCode,
});
}
}
);

Expand All @@ -240,9 +296,16 @@
request.params.id,
request.params.dataSourceMDSId
);
return response.ok({
body: retVal,
});
if (retVal.data.ok) {
return response.ok({
body: retVal,
});
} else {
return response.custom({
body: retVal.data.body,
statusCode: retVal.data.statusCode,
});
}
}
);

Expand Down Expand Up @@ -288,9 +351,16 @@
response
): Promise<IOpenSearchDashboardsResponse<any | ResponseError>> => {
const retVal = await service.describeSyncQueryDataSources(context, request);
return response.ok({
body: retVal,
});
if (retVal.data.ok) {
return response.ok({
body: retVal,
});
} else {
return response.custom({
body: retVal.data.body,
statusCode: retVal.data.statusCode,
});
}
}
);
}
9 changes: 6 additions & 3 deletions server/services/QueryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
* SPDX-License-Identifier: Apache-2.0
*/


Check failure on line 6 in server/services/QueryService.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `⏎`
import 'core-js/stable';
import _ from 'lodash';
import 'regenerator-runtime/runtime';
import { Logger, RequestHandlerContext } from '../../../../src/core/server';

Check failure on line 10 in server/services/QueryService.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `⏎`


export default class QueryService {

Check failure on line 13 in server/services/QueryService.ts

View workflow job for this annotation

GitHub Actions / Lint

Prefer named exports
private client: any;
private dataSourceEnabled: boolean;
private logger: Logger;
Expand All @@ -21,7 +21,7 @@
this.logger = logger;
}

describeQueryPostInternal = async (request: any, format: string, responseFormat: string, body: any, context: RequestHandlerContext) => {

Check failure on line 24 in server/services/QueryService.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `request:·any,·format:·string,·responseFormat:·string,·body:·any,·context:·RequestHandlerContext` with `⏎····request:·any,⏎····format:·string,⏎····responseFormat:·string,⏎····body:·any,⏎····context:·RequestHandlerContext⏎··`
try {
const params = {
body: JSON.stringify(body),
Expand All @@ -30,7 +30,7 @@
let client = this.client;
let queryResponse;

const {dataSourceMDSId} = request.query;

Check failure on line 33 in server/services/QueryService.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `dataSourceMDSId` with `·dataSourceMDSId·`
if (this.dataSourceEnabled && dataSourceMDSId) {
client = context.dataSource.opensearch.legacy.getClient(dataSourceMDSId);
queryResponse = await client.callAPI(format, params);
Expand All @@ -52,24 +52,25 @@
data: {
ok: false,
resp: err.message,
body: err.body
body: err.body,

Check failure on line 55 in server/services/QueryService.ts

View workflow job for this annotation

GitHub Actions / Lint

Insert `,`
statusCode: err.statusCode || 400
},
};
}
};

Check failure on line 61 in server/services/QueryService.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `request:·any,·format:·string,·jobId:·string,·responseFormat:·string,·context:·any,·dataSourceMDSId:·string` with `⏎····request:·any,⏎····format:·string,⏎····jobId:·string,⏎····responseFormat:·string,⏎····context:·any,⏎····dataSourceMDSId:·string⏎··`
describeQueryJobIdInternal = async (request: any, format: string, jobId: string, responseFormat: string, context: any, dataSourceMDSId: string) => {
try {
let client = this.client;
let queryResponse;

if (this.dataSourceEnabled && dataSourceMDSId) {

Check failure on line 67 in server/services/QueryService.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `⏎`

client = context.dataSource.opensearch.legacy.getClient(dataSourceMDSId);
queryResponse = await client.callAPI(format, {

Check failure on line 70 in server/services/QueryService.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected property shorthand
jobId: jobId,
});
} else {

Check failure on line 73 in server/services/QueryService.ts

View workflow job for this annotation

GitHub Actions / Lint

Insert `·`
queryResponse = await this.client.asScoped(request).callAsCurrentUser(format, {
jobId: jobId,
});
Expand All @@ -88,7 +89,8 @@
data: {
ok: false,
resp: err.message,
body: err.body
body: err.body,
statusCode: err.statusCode || 400
},
};
}
Expand Down Expand Up @@ -121,7 +123,8 @@
data: {
ok: false,
resp: err.message,
body: err.body
body: err.body,
statusCode: err.statusCode || 400
},
};
}
Expand Down
Loading