1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-18 05:14:21 -05:00

feat(permission): separate PermissionDeniedError to Retryable and Fatal (#27282)

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 <biwanczuk@gmail.com>
This commit is contained in:
Yusuke Tanaka 2024-12-17 11:45:18 +09:00 committed by GitHub
parent f9add94e17
commit c56274285d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -39,10 +39,11 @@ pub use prompter::PromptCallback;
pub use prompter::PromptResponse; pub use prompter::PromptResponse;
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
#[error("Requires {access}, {}", format_permission_error(.name))] pub enum PermissionDeniedError {
pub struct PermissionDeniedError { #[error("Requires {access}, {}", format_permission_error(.name))]
pub access: String, Retryable { access: String, name: &'static str },
pub name: &'static str, #[error("Requires {access}, which cannot be granted in this environment")]
Fatal { access: String },
} }
fn format_permission_error(name: &'static str) -> String { fn format_permission_error(name: &'static str) -> String {
@ -144,11 +145,11 @@ impl PermissionState {
) )
} }
fn error( fn retryable_error(
name: &'static str, name: &'static str,
info: impl FnOnce() -> Option<String>, info: impl FnOnce() -> Option<String>,
) -> PermissionDeniedError { ) -> PermissionDeniedError {
PermissionDeniedError { PermissionDeniedError::Retryable {
access: Self::fmt_access(name, info), access: Self::fmt_access(name, info),
name, name,
} }
@ -201,10 +202,12 @@ impl PermissionState {
Self::log_perm_access(name, info); Self::log_perm_access(name, info);
(Ok(()), true, true) (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),
} }
} }
} }