Skip to content

Commit cfe592d

Browse files
[UXP-120] Error handling
1 parent 1609659 commit cfe592d

3 files changed

Lines changed: 30 additions & 13 deletions

File tree

src/app/core/services/location.service.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
22
import { HttpClient, HttpParams } from '@angular/common/http';
33
import { Observable } from 'rxjs';
44
import { environment } from '../../../environments/environment';
5-
import { map, take } from 'rxjs/operators';
5+
import { catchError, map, take } from 'rxjs/operators';
66
import { hasValue } from '../../shared/empty.util';
77

88
export interface LocationCoordinates {
@@ -19,6 +19,7 @@ export enum LocationErrorCodes {
1919
// define a `location.error.*` i18n label for each error code
2020
INVALID_COORDINATES = 'invalid-coordinates',
2121
LOCATION_NOT_FOUND = 'location-not-found',
22+
API_ERROR = 'api-error',
2223
}
2324

2425
const IS_COORDINATE_PAIR_REGEXP = /^\d+\.?\d*,\d+\.?\d*$/;
@@ -46,10 +47,14 @@ export class LocationService {
4647
let params = new HttpParams().append('q', address).append('format', NOMINATIM_RESPONSE_FORMAT);
4748

4849
return this.http.get<Record<string,any>[]>(this.searchEndpoint, { params: params }).pipe(
50+
catchError((err) => {
51+
console.error('Location service', err);
52+
throw Error(LocationErrorCodes.API_ERROR);
53+
}),
4954
take(1),
5055
map((searchResults) => {
5156
if (searchResults.length > 1) {
52-
console.warn(`Multiple locations found for address "${address}", showing top matches`, searchResults.slice(0,5));
57+
console.warn('Location service', `Multiple locations found for address "${address}"`, 'Showing top matches', searchResults.slice(0,5));
5358
}
5459
if (searchResults.length > 0) {
5560
const firstMatch = searchResults[0];
@@ -63,8 +68,8 @@ export class LocationService {
6368
};
6469
return info;
6570
} else {
66-
console.warn(`Location service: location "${address}" not found`);
67-
throw new Error(LocationErrorCodes.LOCATION_NOT_FOUND);
71+
console.warn('Location service', `Location "${address}" not found`);
72+
throw Error(LocationErrorCodes.LOCATION_NOT_FOUND);
6873
}
6974
}),
7075
);
@@ -82,10 +87,13 @@ export class LocationService {
8287
.append('format', NOMINATIM_RESPONSE_FORMAT);
8388

8489
return this.http.get<Record<string,any>>(this.reverseSearchEndpoint, { params: params }).pipe(
90+
catchError((err) => {
91+
throw Error(LocationErrorCodes.API_ERROR);
92+
}),
8593
take(1),
8694
map((searchResults) => {
8795
if (hasValue(searchResults.error)) {
88-
throw new Error(searchResults.error);
96+
throw Error(searchResults.error);
8997
} else {
9098
return searchResults.display_name;
9199
}
@@ -140,8 +148,8 @@ export class LocationService {
140148
const longitude = parseFloat(coordinateArr[1]);
141149
return { latitude, longitude };
142150
} else {
143-
console.warn(`Location service: invalid coordinates "${coordinates}"`);
144-
throw new Error(LocationErrorCodes.INVALID_COORDINATES);
151+
console.warn('Location service', `Invalid coordinates "${coordinates}"`);
152+
throw Error(LocationErrorCodes.INVALID_COORDINATES);
145153
}
146154
}
147155
}

src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/open-street-map/open-street-map-rendering.component.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import { TranslateService } from '@ngx-translate/core';
1515
import { map } from 'rxjs/operators';
1616
import { OpenStreetMapPointer } from '../../../../../../../shared/open-street-map/open-street-map.component';
1717

18-
const LocationErrorCodeEnum = LocationErrorCodes;
19-
2018
@Component({
2119
selector: 'ds-open-street-map-rendering',
2220
templateUrl: './open-street-map-rendering.component.html',
@@ -81,16 +79,21 @@ export class OpenStreetMapRenderingComponent extends RenderingTypeValueModelComp
8179
this.place.next(place);
8280
},
8381
error: (err) => {
82+
// show the map centered on provided coordinates despite the possibility to retrieve a description for the place
8483
const place: LocationPlace = {
8584
coordinates: coordinates,
8685
};
8786
this.place.next(place);
88-
console.warn(err.message);
87+
if (err.message === LocationErrorCodes.API_ERROR) {
88+
console.error(err.message);
89+
} else {
90+
console.warn(err.message);
91+
}
8992
},
9093
});
9194
} else {
9295
console.error(`Invalid coordinates: "${position}"`);
93-
this.invalidLocationErrorCode.next(LocationErrorCodeEnum.INVALID_COORDINATES);
96+
this.invalidLocationErrorCode.next(LocationErrorCodes.INVALID_COORDINATES);
9497
}
9598

9699
} else {
@@ -103,8 +106,12 @@ export class OpenStreetMapRenderingComponent extends RenderingTypeValueModelComp
103106
this.place.next(place);
104107
},
105108
error: (err) => {
106-
this.invalidLocationErrorCode.next(LocationErrorCodeEnum.LOCATION_NOT_FOUND);
107-
console.error(err.message);
109+
this.invalidLocationErrorCode.next(err.message); // either LOCATION_NOT_FOUND or API_ERROR
110+
if (err.message === LocationErrorCodes.API_ERROR) {
111+
console.error(err.message);
112+
} else {
113+
console.warn(err.message);
114+
}
108115
},
109116
});
110117
}

src/assets/i18n/en.json5

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,8 @@
870870

871871
"location.error.location-not-found": "Location not found",
872872

873+
"location.error.api-error": "The service that retrieves the position on the map is currently not available.",
874+
873875
"auth.errors.invalid-user": "Invalid email address or password.",
874876

875877
"auth.messages.expired": "Your session has expired. Please log in again.",

0 commit comments

Comments
 (0)