From 62e7b83d1b915237defdc85662a01f4b05dcbeba Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Fri, 19 Dec 2025 15:42:36 -0800 Subject: [PATCH] Return 404 instead of defaulting to London if no location found --- src/http/handler.zig | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/http/handler.zig b/src/http/handler.zig index b1d7683..507d997 100644 --- a/src/http/handler.zig +++ b/src/http/handler.zig @@ -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) {