From b2ce588a1c6c6de0d910dfd2114d499ba92d7b7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Wed, 23 Aug 2023 22:39:36 +0200 Subject: [PATCH] Rename error output :known_reason to :context --- lib/livebook/runtime.ex | 6 +++--- lib/livebook/runtime/evaluator/formatter.ex | 14 +++++++------- lib/livebook/session.ex | 8 +++++++- lib/livebook_web/live/app_session_live.ex | 2 +- lib/livebook_web/live/output.ex | 8 ++++---- test/livebook/runtime/evaluator_test.exs | 2 +- 6 files changed, 23 insertions(+), 17 deletions(-) diff --git a/lib/livebook/runtime.ex b/lib/livebook/runtime.ex index efe21686e..87d21338d 100644 --- a/lib/livebook/runtime.ex +++ b/lib/livebook/runtime.ex @@ -440,17 +440,17 @@ defprotocol Livebook.Runtime do @typedoc """ Error content, usually representing evaluation failure. - The `:known_reason` field is used by Livebook to suggest a way to fix + The `:context` field is used by Livebook to suggest a way to fix the error. """ @type error_output :: %{ type: :error, message: String.t(), - known_reason: + context: {:missing_secret, name :: String.t()} | {:interrupt, variant :: :normal | :error, message :: String.t()} | {:file_entry_forbidden, name :: String.t()} - | :other + | nil } @typedoc """ diff --git a/lib/livebook/runtime/evaluator/formatter.ex b/lib/livebook/runtime/evaluator/formatter.ex index cc8428b5d..3c39afd4c 100644 --- a/lib/livebook/runtime/evaluator/formatter.ex +++ b/lib/livebook/runtime/evaluator/formatter.ex @@ -40,13 +40,13 @@ defmodule Livebook.Runtime.Evaluator.Formatter do |> error_color |> :erlang.list_to_binary() - %{type: :error, message: formatted, known_reason: error_known_reason(error)} + %{type: :error, message: formatted, context: error_context(error)} end end def format_result({:error, kind, error, stacktrace}, _language) do formatted = format_error(kind, error, stacktrace) - %{type: :error, message: formatted, known_reason: error_known_reason(error)} + %{type: :error, message: formatted, context: error_context(error)} end def format_result({:ok, value}, :erlang) do @@ -80,7 +80,7 @@ defmodule Livebook.Runtime.Evaluator.Formatter do catch kind, error -> formatted = format_error(kind, error, __STACKTRACE__) - %{type: :error, message: formatted, known_reason: error_known_reason(error)} + %{type: :error, message: formatted, context: error_context(error)} end end @@ -159,16 +159,16 @@ defmodule Livebook.Runtime.Evaluator.Formatter do IO.ANSI.format([:red, string], true) end - defp error_known_reason(%System.EnvError{env: "LB_" <> secret_name}), + defp error_context(%System.EnvError{env: "LB_" <> secret_name}), do: {:missing_secret, secret_name} - defp error_known_reason(error) when is_struct(error, Kino.InterruptError), + defp error_context(error) when is_struct(error, Kino.InterruptError), do: {:interrupt, error.variant, error.message} - defp error_known_reason(error) when is_struct(error, Kino.FS.ForbiddenError), + defp error_context(error) when is_struct(error, Kino.FS.ForbiddenError), do: {:file_entry_forbidden, error.name} - defp error_known_reason(_), do: :other + defp error_context(_), do: nil defp erlang_to_output(value) do text = :io_lib.format("~p", [value]) |> IO.iodata_to_binary() diff --git a/lib/livebook/session.ex b/lib/livebook/session.ex index 17bad632c..af1c1250f 100644 --- a/lib/livebook/session.ex +++ b/lib/livebook/session.ex @@ -2970,7 +2970,13 @@ defmodule Livebook.Session do end defp normalize_runtime_output({:error, message, type}) do - %{type: :error, message: message, known_reason: type} + context = + case type do + :other -> nil + type -> type + end + + %{type: :error, message: message, context: context} end defp normalize_runtime_output(other) do diff --git a/lib/livebook_web/live/app_session_live.ex b/lib/livebook_web/live/app_session_live.ex index 5d8858475..65007b1ce 100644 --- a/lib/livebook_web/live/app_session_live.ex +++ b/lib/livebook_web/live/app_session_live.ex @@ -490,7 +490,7 @@ defmodule LivebookWeb.AppSessionLive do {idx, %{output | outputs: outputs}} end - defp filter_output({idx, %{type: :error, known_reason: {:interrupt, _, _}} = output}), + defp filter_output({idx, %{type: :error, context: {:interrupt, _, _}} = output}), do: {idx, output} defp filter_output(_output), do: nil diff --git a/lib/livebook_web/live/output.ex b/lib/livebook_web/live/output.ex index 170b41855..8daa1fb48 100644 --- a/lib/livebook_web/live/output.ex +++ b/lib/livebook_web/live/output.ex @@ -38,7 +38,7 @@ defmodule LivebookWeb.Output do end defp border?(%{type: type}) when type in [:terminal_text, :plain_text], do: true - defp border?(%{type: :error, known_reason: {:interrupt, _, _}}), do: false + defp border?(%{type: :error, context: {:interrupt, _, _}}), do: false defp border?(%{type: :error}), do: true defp border?(%{type: :grid, boxed: boxed}), do: boxed defp border?(_output), do: false @@ -253,7 +253,7 @@ defmodule LivebookWeb.Output do end defp render_output( - %{type: :error, known_reason: {:missing_secret, secret_name}} = output, + %{type: :error, context: {:missing_secret, secret_name}} = output, %{session_id: session_id} ) do assigns = %{message: output.message, secret_name: secret_name, session_id: session_id} @@ -281,7 +281,7 @@ defmodule LivebookWeb.Output do end defp render_output( - %{type: :error, known_reason: {:file_entry_forbidden, file_entry_name}} = output, + %{type: :error, context: {:file_entry_forbidden, file_entry_name}} = output, %{session_id: session_id} ) do assigns = %{message: output.message, file_entry_name: file_entry_name, session_id: session_id} @@ -309,7 +309,7 @@ defmodule LivebookWeb.Output do end defp render_output( - %{type: :error, known_reason: {:interrupt, variant, message}}, + %{type: :error, context: {:interrupt, variant, message}}, %{cell_id: cell_id} ) do assigns = %{variant: variant, message: message, cell_id: cell_id} diff --git a/test/livebook/runtime/evaluator_test.exs b/test/livebook/runtime/evaluator_test.exs index acbf9fe6b..71e2cc272 100644 --- a/test/livebook/runtime/evaluator_test.exs +++ b/test/livebook/runtime/evaluator_test.exs @@ -47,7 +47,7 @@ defmodule Livebook.Runtime.EvaluatorTest do defmacrop error(message) do quote do - %{type: :error, message: unquote(message), known_reason: :other} + %{type: :error, message: unquote(message), context: nil} end end