Return 404 instead of defaulting to London if no location found
This commit is contained in:
parent
50f8889e6e
commit
62e7b83d1b
1 changed files with 24 additions and 20 deletions
|
|
@ -2,7 +2,6 @@ const std = @import("std");
|
||||||
const httpz = @import("httpz");
|
const httpz = @import("httpz");
|
||||||
const WeatherProvider = @import("../weather/Provider.zig");
|
const WeatherProvider = @import("../weather/Provider.zig");
|
||||||
const Resolver = @import("../location/resolver.zig").Resolver;
|
const Resolver = @import("../location/resolver.zig").Resolver;
|
||||||
const Location = @import("../location/resolver.zig").Location;
|
|
||||||
const QueryParams = @import("query.zig").QueryParams;
|
const QueryParams = @import("query.zig").QueryParams;
|
||||||
const ansi = @import("../render/ansi.zig");
|
const ansi = @import("../render/ansi.zig");
|
||||||
const line = @import("../render/line.zig");
|
const line = @import("../render/line.zig");
|
||||||
|
|
@ -87,12 +86,18 @@ fn handleWeatherInternal(
|
||||||
res: *httpz.Response,
|
res: *httpz.Response,
|
||||||
location_query: []const u8,
|
location_query: []const u8,
|
||||||
) !void {
|
) !void {
|
||||||
|
const req_alloc = req.arena;
|
||||||
|
|
||||||
// Resolve location
|
// Resolve location. By the time we get here, we really
|
||||||
const location = if (location_query.len == 0)
|
// should have a location from the path, query string, or
|
||||||
Location{ .name = "London", .coords = .{ .latitude = 51.5074, .longitude = -0.1278 } }
|
// client IP lookup. So if we have an empty location parameter, it
|
||||||
else
|
// is better to 404 than to fake it with a London response
|
||||||
opts.resolver.resolve(location_query) catch |err| {
|
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) {
|
switch (err) {
|
||||||
error.LocationNotFound => {
|
error.LocationNotFound => {
|
||||||
res.status = 404;
|
res.status = 404;
|
||||||
|
|
@ -106,7 +111,6 @@ fn handleWeatherInternal(
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const req_alloc = req.arena;
|
|
||||||
|
|
||||||
// Fetch weather using coordinates
|
// Fetch weather using coordinates
|
||||||
const weather = opts.provider.fetch(req_alloc, location.coords) catch |err| {
|
const weather = opts.provider.fetch(req_alloc, location.coords) catch |err| {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue