From 920dceb1f8a54f6c6bbc21f294505d61aea26bcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Mon, 17 Jul 2023 17:53:04 +0200 Subject: [PATCH] Add forbidden error --- lib/kino/bridge.ex | 6 ++++-- lib/kino/fs.ex | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/kino/bridge.ex b/lib/kino/bridge.ex index 6c542530..ced289d2 100644 --- a/lib/kino/bridge.ex +++ b/lib/kino/bridge.ex @@ -79,7 +79,8 @@ defmodule Kino.Bridge do @doc """ Requests the file path for notebook file with the given name. """ - @spec get_file_entry_path(String.t()) :: {:ok, term()} | {:error, request_error() | String.t()} + @spec get_file_entry_path(String.t()) :: + {:ok, term()} | {:error, request_error() | :forbidden | String.t()} def get_file_entry_path(name) do with {:ok, reply} <- io_request({:livebook_get_file_entry_path, name}), do: reply end @@ -87,7 +88,8 @@ defmodule Kino.Bridge do @doc """ Requests the file spec for notebook file with the given name. """ - @spec get_file_entry_spec(String.t()) :: {:ok, term()} | {:error, request_error() | String.t()} + @spec get_file_entry_spec(String.t()) :: + {:ok, term()} | {:error, request_error() | :forbidden | String.t()} def get_file_entry_spec(name) do with {:ok, reply} <- io_request({:livebook_get_file_entry_spec, name}), do: reply end diff --git a/lib/kino/fs.ex b/lib/kino/fs.ex index 37eba702..3011e4ea 100644 --- a/lib/kino/fs.ex +++ b/lib/kino/fs.ex @@ -3,6 +3,19 @@ defmodule Kino.FS do Provides access to notebook files. """ + defmodule ForbiddenError do + @moduledoc """ + Exception raised when access to a notebook file is forbidden. + """ + + defexception [:name] + + @impl true + def message(exception) do + "forbidden access to file #{inspect(exception.name)}" + end + end + @doc """ Accesses notebook file with the given name and returns a local path to ead its contents from. @@ -24,6 +37,9 @@ defmodule Kino.FS do {:ok, path} -> path + {:error, :forbidden} -> + raise ForbiddenError, name: name + {:error, message} when is_binary(message) -> raise message @@ -57,6 +73,9 @@ defmodule Kino.FS do {:ok, spec} -> spec + {:error, :forbidden} -> + raise ForbiddenError, name: name + {:error, message} when is_binary(message) -> raise message