Skip to content

Commit

Permalink
Throttle KbnUrlStateStorage error messages (#152983)
Browse files Browse the repository at this point in the history
Fix duplicate error messages caused by invalid URLs in Discover & Dashboard.
  • Loading branch information
kertal authored Mar 9, 2023
1 parent b3d8661 commit 63da800
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ describe('KbnUrlStateStorage', () => {
expect(cb).toBeCalledWith(expect.any(Error));
});

it('should notify about errors throttled', () => {
const cb = jest.fn();
urlStateStorage = createKbnUrlStateStorage({ useHash: false, history, onGetError: cb });
const key = '_s';
history.replace(`/#?${key}=(ok:2,test:`); // malformed rison
urlStateStorage.get(key);
urlStateStorage.get(key);
expect(cb).toBeCalledTimes(1);
});

describe('withNotifyOnErrors integration', () => {
test('toast is shown', () => {
const toasts = coreMock.createStart().notifications.toasts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { Observable, of } from 'rxjs';
import { catchError, map, share } from 'rxjs/operators';
import { History } from 'history';
import { throttle } from 'lodash';
import { IStateStorage } from './types';
import {
createKbnUrlControls,
Expand Down Expand Up @@ -74,6 +75,7 @@ export const createKbnUrlStateStorage = (
}
): IKbnUrlStateStorage => {
const url = createKbnUrlControls(history);
const onGetErrorThrottled = onGetError ? throttle((e) => onGetError(e), 100) : undefined;
return {
set: <State>(
key: string,
Expand All @@ -100,7 +102,7 @@ export const createKbnUrlStateStorage = (
try {
return getStateFromKbnUrl(key, url.getPendingUrl(), { getFromHashQuery: useHashQuery });
} catch (e) {
if (onGetError) onGetError(e);
if (onGetErrorThrottled) onGetErrorThrottled(e);
return null;
}
},
Expand All @@ -116,7 +118,7 @@ export const createKbnUrlStateStorage = (
}).pipe(
map(() => getStateFromKbnUrl<State>(key, undefined, { getFromHashQuery: useHashQuery })),
catchError((error) => {
if (onGetError) onGetError(error);
if (onGetErrorThrottled) onGetErrorThrottled(error);
return of(null);
}),
share()
Expand Down

0 comments on commit 63da800

Please sign in to comment.