Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Fix error message display issue in dev tool console (opensearch-proje…
Browse files Browse the repository at this point in the history
…ct#3652)

Fix error message display issue in dev tool console

When user sends request in the console and result in error, the console will output a customized error message.

Signed-off-by: Su <szhongna@amazon.com>
Signed-off-by: David Sinclair <david@sinclair.tech>
  • Loading branch information
zhongnansu authored and sikhote committed Apr 24, 2023
1 parent dc7af39 commit fb50e13
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/core/server/opensearch/client/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export type UnauthorizedError = ResponseError & {
};

export function isResponseError(error: any): error is ResponseError {
return Boolean(error.body && error.statusCode && error.headers);
return Boolean(error && error.body && error.statusCode && error.headers);
}

export function isUnauthorizedError(error: any): error is UnauthorizedError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@

import { OpenSearchDashboardsRequest, RequestHandler } from 'opensearch-dashboards/server';
import { trimStart } from 'lodash';
import { Readable } from 'stream';

import { ResponseError } from '@opensearch-project/opensearch/lib/errors';
import { ApiResponse } from '@opensearch-project/opensearch/';

// eslint-disable-next-line @osd/eslint/no-restricted-paths
import { ensureRawRequest } from '../../../../../../../core/server/http/router';
// eslint-disable-next-line @osd/eslint/no-restricted-paths
import { isResponseError } from '../../../../../../../core/server/opensearch/client/errors';

import { RouteDependencies } from '../../../';

Expand Down Expand Up @@ -127,15 +129,22 @@ export const createHandler = ({
},
});
} catch (e: any) {
log.error(e);
const isResponseErrorFlag = isResponseError(e);

const errorMessage = isResponseErrorFlag ? JSON.stringify(e.meta.body) : e.message;
// core http route handler has special logic that asks for stream readable input to pass error opaquely
const errorResponseBody = new Readable({
read() {
this.push(errorMessage);
this.push(null);
},
});
return response.customError({
statusCode: isResponseErrorFlag ? e.statusCode : 502,
body: isResponseErrorFlag ? JSON.stringify(e.meta.body) : `502.${e.statusCode || 0}`,
body: errorResponseBody,
headers: {
'Content-Type': 'application/json',
},
});
}
};

const isResponseError = (error: any): error is ResponseError => {
return Boolean(error && error.body && error.statusCode && error.header);
};

0 comments on commit fb50e13

Please sign in to comment.