Conslidate log messages for object fetcher failures and leverage Logger.metadata

This commit is contained in:
Mark Felder 2023-12-27 20:47:18 -05:00 committed by Floatingghost
parent 825ae46bfa
commit 3c54f407c5
2 changed files with 60 additions and 19 deletions

View file

@ -151,26 +151,28 @@ def fetch_object_from_id(id, options \\ []) do
{:object, data, Object.normalize(activity, fetch: false)} do {:object, data, Object.normalize(activity, fetch: false)} do
{:ok, object} {:ok, object}
else else
{:allowed_depth, false} -> {:allowed_depth, false} = e ->
log_fetch_error(id, e)
{:error, "Max thread distance exceeded."} {:error, "Max thread distance exceeded."}
{:scheme, false} -> {:scheme, false} ->
{:error, "URI Scheme Invalid"} {:error, "URI Scheme Invalid"}
{:containment, e} -> {:containment, reason} = e ->
Logger.error("Error while fetching #{id}: Object containment failed. #{inspect(e)}") log_fetch_error(id, e)
{:error, e} {:error, reason}
{:transmogrifier, {:error, {:reject, e}}} -> {:transmogrifier, {:error, {:reject, reason}}} = e ->
Logger.error("Rejected #{id} while fetching: #{inspect(e)}") log_fetch_error(id, e)
{:reject, e} {:reject, reason}
{:transmogrifier, {:reject, e}} -> {:transmogrifier, {:reject, reason}} = e ->
Logger.error("Rejected #{id} while fetching: #{inspect(e)}") log_fetch_error(id, e)
{:reject, e} {:reject, reason}
{:transmogrifier, _} = e -> {:transmogrifier, reason} = e ->
{:error, e} log_fetch_error(id, e)
{:error, reason}
{:object, data, nil} -> {:object, data, nil} ->
reinject_object(%Object{}, data) reinject_object(%Object{}, data)
@ -181,19 +183,24 @@ def fetch_object_from_id(id, options \\ []) do
{:fetch_object, %Object{} = object} -> {:fetch_object, %Object{} = object} ->
{:ok, object} {:ok, object}
{:fetch, {:error, error}} -> {:fetch, {:error, reason}} = e ->
Logger.error("Error while fetching #{id}: #{inspect(error)}") log_fetch_error(id, e)
{:error, error} {:error, reason}
{:reject, reason} -> {:reject, reason} ->
{:reject, reason} {:reject, reason}
e -> e ->
Logger.error("Error while fetching #{id}: #{inspect(e)}") log_fetch_error(id, e)
{:error, e} {:error, e}
end end
end end
defp log_fetch_error(id, error) do
Logger.metadata([object: id])
Logger.error("Object rejected while fetching #{id} #{inspect(error)}")
end
defp prepare_activity_params(data) do defp prepare_activity_params(data) do
%{ %{
"type" => "Create", "type" => "Create",

View file

@ -124,6 +124,40 @@ test "it fixes both the Create and object contexts in a reply" do
assert activity.data["context"] == object.data["context"] assert activity.data["context"] == object.data["context"]
end end
test "it keeps link tags" do
insert(:user, ap_id: "https://example.org/users/alice")
message = File.read!("test/fixtures/fep-e232.json") |> Jason.decode!()
assert capture_log(fn ->
assert {:ok, activity} = Transmogrifier.handle_incoming(message)
object = Object.normalize(activity)
assert [%{"type" => "Mention"}, %{"type" => "Link"}] = object.data["tag"]
end) =~ "Object rejected while fetching"
end
test "it accepts quote posts" do
insert(:user, ap_id: "https://misskey.io/users/7rkrarq81i")
object = File.read!("test/fixtures/quote_post/misskey_quote_post.json") |> Jason.decode!()
message = %{
"@context" => "https://www.w3.org/ns/activitystreams",
"type" => "Create",
"actor" => "https://misskey.io/users/7rkrarq81i",
"object" => object
}
assert {:ok, activity} = Transmogrifier.handle_incoming(message)
# Object was created in the database
object = Object.normalize(activity)
assert object.data["quoteUrl"] == "https://misskey.io/notes/8vs6wxufd0"
# It fetched the quoted post
assert Object.normalize("https://misskey.io/notes/8vs6wxufd0")
end
end end
describe "prepare outgoing" do describe "prepare outgoing" do
@ -413,7 +447,7 @@ test "it rejects activities which reference objects with bogus origins" do
assert capture_log(fn -> assert capture_log(fn ->
{:error, _} = Transmogrifier.handle_incoming(data) {:error, _} = Transmogrifier.handle_incoming(data)
end) =~ "Object containment failed" end) =~ "Object rejected while fetching"
end end
test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do test "it rejects activities which reference objects that have an incorrect attribution (variant 1)" do
@ -428,7 +462,7 @@ test "it rejects activities which reference objects that have an incorrect attri
assert capture_log(fn -> assert capture_log(fn ->
{:error, _} = Transmogrifier.handle_incoming(data) {:error, _} = Transmogrifier.handle_incoming(data)
end) =~ "Object containment failed" end) =~ "Object rejected while fetching"
end end
test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do test "it rejects activities which reference objects that have an incorrect attribution (variant 2)" do
@ -443,7 +477,7 @@ test "it rejects activities which reference objects that have an incorrect attri
assert capture_log(fn -> assert capture_log(fn ->
{:error, _} = Transmogrifier.handle_incoming(data) {:error, _} = Transmogrifier.handle_incoming(data)
end) =~ "Object containment failed" end) =~ "Object rejected while fetching"
end end
end end