Skip to content

Commit

Permalink
Merge pull request #700 from geonetwork/empty-geometry
Browse files Browse the repository at this point in the history
Datahub: handle filter geometry error
  • Loading branch information
fgravin authored Nov 29, 2023
2 parents 998c2e2 + 69c9d3f commit 3351a2d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
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()
}
}),
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

0 comments on commit 3351a2d

Please sign in to comment.