Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datahub: handle filter geometry error #700

Merged
merged 4 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions libs/feature/search/src/lib/state/effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,45 @@ describe('Effects', () => {
})
)
})
describe('when geometry is broken', () => {
beforeEach(() => {
effects['filterGeometry$'] = of({
type: 'Polygon',
coordinates: [[]],
})
effects = TestBed.inject(SearchEffects)
actions$ = of(new RequestMoreResults('main'))
})
it('skips the geometry in the search', async () => {
await firstValueFrom(effects.loadResults$)
expect(repository.search).toHaveBeenCalledWith(
expect.not.objectContaining({
filterGeometry: { type: 'Polygon', coordinates: [[]] },
})
)
})
})
describe('when geometry is invalid', () => {
beforeEach(() => {
effects['filterGeometry$'] = of({
type: 'Polygon',
coordinates: [
[0, 1],
[0, 1],
],
}) as any
effects = TestBed.inject(SearchEffects)
actions$ = of(new RequestMoreResults('main'))
})
it('skips the geometry in the search', async () => {
await firstValueFrom(effects.loadResults$)
expect(repository.search).toHaveBeenCalledWith(
expect.not.objectContaining({
filterGeometry: { type: 'Polygon', coordinates: [[]] },
})
)
})
})
})
describe('when useSpatialFilter is disabled', () => {
beforeEach(() => {
Expand Down
21 changes: 19 additions & 2 deletions libs/feature/search/src/lib/state/effects.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Inject, Injectable, Optional } from '@angular/core'
import { Actions, createEffect, ofType } from '@ngrx/effects'
import { select, Store } from '@ngrx/store'
import { buffer, combineLatestWith, debounceTime, from, of } from 'rxjs'
import { buffer, combineLatestWith, debounceTime, from, of, tap } from 'rxjs'
import {
catchError,
map,
Expand Down Expand Up @@ -45,6 +45,7 @@ import { FILTER_GEOMETRY } from '../feature-search.module'
import { RecordsRepositoryInterface } from '@geonetwork-ui/common/domain/repository/records-repository.interface'
import { FavoritesService } from '@geonetwork-ui/api/repository/gn4'
import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface'
import { valid as validGeoJson } from 'geojson-validation'

@Injectable()
export class SearchEffects {
Expand Down Expand Up @@ -128,8 +129,24 @@ export class SearchEffects {
return of([state, favorites, null])
}
return this.filterGeometry$.pipe(
tap((geom) => {
try {
const trace = validGeoJson(geom, true) as string[]
if (trace?.length > 0) {
throw new Error(trace.join('\n'))
}
} catch (error) {
console.warn(
'Error while parsing the geometry filter\n',
error
)
throw new Error()
}
}),
fgravin marked this conversation as resolved.
Show resolved Hide resolved
map((geom) => [state, favorites, geom]),
catchError(() => of([state, favorites, null])) // silently opt out of spatial filter if an error happens
catchError((e) => {
return of([state, favorites, null])
})
)
}),
switchMap(
Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"duration-relativetimeformat": "^2.0.3",
"embla-carousel": "^8.0.0-rc14",
"express": "^4.17.1",
"geojson-validation": "^1.0.2",
"moment": "^2.29.4",
"ng-table-virtual-scroll": "^1.4.1",
"ngx-chips": "3.0.0",
Expand Down
Loading