Skip to content

Commit

Permalink
PR feedback: naming/comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cee-chen committed Apr 5, 2021
1 parent 9571b4c commit 3ca74f6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 './';

Expand Down Expand Up @@ -112,7 +112,7 @@ export const ApiLogFlyout: React.FC = () => {
})}
</ApiLogHeading>
<EuiCodeBlock language="json" paddingSize="m">
{safeJsonParseAndStringify(apiLog.request_body)}
{attemptToFormatJson(apiLog.request_body)}
</EuiCodeBlock>
<EuiSpacer />

Expand All @@ -122,7 +122,7 @@ export const ApiLogFlyout: React.FC = () => {
})}
</ApiLogHeading>
<EuiCodeBlock language="json" paddingSize="m">
{safeJsonParseAndStringify(apiLog.response_body)}
{attemptToFormatJson(apiLog.response_body)}
</EuiCodeBlock>
</EuiFlyoutBody>
</EuiFlyout>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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": {
Expand All @@ -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}');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};

0 comments on commit 3ca74f6

Please sign in to comment.