astarte-platform / astarte

Core Astarte Repository
https://docs.astarte-platform.org/
Apache License 2.0
240 stars 50 forks source link

AppEngine: is returning 500 error for queries with no matching data #552

Closed davidebriani closed 2 years ago

davidebriani commented 3 years ago

When querying on an interface for device data, since to and sinceAfter params can be specified to filter data. When such params are specified and no data is matched:

Below is a log of an example query with since param on a datastream individual interface and no matching data:

Mar 16 10:41:13 |ERRO| #PID<0.27372.113> running Astarte.AppEngine.APIWeb.Endpoint (connection #PID<0.26978.113>, stream id 5) terminated
Server: api.eu1.astarte.cloud:80 (http)
Request: GET /v1/davidebriani/devices/CUPVU9qTQOeRPjfkHMvPYA/interfaces/datastream.individual?keep_milliseconds=true&since=2020-10-14T12:27:13.200Z&since_after=&to=&limit=
** (exit) an exception was raised:
    ** (MatchError) no match of right hand side value: :empty_dataset
        (astarte_appengine_api 1.0.0-beta.2) lib/astarte_appengine_api/device/device.ex:1078: anonymous fn/8 in Astarte.AppEngine.API.Device.retrieve_endpoint_values/9
        (elixir 1.11.3) lib/enum.ex:3473: anonymous fn/3 in Enum.reduce/3
        (cqex 1.0.1) lib/cqex/result.ex:242: Enumerable.CQEx.Result.reduce/3
        (elixir 1.11.3) lib/enum.ex:3473: Enum.reduce/3
        (astarte_appengine_api 1.0.0-beta.2) lib/astarte_appengine_api/device/device.ex:844: anonymous fn/6 in Astarte.AppEngine.API.Device.do_get_interface_values!/5
        (elixir 1.11.3) lib/enum.ex:3473: anonymous fn/3 in Enum.reduce/3
        (cqex 1.0.1) lib/cqex/result.ex:242: Enumerable.CQEx.Result.reduce/3
        (elixir 1.11.3) lib/enum.ex:3473: Enum.reduce/3 

Below is a log of an example query with since param on a datastream object interface and no matching data:

