Skip to content

Commit

Permalink
feat(core): validate dialog default_path (it must exist) (#1599)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Apr 23, 2021
1 parent aa7e273 commit cfa74eb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changes/dialog-default-path-validation.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Validate dialog option `defaultPath` - it must exists.
6 changes: 6 additions & 0 deletions core/tauri/src/endpoints/dialog.rs
Expand Up @@ -144,6 +144,9 @@ fn set_default_path(
pub fn open(options: OpenDialogOptions) -> crate::Result<InvokeResponse> {
let mut dialog_builder = FileDialogBuilder::new();
if let Some(default_path) = options.default_path {
if !default_path.exists() {
return Err(crate::Error::DialogDefaultPathNotExists(default_path));
}
dialog_builder = set_default_path(dialog_builder, default_path);
}
for filter in options.filters {
Expand All @@ -165,6 +168,9 @@ pub fn open(options: OpenDialogOptions) -> crate::Result<InvokeResponse> {
pub fn save(options: SaveDialogOptions) -> crate::Result<InvokeResponse> {
let mut dialog_builder = FileDialogBuilder::new();
if let Some(default_path) = options.default_path {
if !default_path.exists() {
return Err(crate::Error::DialogDefaultPathNotExists(default_path));
}
dialog_builder = set_default_path(dialog_builder, default_path);
}
for filter in options.filters {
Expand Down
5 changes: 5 additions & 0 deletions core/tauri/src/error.rs
Expand Up @@ -2,6 +2,8 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use std::path::PathBuf;

/// Runtime errors that can happen inside a Tauri application.
#[derive(Debug, thiserror::Error)]
pub enum Error {
Expand Down Expand Up @@ -60,6 +62,9 @@ pub enum Error {
/// Error initializing plugin.
#[error("failed to initialize plugin `{0}`: {1}")]
PluginInitialization(String, String),
/// `default_path` provided to dialog API doesn't exist.
#[error("failed to setup dialog: provided default path `{0}` doesn't exist")]
DialogDefaultPathNotExists(PathBuf),
}

impl From<serde_json::Error> for Error {
Expand Down

0 comments on commit cfa74eb

Please sign in to comment.