Skip to content

Commit

Permalink
useGetItemIndexByValue error handling (#1889)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Apr 1, 2024
1 parent de2aa6d commit fe09fa6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
16 changes: 16 additions & 0 deletions packages/jsapi-components/src/useGetItemIndexByValue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ describe('useGetItemIndexByValue', () => {
}
);

it('should return null if seekRow fails', async () => {
asMock(mockTable.seekRow).mockRejectedValue('Some error');

const { result } = renderHook(() =>
useGetItemIndexByValue({
columnName: 'mock.columnName',
table: mockTable,
value: 'mock.value',
})
);

const actual = await result.current();

expect(actual).toBeNull();
});

it.each([
['mock.value', 42, 42],
['mock.value', -1, null],
Expand Down
13 changes: 10 additions & 3 deletions packages/jsapi-components/src/useGetItemIndexByValue.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { useCallback } from 'react';
import { dh } from '@deephaven/jsapi-types';
import Log from '@deephaven/log';
import { useTableUtils } from './useTableUtils';

const log = Log.module('useGetItemIndexByValue');

/**
* Returns a function that gets the index of the first row containing a column
* value.
Expand Down Expand Up @@ -30,9 +33,13 @@ export function useGetItemIndexByValue<TValue>({
const column = table.findColumn(columnName);
const columnValueType = tableUtils.getValueType(column.type);

const index = await table.seekRow(0, column, columnValueType, value);

return index === -1 ? null : index;
try {
const index = await table.seekRow(0, column, columnValueType, value);
return index === -1 ? null : index;
} catch (err) {
log.debug('Error seeking row', { column, value, columnValueType });
return null;
}
}, [columnName, table, tableUtils, value]);
}

Expand Down

0 comments on commit fe09fa6

Please sign in to comment.