Skip to content

Commit d63923e

Browse files
Add logging
Signed-off-by: Luis Valdes <[email protected]>
1 parent d490264 commit d63923e

File tree

1 file changed

+48
-19
lines changed

1 file changed

+48
-19
lines changed

typescript-sdk/integrations/mastra/example/src/mastra/tools/weather-tool.ts

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,59 @@ export const weatherTool = createTool({
4141
});
4242

4343
const getWeather = async (location: string) => {
44-
const geocodingUrl = `https://geocoding-api.open-meteo.com/v1/search?name=${encodeURIComponent(location)}&count=1`;
45-
const geocodingResponse = await fetch(geocodingUrl);
46-
const geocodingData = (await geocodingResponse.json()) as GeocodingResponse;
44+
try {
45+
const geocodingUrl = `https://geocoding-api.open-meteo.com/v1/search?name=${encodeURIComponent(location)}&count=1&language=en&format=json`;
46+
const geocodingResponse = await fetch(geocodingUrl);
4747

48-
if (!geocodingData.results?.[0]) {
49-
throw new Error(`Location '${location}' not found`);
50-
}
48+
if (!geocodingResponse.ok) {
49+
throw new Error(`Geocoding API failed with status: ${geocodingResponse.status}`);
50+
}
5151

52-
const { latitude, longitude, name } = geocodingData.results[0];
52+
const geocodingData = (await geocodingResponse.json()) as GeocodingResponse;
5353

54-
const weatherUrl = `https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&current=temperature_2m,apparent_temperature,relative_humidity_2m,wind_speed_10m,wind_gusts_10m,weather_code`;
54+
if (!geocodingData.results?.[0]) {
55+
throw new Error(`Location '${location}' not found`);
56+
}
5557

56-
const response = await fetch(weatherUrl);
57-
const data = (await response.json()) as WeatherResponse;
58+
const { latitude, longitude, name } = geocodingData.results[0];
5859

59-
return {
60-
temperature: data.current.temperature_2m,
61-
feelsLike: data.current.apparent_temperature,
62-
humidity: data.current.relative_humidity_2m,
63-
windSpeed: data.current.wind_speed_10m,
64-
windGust: data.current.wind_gusts_10m,
65-
conditions: getWeatherCondition(data.current.weather_code),
66-
location: name,
67-
};
60+
const weatherUrl = `https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&current=temperature_2m,apparent_temperature,relative_humidity_2m,wind_speed_10m,wind_gusts_10m,weather_code&timezone=auto`;
61+
62+
const response = await fetch(weatherUrl);
63+
64+
if (!response.ok) {
65+
throw new Error(`Weather API failed with status: ${response.status}`);
66+
}
67+
68+
const data = (await response.json()) as WeatherResponse;
69+
70+
// Add validation to check if the response has the expected structure
71+
if (!data || !data.current) {
72+
console.error('Invalid weather API response:', JSON.stringify(data, null, 2));
73+
throw new Error(`Invalid weather data received for location '${location}'`);
74+
}
75+
76+
const current = data.current;
77+
78+
// Validate that all required fields are present
79+
if (current.temperature_2m === undefined || current.temperature_2m === null) {
80+
console.error('Missing temperature data in response:', JSON.stringify(current, null, 2));
81+
throw new Error(`Temperature data not available for location '${location}'`);
82+
}
83+
84+
return {
85+
temperature: current.temperature_2m,
86+
feelsLike: current.apparent_temperature ?? current.temperature_2m,
87+
humidity: current.relative_humidity_2m ?? 0,
88+
windSpeed: current.wind_speed_10m ?? 0,
89+
windGust: current.wind_gusts_10m ?? 0,
90+
conditions: getWeatherCondition(current.weather_code ?? 0),
91+
location: name,
92+
};
93+
} catch (error) {
94+
console.error(`Weather tool error for location '${location}':`, error);
95+
throw new Error(`Failed to get weather data for '${location}': ${error instanceof Error ? error.message : 'Unknown error'}`);
96+
}
6897
};
6998

7099
function getWeatherCondition(code: number): string {

0 commit comments

Comments
 (0)