@@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
22import { HttpClient , HttpParams } from '@angular/common/http' ;
33import { Observable } from 'rxjs' ;
44import { environment } from '../../../environments/environment' ;
5- import { map , take } from 'rxjs/operators' ;
5+ import { catchError , map , take } from 'rxjs/operators' ;
66import { hasValue } from '../../shared/empty.util' ;
77
88export 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
2425const 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}
0 commit comments