diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/api_log/api_log_flyout.tsx b/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/api_log/api_log_flyout.tsx index b3cc45801786c..dd53e997da0f7 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/api_log/api_log_flyout.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/api_log/api_log_flyout.tsx @@ -32,7 +32,7 @@ import { } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; -import { getStatusColor, safeJsonParseAndStringify } from '../utils'; +import { getStatusColor, attemptToFormatJson } from '../utils'; import { ApiLogLogic } from './'; @@ -112,7 +112,7 @@ export const ApiLogFlyout: React.FC = () => { })} - {safeJsonParseAndStringify(apiLog.request_body)} + {attemptToFormatJson(apiLog.request_body)} @@ -122,7 +122,7 @@ export const ApiLogFlyout: React.FC = () => { })} - {safeJsonParseAndStringify(apiLog.response_body)} + {attemptToFormatJson(apiLog.response_body)} diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/utils.test.ts b/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/utils.test.ts index a8966d6c3bb8b..ac464e2af353d 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/utils.test.ts +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/utils.test.ts @@ -7,7 +7,7 @@ import dedent from 'dedent'; -import { getDateString, getStatusColor, safeJsonParseAndStringify } from './utils'; +import { getDateString, getStatusColor, attemptToFormatJson } from './utils'; describe('getDateString', () => { const mockDate = jest @@ -35,9 +35,9 @@ describe('getStatusColor', () => { }); }); -describe('safeJsonParseAndStringify', () => { +describe('attemptToFormatJson', () => { it('takes an unformatted JSON string and correctly newlines/indents it', () => { - expect(safeJsonParseAndStringify('{"hello":"world","lorem":{"ipsum":"dolor","sit":"amet"}}')) + expect(attemptToFormatJson('{"hello":"world","lorem":{"ipsum":"dolor","sit":"amet"}}')) .toEqual(dedent`{ "hello": "world", "lorem": { @@ -48,6 +48,6 @@ describe('safeJsonParseAndStringify', () => { }); it('returns the original content if it is not properly formatted JSON', () => { - expect(safeJsonParseAndStringify('{invalid json}')).toEqual('{invalid json}'); + expect(attemptToFormatJson('{invalid json}')).toEqual('{invalid json}'); }); }); diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/utils.ts b/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/utils.ts index 1f78da8dd2075..7e5f19686f13b 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/utils.ts +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/utils.ts @@ -20,12 +20,12 @@ export const getStatusColor = (status: number) => { return color; }; -export const attemptToFormatPossibleJson = (possibleJson: string) => { +export const attemptToFormatJson = (possibleJson: string) => { try { - // it is JSON, we can format it + // it is JSON, we can format it with newlines/indentation return JSON.stringify(JSON.parse(possibleJson), null, 2); } catch { - // it's not JSON, likely a string message, we return it rather than format it + // if it's not JSON, we return the original content return possibleJson; } };