From 81eb2b73dfa4699d65221e68d6bc300cbc04c387 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Tue, 18 Jan 2022 10:03:50 -0600 Subject: [PATCH] Notify Sentry when location code is not found --- .../controllers/api/stats_controller.ex | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/lib/plausible_web/controllers/api/stats_controller.ex b/lib/plausible_web/controllers/api/stats_controller.ex index 5eeb7b4da..b029c51db 100644 --- a/lib/plausible_web/controllers/api/stats_controller.ex +++ b/lib/plausible_web/controllers/api/stats_controller.ex @@ -568,12 +568,22 @@ defmodule PlausibleWeb.Api.StatsController do Enum.map(countries, fn row -> country = Location.get_country(row["code"]) - Map.merge(row, %{ - "name" => country.name, - "flag" => country.flag, - "alpha_3" => country.alpha_3, - "code" => country.alpha_2 - }) + if country do + Map.merge(row, %{ + "name" => country.name, + "flag" => country.flag, + "alpha_3" => country.alpha_3, + "code" => country.alpha_2 + }) + else + Sentry.capture_message("Could not find country info", extra: %{code: row["code"]}) + + Map.merge(row, %{ + "name" => row["code"], + "alpha_3" => row["code"], + "code" => row["code"] + }) + end end) json(conn, countries) @@ -590,8 +600,14 @@ defmodule PlausibleWeb.Api.StatsController do |> transform_keys(%{"region" => "code"}) |> Enum.map(fn region -> region_entry = Location.get_subdivision(region["code"]) - country_entry = Location.get_country(region_entry.country_code) - Map.merge(region, %{"name" => region_entry.name, "country_flag" => country_entry.flag}) + + if region do + country_entry = Location.get_country(region_entry.country_code) + Map.merge(region, %{"name" => region_entry.name, "country_flag" => country_entry.flag}) + else + Sentry.capture_message("Could not find region info", extra: %{code: region["code"]}) + Map.merge(region, %{"name" => region["code"]}) + end end) if params["csv"] do @@ -626,6 +642,8 @@ defmodule PlausibleWeb.Api.StatsController do "country_flag" => country_info.flag }) else + Sentry.capture_message("Could not find city info", extra: %{code: city["code"]}) + Map.merge(city, %{"name" => "N/A"}) end end)