Mar 16 10:46:55 |ERRO| #PID<0.4114.114> running Astarte.AppEngine.APIWeb.Endpoint (connection #PID<0.3417.114>, stream id 7) terminated
Server: api.eu1.astarte.cloud:80 (http)
Request: GET /v1/davidebriani/devices/hdjwy4KfQhCQn4NScydlIA/interfaces/com.sample.GPS?keep_milliseconds=true&since=&since_after=&to=2020-11-06T11:40:54.588Z&limit=
** (exit) an exception was raised:
    ** (FunctionClauseError) no function clause matching in Astarte.AppEngine.APIWeb.FallbackController.call/2
        (astarte_appengine_api 1.0.0-beta.2) lib/astarte_appengine_api_web/controllers/fallback_controller.ex:28: Astarte.AppEngine.APIWeb.FallbackController.call(%Plug.Conn{adapter: {Plug.Cowboy.Conn, :...}, assigns: %{}, before_send: [#Function<0.11227428/1 in Plug.Telemetry.call/2>, #Function<1.34346314/1 in PlugLoggerWithMeta.call/2>], body_params: %{}, cookies: %Plug.Conn.Unfetched{aspect: :cookies}, halted: false, host: "api.eu1.astarte.cloud", method: "GET", owner: #PID<0.4114.114>, params: %{"device_id" => "hdjwy4KfQhCQn4NScydlIA", "interface" => "com.sample.GPS", "keep_milliseconds" => "true", "limit" => "", "realm_name" => "davidebriani", "since" => "", "since_after" => "", "to" => "2020-11-06T11:40:54.588Z"}, path_info: ["v1", "davidebriani", "devices", "hdjwy4KfQhCQn4NScydlIA", "interfaces", "com.sample.GPS"], path_params: %{"device_id" => "hdjwy4KfQhCQn4NScydlIA", "interface" => "com.sample.GPS", "realm_name" => "davidebriani"}, port: 80, private: %{Astarte.AppEngine.APIWeb.Router => {[], %{PhoenixSwagger.Plug.SwaggerUI => []}}, :guardian_default_claims => %{"a_aea" => [".*::.*"], "a_ch" => ["WATCH::.*", "JOIN::.*"], "a_f" => [".*::.*"], "a_pa" => [".*::.*"], "a_rma" => [".*::.*"], "exp" => 1615975528, "iat" => 1615889128, "iss" => "Astarte Cloud"}, :guardian_default_resource => %Astarte.AppEngine.API.Auth.User{authorizations: [".*::.*"], id: nil}, :guardian_default_token => "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJhX2FlYSI6WyIuKjo6LioiXSwiYV9jaCI6WyJXQVRDSDo6LioiLCJKT0lOOjouKiJdLCJhX2YiOlsiLio6Oi4qIl0sImFfcGEiOlsiLio6Oi4qIl0sImFfcm1hIjpbIi4qOjouKiJdLCJleHAiOjE2MTU5NzU1MjgsImlhdCI6MTYxNTg4OTEyOCwiaXNzIjoiQXN0YXJ0ZSBDbG91ZCJ9.LuiHysfMklUiLajPA_jtV6IhpApI3LWUspZF7XQsZ94IH6fm6Y6QcYv28Okh7lkXALF-A7UvigvGswEgt73FFw", :guardian_error_handler => Astarte.AppEngine.APIWeb.FallbackController, :guardian_module => Astarte.AppEngine.APIWeb.AuthGuardian, :phoenix_action => :show, :phoenix_controller => Astarte.AppEngine.APIWeb.InterfaceValuesController, :phoenix_endpoint => Astarte.AppEngine.APIWeb.Endpoint, :phoenix_format => "json", :phoenix_layout => {Astarte.AppEngine.APIWeb.LayoutView, :app}, :phoenix_router => Astarte.AppEngine.APIWeb.Router, :phoenix_view => Astarte.AppEngine.APIWeb.InterfaceValuesView, :plug_session_fetch => #Function<1.123471702/1 in Plug.Session.fetch_session/1>}, query_params: %{"keep_milliseconds" => "true", "limit" => "", "since" => "", "since_after" => "", "to" => "2020-11-06T11:40:54.588Z"}, query_string: "keep_milliseconds=true&since=&since_after=&to=2020-11-06T11:40:54.588Z&limit=", remote_ip: {10, 36, 0, 2}, req_cookies: %Plug.Conn.Unfetched{aspect: :cookies}, req_headers: [{"accept", "application/json, text/plain, */*"}, {"accept-encoding", "gzip, deflate, br"}, {"accept-language", "en-US,en;q=0.5"}, {"authorization", "Bearer eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJhX2FlYSI6WyIuKjo6LioiXSwiYV9jaCI6WyJXQVRDSDo6LioiLCJKT0lOOjouKiJdLCJhX2YiOlsiLio6Oi4qIl0sImFfcGEiOlsiLio6Oi4qIl0sImFfcm1hIjpbIi4qOjouKiJdLCJleHAiOjE2MTU5NzU1MjgsImlhdCI6MTYxNTg4OTEyOCwiaXNzIjoiQXN0YXJ0ZSBDbG91ZCJ9.LuiHysfMklUiLajPA_jtV6IhpApI3LWUspZF7XQsZ94IH6fm6Y6QcYv28Okh7lkXALF-A7UvigvGswEgt73FFw"}, {"host", "api.eu1.astarte.cloud"}, {"origin", "http://localhost:8080"}, {"referer", "http://localhost:8080/"}, {"user-agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:86.0) Gecko/20100101 Firefox/86.0"}, {"x-forwarded-for", "37.179.88.191"}], request_path: "/v1/davidebriani/devices/hdjwy4KfQhCQn4NScydlIA/interfaces/com.sample.GPS", resp_body: nil, resp_cookies: %{}, resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}, {"x-request-id", "FmzM3Mr5Qud6L0wBTI1h"}, {"access-control-allow-origin", "*"}, {"access-control-expose-headers", ""}, {"access-control-allow-credentials", "true"}], scheme: :http, script_name: [], secret_key_base: :..., state: :unset, status: nil}, {:ok, %{}})
        (astarte_appengine_api 1.0.0-beta.2) lib/astarte_appengine_api_web/controllers/interface_values_controller.ex:18: Astarte.AppEngine.APIWeb.InterfaceValuesController.action/2
        (astarte_appengine_api 1.0.0-beta.2) lib/astarte_appengine_api_web/controllers/interface_values_controller.ex:18: Astarte.AppEngine.APIWeb.InterfaceValuesController.phoenix_controller_pipeline/2
        (phoenix 1.5.8) lib/phoenix/router.ex:352: Phoenix.Router.__call__/2
        (astarte_appengine_api 1.0.0-beta.2) lib/astarte_appengine_api_web/endpoint.ex:18: Astarte.AppEngine.APIWeb.Endpoint.plug_builder_call/2
        (astarte_appengine_api 1.0.0-beta.2) lib/astarte_appengine_api_web/endpoint.ex:18: Astarte.AppEngine.APIWeb.Endpoint.call/2
        (phoenix 1.5.8) lib/phoenix/endpoint/cowboy2_handler.ex:65: Phoenix.Endpoint.Cowboy2Handler.init/4
        (cowboy 2.8.0) /app/deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
Annopaolo commented 2 years ago

Closed by #614