error.rs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. /// Can't access webview dispatcher because the webview was closed or not found.
  19. #[error("webview not found: invalid label or it was closed")]
  20. WebviewNotFound,
  21. /// Failed to send message to webview.
  22. #[error("failed to send message to the webview")]
  23. FailedToSendMessage,
  24. /// Embedded asset not found.
  25. #[error("asset not found: {0}")]
  26. AssetNotFound(String),
  27. /// Failed to serialize/deserialize.
  28. #[error("JSON error: {0}")]
  29. Json(serde_json::Error),
  30. /// Unknown API type.
  31. #[error("unknown API: {0:?}")]
  32. UnknownApi(Option<serde_json::Error>),
  33. /// Failed to execute tauri API.
  34. #[error("failed to execute API: {0}")]
  35. FailedToExecuteApi(#[from] crate::api::Error),
  36. /// IO error.
  37. #[error("{0}")]
  38. Io(#[from] std::io::Error),
  39. /// Failed to decode base64.
  40. #[error("Failed to decode base64 string: {0}")]
  41. Base64Decode(#[from] base64::DecodeError),
  42. /// Failed to load window icon.
  43. #[error("invalid icon: {0}")]
  44. InvalidIcon(Box<dyn std::error::Error + Send>),
  45. /// Client with specified ID not found.
  46. #[error("http client dropped or not initialized")]
  47. HttpClientNotInitialized,
  48. /// API not enabled by Tauri.
  49. #[error("{0}")]
  50. ApiNotEnabled(String),
  51. /// API not whitelisted on tauri.conf.json
  52. #[error("'{0}' not on the allowlist (https://tauri.studio/docs/api/config#tauri.allowlist)")]
  53. ApiNotAllowlisted(String),
  54. /// Invalid args when running a command.
  55. #[error("invalid args for command `{0}`: {1}")]
  56. InvalidArgs(&'static str, serde_json::Error),
  57. /// Encountered an error in the setup hook,
  58. #[error("error encountered during setup hook: {0}")]
  59. Setup(Box<dyn std::error::Error + Send>),
  60. /// Tauri updater error.
  61. #[cfg(feature = "updater")]
  62. #[error("Updater: {0}")]
  63. TauriUpdater(#[from] crate::updater::Error),
  64. /// Error initializing plugin.
  65. #[error("failed to initialize plugin `{0}`: {1}")]
  66. PluginInitialization(String, String),
  67. /// `default_path` provided to dialog API doesn't exist.
  68. #[error("failed to setup dialog: provided default path `{0}` doesn't exist")]
  69. DialogDefaultPathNotExists(PathBuf),
  70. /// Encountered an error creating the app system tray,
  71. #[error("error encountered during tray setup: {0}")]
  72. SystemTray(Box<dyn std::error::Error + Send>),
  73. }
  74. impl From<serde_json::Error> for Error {
  75. fn from(error: serde_json::Error) -> Self {
  76. if error.to_string().contains("unknown variant") {
  77. Self::UnknownApi(Some(error))
  78. } else {
  79. Self::Json(error)
  80. }
  81. }
  82. }