Skip to content

Commit aaf5f15

Browse files
authored
update get_weather to use the new error make (nushell#261)
1 parent 7a59ee5 commit aaf5f15

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

weather/get-weather.nu

+8-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def locations [] {
66
[location city_column state_column country_column lat_column lon_column];
77
["http://ip-api.com/json/" city region countryCode lat lon]
88
["https://ipapi.co/json/" city region_code country_code latitude longitude]
9-
["https://freegeoip.app/json/" city region_code country_code latitude longitude]
9+
# ["https://freegeoip.app/json/" city region_code country_code latitude longitude] # doesn't appear to be free any longer
1010
["https://ipwhois.app/json/" city region country_code latitude longitude]
1111
]
1212
}
@@ -48,6 +48,11 @@ def get_location_by_ip [locIdx: int, token: string] {
4848
fetch $url
4949
}
5050

51+
def show-error [msg label err] { 07/02/2022 09:24:44 AM
52+
let span = (metadata $err).span;
53+
error make {msg: $msg, label: {text: $label, start: $span.start, end: $span.end } }
54+
}
55+
5156
def get_weather_by_ip [locIdx: int, units: string, token: string] {
5257
# units
5358
# f = imperial aka Fahrenheit
@@ -56,10 +61,7 @@ def get_weather_by_ip [locIdx: int, units: string, token: string] {
5661
let URL_FORECAST = "http://api.openweathermap.org/data/2.5/forecast/daily"
5762
let coords = (get_location_by_ip $locIdx $token)
5863
if ($coords | length) > 1 {
59-
[
60-
[msg, labels, span];
61-
["Error getting location", "There were more than one locations found", ([[start, end]; [0, 1]])]
62-
] | error make
64+
show-error "Error getting location" "There were more than one locations found" $coords
6365
}
6466

6567
if $units == "f" {
@@ -160,7 +162,7 @@ def get_icon_from_table [w] {
160162

161163
# Get the local weather by ip address
162164
export def get_weather [
163-
--locIdx(-l): int # The location id 0-3
165+
--locIdx(-l): int # The location id 0-2
164166
--units(-u): string # The units "f" or "c"
165167
] {
166168
let token = "85a4e3c55b73909f42c6a23ec35b7147"

0 commit comments

Comments
 (0)