Rename error output :known_reason to :context

This commit is contained in:
Jonatan Kłosko 2023-08-23 22:39:36 +02:00
parent da9cc6643b
commit b2ce588a1c
6 changed files with 23 additions and 17 deletions

View file

@ -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 """

View file

@ -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()

View file

@ -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

View file

@ -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

View file

@ -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}

View file

@ -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