From 938e6aabd53d7983e3fa383d36bc4edf711e5f54 Mon Sep 17 00:00:00 2001 From: Steffen Slavetinsky Date: Tue, 8 Aug 2023 10:12:33 +0200 Subject: [PATCH 1/3] :bug: :bug: dont fail when filter match fails instead remove filter and show message --- src/filters.ts | 6 +++++- src/stores/dataset/dataset.ts | 18 +++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/filters.ts b/src/filters.ts index d40cf392..144fac95 100644 --- a/src/filters.ts +++ b/src/filters.ts @@ -56,7 +56,11 @@ function matchString(value: string, ref: string) { if (!ref?.length) { return !value; } - return new RegExp(ref).test(value); + try { + return new RegExp(ref).test(value); + } catch (error) { + throw new Error(`Invalid regular expression: ${ref}`); + } } const predicatesByType: PredicateRegistry = { diff --git a/src/stores/dataset/dataset.ts b/src/stores/dataset/dataset.ts index 3b1488ed..92592b5c 100644 --- a/src/stores/dataset/dataset.ts +++ b/src/stores/dataset/dataset.ts @@ -18,7 +18,7 @@ import { TableData, } from '../../types'; import api from '../../api'; -import { notifyAPIError } from '../../notify'; +import { notifyAPIError, notifyError } from '../../notify'; import { makeColumnsColorTransferFunctions } from './colorTransferFunctionFactory'; import { makeColumn } from './columnFactory'; import { makeColumnsStats } from './statisticsFactory'; @@ -490,8 +490,20 @@ useDataset.subscribe( }; const isIndexFiltered = Array(length); - for (let i = 0; i < length; i++) { - isIndexFiltered[i] = filters.every((filter) => applyFilter(filter, i)); + try { + for (let i = 0; i < length; i++) { + isIndexFiltered[i] = filters.every((filter) => { + try { + return applyFilter(filter, i); + } catch (error) { + useDataset.getState().removeFilter(filter); + throw error; + } + }); + } + } catch (error) { + console.error(error); + notifyError(`Error applying filter\n${error}`); } const filteredIndices: number[] = []; isIndexFiltered.forEach((isFiltered, i) => { From be66b6eb9d281281c8d36ccbebdb1fed75c399a8 Mon Sep 17 00:00:00 2001 From: Steffen Slavetinsky Date: Tue, 8 Aug 2023 10:24:56 +0200 Subject: [PATCH 2/3] :lipstick: Error formatting --- src/stores/dataset/dataset.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stores/dataset/dataset.ts b/src/stores/dataset/dataset.ts index 92592b5c..39aabd46 100644 --- a/src/stores/dataset/dataset.ts +++ b/src/stores/dataset/dataset.ts @@ -503,7 +503,7 @@ useDataset.subscribe( } } catch (error) { console.error(error); - notifyError(`Error applying filter\n${error}`); + notifyError(`Error applying filter! '${error}'`); } const filteredIndices: number[] = []; isIndexFiltered.forEach((isFiltered, i) => { From fb0a43f59c3782e13d73725ac597523ae00bb156 Mon Sep 17 00:00:00 2001 From: Steffen Slavetinsky Date: Tue, 8 Aug 2023 10:35:25 +0200 Subject: [PATCH 3/3] :children_crossing: Fallback string comparison in string filter --- src/filters.ts | 2 +- src/types/filter.ts | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/filters.ts b/src/filters.ts index 144fac95..f8670771 100644 --- a/src/filters.ts +++ b/src/filters.ts @@ -59,7 +59,7 @@ function matchString(value: string, ref: string) { try { return new RegExp(ref).test(value); } catch (error) { - throw new Error(`Invalid regular expression: ${ref}`); + return value.includes(ref); } } diff --git a/src/types/filter.ts b/src/types/filter.ts index 66a98e59..9b5d5a0d 100644 --- a/src/types/filter.ts +++ b/src/types/filter.ts @@ -1,4 +1,6 @@ import * as datatypes from '../datatypes'; +import { isString } from '../datatypes'; +import { notifyError } from '../notify'; import type { DataColumn, TableData } from './dataset'; import { uniqueNamesGenerator, adjectives, animals } from 'unique-names-generator'; @@ -30,6 +32,19 @@ export class PredicateFilter extends Filter { this.column = column; this.predicate = predicate; this.referenceValue = referenceValue; + + if (isString(column.type)) { + // as this is a string column try to create a regex from the reference value + // in order to be able to inform the user when this fails and we are likely going to + // fallback to a simple string comparison + try { + new RegExp(referenceValue as string); + } catch (e) { + notifyError( + `Couldn't create regex from filter value ${referenceValue}. Falling back to string comparison.` + ); + } + } } get type(): datatypes.DataType {