@@ -120,54 +120,67 @@ export class OpenCageServer {
120120 // eslint-disable-next-line @typescript-eslint/no-explicit-any
121121 async handleReverseGeocode ( args ) {
122122 const { latitude, longitude, language = 'en' , no_annotations } = args ;
123- const params = new URLSearchParams ( {
124- q : `${ latitude } ,${ longitude } ` ,
125- key : OPENCAGE_API_KEY ,
126- language,
127- limit : '1' ,
128- } ) ;
129- if ( no_annotations ) {
130- params . append ( 'no_annotations' , '1' ) ;
131- }
132- const url = `${ OPENCAGE_API_URL } ?${ params } ` ;
133- const response = await fetch ( url ) ;
134- const data = await response . json ( ) ;
135- if ( data . status . code !== 200 ) {
136- throw new Error ( `OpenCage API error: ${ data . status . message } ` ) ;
123+ try {
124+ const params = new URLSearchParams ( {
125+ q : `${ latitude } ,${ longitude } ` ,
126+ key : OPENCAGE_API_KEY ,
127+ language,
128+ limit : '1' ,
129+ } ) ;
130+ if ( no_annotations ) {
131+ params . append ( 'no_annotations' , '1' ) ;
132+ }
133+ const url = `${ OPENCAGE_API_URL } ?${ params } ` ;
134+ const response = await fetch ( url , { headers : HEADERS } ) ;
135+ const data = await response . json ( ) ;
136+ if ( data . status . code !== 200 ) {
137+ throw new Error ( `OpenCage API error: ${ data . status . message } ` ) ;
138+ }
139+ if ( data . total_results === 0 ) {
140+ return {
141+ content : [
142+ {
143+ type : 'text' ,
144+ text : `No address found for coordinates ${ latitude } , ${ longitude } ` ,
145+ } ,
146+ ] ,
147+ } ;
148+ }
149+ const result = data . results [ 0 ] ;
150+ const info = {
151+ formatted : result . formatted ,
152+ components : result . components ,
153+ flag : result . annotations ?. flag ,
154+ timezone : result . annotations ?. timezone ,
155+ currency : result . annotations ?. currency ,
156+ ...( result . annotations &&
157+ ! no_annotations && { annotations : result . annotations } ) ,
158+ } ;
159+ return {
160+ content : [
161+ {
162+ type : 'text' ,
163+ text : `Reverse geocoding for coordinates ${ latitude } , ${ longitude } :\n\n` +
164+ `Address: ${ info . formatted } \n` +
165+ `Flag: ${ info . flag } \n` +
166+ `Timezone: ${ info . timezone . name } \n` +
167+ `Currency: ${ info . currency . name } (${ info . currency . iso_code } )\n` +
168+ `\n\nThe full API response:\n${ JSON . stringify ( data , null , 2 ) } ` ,
169+ } ,
170+ ] ,
171+ } ;
137172 }
138- if ( data . total_results === 0 ) {
173+ catch ( error ) {
139174 return {
140175 content : [
141176 {
142177 type : 'text' ,
143- text : `No address found for coordinates ${ latitude } , ${ longitude } ` ,
178+ text : `Error reverse geocoding coordinates ${ latitude } , ${ longitude } : ${ error instanceof Error ? error . message : 'Unknown error' } ` ,
144179 } ,
145180 ] ,
181+ isError : true ,
146182 } ;
147183 }
148- const result = data . results [ 0 ] ;
149- const info = {
150- formatted : result . formatted ,
151- components : result . components ,
152- flag : result . annotations ?. flag ,
153- timezone : result . annotations ?. timezone ,
154- currency : result . annotations ?. currency ,
155- ...( result . annotations &&
156- ! no_annotations && { annotations : result . annotations } ) ,
157- } ;
158- return {
159- content : [
160- {
161- type : 'text' ,
162- text : `Reverse geocoding for coordinates ${ latitude } , ${ longitude } :\n\n` +
163- `Address: ${ info . formatted } \n` +
164- `Flag: ${ info . flag } \n` +
165- `Timezone: ${ info . timezone . name } \n` +
166- `Currency: ${ info . currency . name } (${ info . currency . iso_code } )\n` +
167- `\n\nThe full API response:\n${ JSON . stringify ( data , null , 2 ) } ` ,
168- } ,
169- ] ,
170- } ;
171184 }
172185 /**
173186 * Handles the OpenCage API info request.
0 commit comments