فهرست منبع

Handle missing location store file

theenglishway (time) 4 سال پیش
والد
کامیت
a242072aad
1فایلهای تغییر یافته به همراه11 افزوده شده و 2 حذف شده
  1. 11 2
      lib/vaccins/location_store.ex

+ 11 - 2
lib/vaccins/location_store.ex

@@ -79,6 +79,7 @@ defmodule Vaccins.LocationStore do
 
   use GenServer
   require Ex2ms
+  require Logger
   import Ecto.Changeset
   alias Vaccins.Queries.Doctolib
   @name Vaccins.LocationStore
@@ -148,14 +149,22 @@ defmodule Vaccins.LocationStore do
          do: {:reply, :ok, new_state}
   end
 
-  defp load_state(), do: %{locations: read_file() |> Map.new(&{&1.id, &1})}
+  defp load_state() do
+    with {:ok, locations} <- read_file() do
+      %{locations: locations |> Map.new(&{&1.id, &1})}
+    else
+      {:error, reason} ->
+        Logger.error("Could not read file #{@file_path} : #{reason}")
+        %{locations: %{}}
+    end
+  end
 
   defp read_file(),
     do:
       with(
         {:ok, content} <- File.read(@file_path),
         {:ok, decoded} <- Jason.decode(content, keys: :atoms),
-        do: decoded |> Enum.map(&Location.from_json/1)
+        do: {:ok, decoded |> Enum.map(&Location.from_json/1)}
       )
 
   defp dump_state(%{locations: locations}), do: locations |> Map.values() |> write_file()