Skip to content

Commit

Permalink
UIHandler: ignore rejections from the underlying makeRequest() call i…
Browse files Browse the repository at this point in the history
…n UI interactions
  • Loading branch information
jiripudil committed Jan 12, 2021
1 parent e332691 commit 94d3ad4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/core/UIHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,16 @@ export class UIHandler extends EventTarget {
const element = event.currentTarget;
const options: Options = {};

const ignoreErrors = () => {
// don't reject the promise in case of an error as developers have no way of handling the rejection
// in this situation; errors should be handled in `naja.addEventListener('error', errorHandler)`
};

if (event.type === 'submit') {
this.submitForm(element as HTMLFormElement, options, event);
this.submitForm(element as HTMLFormElement, options, event).catch(ignoreErrors);

} else if (event.type === 'click') {
this.clickElement(element as HTMLElement, options, mouseEvent);
this.clickElement(element as HTMLElement, options, mouseEvent).catch(ignoreErrors);
}
}

Expand Down
20 changes: 20 additions & 0 deletions tests/Naja.UIHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,26 @@ describe('UIHandler', function () {
mock.verify();
});

it('failed request should not cause unhandled rejection', function () {
const naja = mockNaja();
const mock = sinon.mock(naja);
mock.expects('makeRequest')
.withExactArgs('GET', 'http://localhost:9876/UIHandler/a', null, {})
.once()
.returns(Promise.reject(new Error()));

const handler = new UIHandler(naja);

const evt = {
type: 'click',
currentTarget: this.a,
preventDefault: () => undefined,
};
handler.handleUI(evt);

mock.verify();
});

it('a.ajax', function () {
const naja = mockNaja();
const mock = sinon.mock(naja);
Expand Down

0 comments on commit 94d3ad4

Please sign in to comment.