iacobson / ecspanse

Entity Component System (ECS) library for Elixir, offering a suite of features including: flexible queries with multiple filters, dynamic bidirectional relationships, versatile tagging capabilities, system event subscriptions, or asynchronous system execution.
https://hexdocs.pm/ecspanse
Apache License 2.0
63 stars 7 forks source link

Fetch commands don't have a nice error if the application isn't started #23

Closed Teifion closed 10 months ago

Teifion commented 11 months ago

Obviously if the use Ecspanse module isn't started as part of the supervision tree you get something like this:

iex(2)> Durandal.Entities.Player.fetch()
** (ArgumentError) argument error
    (stdlib 5.2) :ets.select(:ets_ecspanse_components_state, [{{{:"$1", :"$2"}, :"$3", :"$4"}, [], [{{:"$1", :"$2"}}]}])
    (ecspanse 0.8.0) lib/ecspanse/util.ex:63: Ecspanse.Util.__list_entities_components_memoize/0
    (memoize 1.4.2) lib/memoize/cache.ex:111: Memoize.Cache.do_get_or_run/3
    (ecspanse 0.8.0) lib/ecspanse/query.ex:1002: Ecspanse.Query.filter_for_entities/1
    (ecspanse 0.8.0) lib/ecspanse/query.ex:214: Ecspanse.Query.stream/1
    (ecspanse 0.8.0) lib/ecspanse/query.ex:237: Ecspanse.Query.one/1
    (durandal 0.1.0) lib/durandal/entities/player.ex:16: Durandal.Entities.Player.fetch/0
    iex:2: (file)

For people brand new to the project who maybe missed that line at the start a more informative error message would be a nice pointer.

For context, the fetch command is from the tutorial (swap Hero for Player)

  def fetch do
    Ecspanse.Query.select({Ecspanse.Entity}, with: [Components.Owner])
    |> Ecspanse.Query.one()
    |> case do
      {%Ecspanse.Entity{} = entity} -> {:ok, entity}
      _ -> {:error, :not_found}
    end
  end
iacobson commented 10 months ago

Thanks for the feedback. I will try to see if I can generically implement a meaningful error, for all query functions.