error.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. use std::path::PathBuf;
  5. /// Runtime errors that can happen inside a Tauri application.
  6. #[derive(Debug, thiserror::Error)]
  7. #[non_exhaustive]
  8. pub enum Error {
  9. /// Runtime error.
  10. #[error("runtime error: {0}")]
  11. Runtime(#[from] tauri_runtime::Error),
  12. /// Failed to create webview.
  13. #[error("failed to create webview: {0}")]
  14. CreateWebview(Box<dyn std::error::Error + Send>),
  15. /// Failed to create window.
  16. #[error("failed to create window")]
  17. CreateWindow,
  18. /// Window label must be unique.
  19. #[error("a window with label `{0}` already exists")]
  20. WindowLabelAlreadyExists(String),
  21. /// Can't access webview dispatcher because the webview was closed or not found.
  22. #[error("webview not found: invalid label or it was closed")]
  23. WebviewNotFound,
  24. /// Failed to send message to webview.
  25. #[error("failed to send message to the webview")]
  26. FailedToSendMessage,
  27. /// Embedded asset not found.
  28. #[error("asset not found: {0}")]
  29. AssetNotFound(String),
  30. /// Failed to serialize/deserialize.
  31. #[error("JSON error: {0}")]
  32. Json(serde_json::Error),
  33. /// Unknown API type.
  34. #[error("unknown API: {0:?}")]
  35. UnknownApi(Option<serde_json::Error>),
  36. /// Failed to execute tauri API.
  37. #[error("failed to execute API: {0}")]
  38. FailedToExecuteApi(#[from] crate::api::Error),
  39. /// IO error.
  40. #[error("{0}")]
  41. Io(#[from] std::io::Error),
  42. /// Failed to decode base64.
  43. #[cfg(any(fs_write_binary_file, feature = "updater"))]
  44. #[error("Failed to decode base64 string: {0}")]
  45. Base64Decode(#[from] base64::DecodeError),
  46. /// Failed to load window icon.
  47. #[error("invalid icon: {0}")]
  48. InvalidIcon(Box<dyn std::error::Error + Send>),
  49. /// Client with specified ID not found.
  50. #[error("http client dropped or not initialized")]
  51. HttpClientNotInitialized,
  52. /// API not enabled by Tauri.
  53. #[error("{0}")]
  54. ApiNotEnabled(String),
  55. /// API not whitelisted on tauri.conf.json
  56. #[error("'{0}' not on the allowlist (https://tauri.studio/docs/api/config#tauri.allowlist)")]
  57. ApiNotAllowlisted(String),
  58. /// Invalid args when running a command.
  59. #[error("invalid args for command `{0}`: {1}")]
  60. InvalidArgs(&'static str, serde_json::Error),
  61. /// Encountered an error in the setup hook,
  62. #[error("error encountered during setup hook: {0}")]
  63. Setup(Box<dyn std::error::Error + Send>),
  64. /// Tauri updater error.
  65. #[cfg(feature = "updater")]
  66. #[error("Updater: {0}")]
  67. TauriUpdater(#[from] crate::updater::Error),
  68. /// Error initializing plugin.
  69. #[error("failed to initialize plugin `{0}`: {1}")]
  70. PluginInitialization(String, String),
  71. /// `default_path` provided to dialog API doesn't exist.
  72. #[error("failed to setup dialog: provided default path `{0}` doesn't exist")]
  73. DialogDefaultPathNotExists(PathBuf),
  74. /// Encountered an error creating the app system tray,
  75. #[error("error encountered during tray setup: {0}")]
  76. SystemTray(Box<dyn std::error::Error + Send>),
  77. /// A part of the URL is malformed or invalid. This may occur when parsing and combining
  78. /// user-provided URLs and paths.
  79. #[error("invalid url: {0}")]
  80. InvalidUrl(url::ParseError),
  81. }
  82. impl From<serde_json::Error> for Error {
  83. fn from(error: serde_json::Error) -> Self {
  84. if error.to_string().contains("unknown variant") {
  85. Self::UnknownApi(Some(error))
  86. } else {
  87. Self::Json(error)
  88. }
  89. }
  90. }