Skip to content

Commit

Permalink
move error esponse handler from router.ts to resolve_index.ts
Browse files Browse the repository at this point in the history
Signed-off-by: Su <[email protected]>
  • Loading branch information
zhongnansu committed Oct 26, 2022
1 parent ddb267a commit 0d8f5e3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
10 changes: 0 additions & 10 deletions src/core/server/http/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,21 +301,11 @@ export class Router implements IRouter {
return e;
}

if (isDataSourceError(e)) {
return hapiResponseAdapter.handle(
opensearchDashboardsResponseFactory.badRequest({ body: e.message })
);
}

return hapiResponseAdapter.toInternalError();
}
}
}

const isDataSourceError = (error: any) => {
return error.constructor.name === 'DataSourceError' && error.statusCode === 400;
};

const convertOpenSearchUnauthorized = (
e: OpenSearchNotAuthorizedError
): ErrorHttpResponseOptions => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,25 @@ export function registerResolveIndexRoute(router: IRouter): void {
? context.dataSource.opensearch.legacy.getClient(dataSourceId).callAPI
: context.core.opensearch.legacy.client.callAsCurrentUser;

const result = await caller('transport.request', {
method: 'GET',
path: `/_resolve/index/${encodeURIComponent(req.params.query)}${
queryString ? '?' + new URLSearchParams(queryString).toString() : ''
}`,
});
return res.ok({ body: result });
try {
const result = await caller('transport.request', {
method: 'GET',
path: `/_resolve/index/${encodeURIComponent(req.params.query)}${
queryString ? '?' + new URLSearchParams(queryString).toString() : ''
}`,
});
return res.ok({ body: result });
} catch (err) {
return res.customError({
statusCode: err.statusCode || 500,
body: {
message: err.message,
attributes: {
error: err.body?.error || err.message,
},
},
});
}
}
);
}

0 comments on commit 0d8f5e3

Please sign in to comment.