From c56274285da8f4b2ea4da9be4b5a104d405201f4 Mon Sep 17 00:00:00 2001 From: Yusuke Tanaka Date: Tue, 17 Dec 2024 11:45:18 +0900 Subject: [PATCH] feat(permission): separate PermissionDeniedError to Retryable and Fatal (#27282) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit separates `PermissionDeniedError` into two kinds; `Retryable` and `Fatal`. The existing `PermissionDeniedError`s fall into `Retryable`, since permission errors can be resolved by retrying with proper permissions in Deno CLI. The motivation of adding `Fatal` is that in some environments some operations are just disabled; for instance, in Deno Deploy, any write operations to filesystem can never be granted, in which case `Fatal` kind becomes useful. Co-authored-by: Bartek IwaƄczuk --- runtime/permissions/lib.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/runtime/permissions/lib.rs b/runtime/permissions/lib.rs index a1a217738d..bbd0301db4 100644 --- a/runtime/permissions/lib.rs +++ b/runtime/permissions/lib.rs @@ -39,10 +39,11 @@ pub use prompter::PromptCallback; pub use prompter::PromptResponse; #[derive(Debug, thiserror::Error)] -#[error("Requires {access}, {}", format_permission_error(.name))] -pub struct PermissionDeniedError { - pub access: String, - pub name: &'static str, +pub enum PermissionDeniedError { + #[error("Requires {access}, {}", format_permission_error(.name))] + Retryable { access: String, name: &'static str }, + #[error("Requires {access}, which cannot be granted in this environment")] + Fatal { access: String }, } fn format_permission_error(name: &'static str) -> String { @@ -144,11 +145,11 @@ impl PermissionState { ) } - fn error( + fn retryable_error( name: &'static str, info: impl FnOnce() -> Option, ) -> PermissionDeniedError { - PermissionDeniedError { + PermissionDeniedError::Retryable { access: Self::fmt_access(name, info), name, } @@ -201,10 +202,12 @@ impl PermissionState { Self::log_perm_access(name, info); (Ok(()), true, true) } - PromptResponse::Deny => (Err(Self::error(name, info)), true, false), + PromptResponse::Deny => { + (Err(Self::retryable_error(name, info)), true, false) + } } } - _ => (Err(Self::error(name, info)), false, false), + _ => (Err(Self::retryable_error(name, info)), false, false), } } }