feat(core): validate dialog default_path (it must exist) (#1599)

This commit is contained in:
Lucas Fernandes Nogueira
2021-04-23 03:17:29 -03:00
committed by GitHub
parent aa7e2738cc
commit cfa74ebf68
3 changed files with 16 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
---
"tauri": patch
---
Validate dialog option `defaultPath` - it must exists.

View File

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

View File

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