Skip to content

Commit

Permalink
Bento: Coreify deserializeMessage (#35563)
Browse files Browse the repository at this point in the history
* Core-ify deserializeMessage

* Should be devAssert

* Update tests

* Update src/3p-frame-messaging.js

Co-authored-by: Justin Ridgewell <justin@ridgewell.name>

* Use alias import

Co-authored-by: Justin Ridgewell <justin@ridgewell.name>
  • Loading branch information
caroqliu and jridgewell committed Aug 11, 2021
1 parent c9d58fe commit dc6cd22
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
17 changes: 10 additions & 7 deletions src/3p-frame-messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
* limitations under the License.
*/

import {internalListenImplementation} from './core/dom/event-helper-listen';
import {dict} from './core/types/object';
import {tryParseJson} from './core/types/object/json';
import {dev, devAssert} from './log';
import {devAssert} from '#core/assert';
import {internalListenImplementation} from '#core/dom/event-helper-listen';
import {rethrowAsync} from '#core/error';
import {dict} from '#core/types/object';
import {tryParseJson} from '#core/types/object/json';

/** @const */
const AMP_MESSAGE_PREFIX = 'amp-';
Expand Down Expand Up @@ -120,9 +121,11 @@ export function deserializeMessage(message) {
}
const startPos = message.indexOf('{');
devAssert(startPos != -1, 'JSON missing in %s', message);
return tryParseJson(message.substr(startPos), (e) =>
dev().error('MESSAGING', 'Failed to parse message: ' + message, e)
);
return tryParseJson(message.substr(startPos), (e) => {
rethrowAsync(
new Error(`MESSAGING: Failed to parse message: ${message}\n${e.message}`)
);
});
}

/**
Expand Down
8 changes: 1 addition & 7 deletions test/unit/3p/test-3p-frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import {
deserializeMessage,
serializeMessage,
} from '../../../src/3p-frame-messaging';
import {dev} from '../../../src/log';

describes.realWin('3p-frame', {amp: true}, (env) => {
let window, document;
Expand Down Expand Up @@ -605,23 +604,18 @@ describes.realWin('3p-frame', {amp: true}, (env) => {
});

it('should return null if the input is not a json', () => {
const errorStub = env.sandbox.stub(dev(), 'error');
expect(deserializeMessage('amp-other')).to.be.null;
expect(errorStub).to.not.be.called;
});

it('should return null if failed to parse the input', () => {
const errorStub = env.sandbox.stub(dev(), 'error');
expectAsyncConsoleError(/MESSAGING: Failed to parse message/i, 2);
expect(deserializeMessage('amp-{"type","sentinel":"msgsentinel"}')).to
.be.null;
expect(errorStub).to.be.calledOnce;

expect(
deserializeMessage(
'amp-{"type":"msgtype"|"sentinel":"msgsentinel"}'
)
).to.be.null;
expect(errorStub).to.be.calledTwice;
});
});
});
Expand Down

0 comments on commit dc6cd22

Please sign in to comment.