Return 404 instead of defaulting to London if no location found

This commit is contained in:
Emil Lerch 2025-12-19 15:42:36 -08:00
parent 50f8889e6e
commit 62e7b83d1b
Signed by: lobo
GPG key ID: A7B62D657EF764F8

View file

@ -2,7 +2,6 @@ const std = @import("std");
const httpz = @import("httpz");
const WeatherProvider = @import("../weather/Provider.zig");
const Resolver = @import("../location/resolver.zig").Resolver;
const Location = @import("../location/resolver.zig").Location;
const QueryParams = @import("query.zig").QueryParams;
const ansi = @import("../render/ansi.zig");
const line = @import("../render/line.zig");
@ -87,27 +86,32 @@ fn handleWeatherInternal(
res: *httpz.Response,
location_query: []const u8,
) !void {
// Resolve location
const location = if (location_query.len == 0)
Location{ .name = "London", .coords = .{ .latitude = 51.5074, .longitude = -0.1278 } }
else
opts.resolver.resolve(location_query) catch |err| {
switch (err) {
error.LocationNotFound => {
res.status = 404;
res.body = "Location not found\n";
return;
},
else => {
res.status = 500;
res.body = "Internal server error\n";
return;
},
}
};
const req_alloc = req.arena;
// Resolve location. By the time we get here, we really
// should have a location from the path, query string, or
// client IP lookup. So if we have an empty location parameter, it
// is better to 404 than to fake it with a London response
if (location_query.len == 0) {
res.status = 404;
res.body = "Location not found\n";
return;
}
const location = opts.resolver.resolve(location_query) catch |err| {
switch (err) {
error.LocationNotFound => {
res.status = 404;
res.body = "Location not found\n";
return;
},
else => {
res.status = 500;
res.body = "Internal server error\n";
return;
},
}
};
// Fetch weather using coordinates
const weather = opts.provider.fetch(req_alloc, location.coords) catch |err| {
switch (err) {