From f83bae7c2224edbaddc46fbc5468abbfeacc70bc Mon Sep 17 00:00:00 2001 From: href Date: Sun, 27 Jan 2019 19:15:35 +0100 Subject: [PATCH] Views: wrap activity rendering in a rescue this avoids complete timeline breakage when an activity fucks up rendering. --- .../web/mastodon_api/views/status_view.ex | 2 +- .../web/twitter_api/views/activity_view.ex | 2 +- lib/pleroma/web/web.ex | 27 +++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index 7a384e941..0f2679444 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -49,7 +49,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do replied_to_activities = get_replied_to_activities(opts.activities) opts.activities - |> render_many( + |> safe_render_many( StatusView, "status.json", Map.put(opts, :replied_to_activities, replied_to_activities) diff --git a/lib/pleroma/web/twitter_api/views/activity_view.ex b/lib/pleroma/web/twitter_api/views/activity_view.ex index e0a52d94a..a01ee0010 100644 --- a/lib/pleroma/web/twitter_api/views/activity_view.ex +++ b/lib/pleroma/web/twitter_api/views/activity_view.ex @@ -114,7 +114,7 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do |> Map.put(:context_ids, context_ids) |> Map.put(:users, users) - render_many( + safe_render_many( opts.activities, ActivityView, "activity.json", diff --git a/lib/pleroma/web/web.ex b/lib/pleroma/web/web.ex index 74b13f929..30558e692 100644 --- a/lib/pleroma/web/web.ex +++ b/lib/pleroma/web/web.ex @@ -38,6 +38,33 @@ defmodule Pleroma.Web do import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1] import Pleroma.Web.{ErrorHelpers, Gettext, Router.Helpers} + + require Logger + + @doc "Same as `render/3` but wrapped in a rescue block" + def safe_render(view, template, assigns \\ %{}) do + Phoenix.View.render(view, template, assigns) + rescue + error -> + Logger.error( + "#{__MODULE__} failed to render #{inspect({view, template})}: #{inspect(error)}" + ) + + Logger.error(inspect(__STACKTRACE__)) + nil + end + + @doc """ + Same as `render_many/4` but wrapped in rescue block. + """ + def safe_render_many(collection, view, template, assigns \\ %{}) do + Enum.map(collection, fn resource -> + as = Map.get(assigns, :as) || view.__resource__ + assigns = Map.put(assigns, as, resource) + safe_render(view, template, assigns) + end) + |> Enum.filter(& &1) + end end end