Offentliggjort kilde
Download kilde ZIP
CH-J Tools
Gennemse mapper og filer til en specifik applikationsudgivelse.
CH-J / Server Manager
872 filer
core-20261030T220507Z-61e4558d889f
Tilføjet: · Bygget:
1
//! Structured error shared by all Core services.2
//!3
//! The JSON shape (`code`, `message` and optional detail fields) matches the4
//! Electron 0.1.0 contract (`SessionError.toJSON`, diagnostics/biometric5
//! envelopes), so the unchanged renderer code keeps working.7
use serde::Serialize;8
use serde_json::{Map, Value};9
use std::fmt;11
#[derive(Debug, Clone, Serialize)]12
pub struct CoreError {13
pub code: String,14
pub message: String,15
#[serde(flatten)]16
pub details: Map<String, Value>,17
}19
pub type CoreResult<T> = Result<T, CoreError>;21
impl CoreError {22
pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {23
Self { code: code.into(), message: message.into(), details: Map::new() }24
}26
/// Error without a public code (Electron handlers that only threw `new Error(message)`).27
pub fn plain(message: impl Into<String>) -> Self {28
Self::new("", message)29
}31
pub fn with(mut self, key: &str, value: impl Into<Value>) -> Self {32
self.details.insert(key.to_string(), value.into());33
self34
}36
pub fn has_code(&self) -> bool {37
!self.code.is_empty()38
}40
/// Message with a `CODE: ` prefix, as the plugin runtime of 0.1.0 exposed it.41
pub fn prefixed_message(&self) -> String {42
if self.code.is_empty() || self.message.starts_with(&format!("{}:", self.code)) {43
self.message.clone()44
} else {45
format!("{}: {}", self.code, self.message)46
}47
}48
}50
impl fmt::Display for CoreError {51
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {52
if self.code.is_empty() {53
write!(f, "{}", self.message)54
} else {55
write!(f, "{}: {}", self.code, self.message)56
}57
}58
}60
impl std::error::Error for CoreError {}62
impl From<std::io::Error> for CoreError {63
fn from(error: std::io::Error) -> Self {64
let code = match error.kind() {65
std::io::ErrorKind::NotFound => "ENOENT",66
std::io::ErrorKind::PermissionDenied => "EACCES",67
std::io::ErrorKind::AlreadyExists => "EEXIST",68
_ => "EIO",69
};70
CoreError::new(code, error.to_string())71
}72
}74
impl From<serde_json::Error> for CoreError {75
fn from(error: serde_json::Error) -> Self {76
CoreError::new("INVALID_JSON", error.to_string())77
}78
}80
#[macro_export]81
macro_rules! bail {82
($code:expr, $($arg:tt)*) => {83
return Err($crate::core::error::CoreError::new($code, format!($($arg)*)))84
};85
}SHA-256
2dddbf80e310eb29c76530afab399239b5dd3a17787ddc8f851ecea1bd44dc5fCH-J Proprietary Software License 1.14 · Kilden leveres under CH-J Proprietary Software License 1.14. Dens tilgængelighed ændrer ikke licensbetingelserne eller giver yderligere rettigheder.
Arkiv SHA-256
77380d4805485ffa4027fbe7858b5836f197a4aa9d52358a43e2d12d018744df