Best way to handle LiveView upload "direct to S3" errors

Hi @soulpoint! You can use upload_errors/2 to get the error for a specific upload entry - here’s an example from the docs:

# You define this helper function in your code
def error_to_string(:too_large), do: "Too large"
def error_to_string(:too_many_files), do: "You have selected too many files"
def error_to_string(:not_accepted), do: "You have selected an unacceptable file type"

# Then you can use upload_errors/2 in your LV template
<%= for entry <- @uploads.avatar.entries do %>
  <%= for err <- upload_errors(@uploads.avatar, entry) do %>
    <div class="alert alert-danger">
      <%= error_to_string(err) %>
    </div>
  <% end %>
<% end %>

In your example, if you only want to show the Clear link on error, you could do something like this:

<%= for entry <- @uploads.file.entries do %>
  <% errors = upload_errors(@uploads.file, entry) %>
  <%= if errors != [] do %>
    <span phx-click="clear-failed-upload" phx-value-ref="<%= entry.ref %>">Clear</span>
  <% end %>
  <%= for err <- errors do %>
    <div class="alert alert-danger">
      <%= error_to_string(err) %>
    </div>
  <% end %>
<% end %>
3 Likes