Skip to main content

realm/
errors.rs

1use std::fmt;
2
3#[derive(Debug, Clone)]
4pub enum RealmError {
5  ConfigError(ConfigError),
6  ProcessError(ProcessError),
7  ProxyError(ProxyError),
8  RuntimeError(RuntimeError),
9  TemplateError(TemplateError),
10  IoError(String),
11  NetworkError(String),
12  ValidationError(String),
13}
14
15#[derive(Debug, Clone)]
16pub enum ConfigError {
17  FileNotFound(String),
18  ParseError(String),
19  InvalidFormat(String),
20  MissingField(String),
21}
22
23#[derive(Debug, Clone)]
24pub enum ProcessError {
25  StartFailed(String),
26  StopFailed(String),
27  NotFound(String),
28  AlreadyRunning(String),
29  CommandParseError(String),
30  PermissionDenied(String),
31}
32
33#[derive(Debug, Clone)]
34pub enum ProxyError {
35  BindFailed(String),
36  RouteNotFound(String),
37  UpstreamError(String),
38  InvalidPort(u16),
39  RequestForwardError(String),
40}
41
42#[derive(Debug, Clone)]
43pub enum RuntimeError {
44  NotInstalled(String),
45  InstallationFailed(String),
46  DownloadFailed(String),
47  UnsupportedPlatform(String),
48  InvalidVersion(String),
49  ExtractionFailed(String),
50}
51
52#[derive(Debug, Clone)]
53pub enum TemplateError {
54  NotFound(String),
55  CreationFailed(String),
56  InvalidTemplate(String),
57  FileSystemError(String),
58}
59
60impl fmt::Display for RealmError {
61  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62    match self {
63      RealmError::ConfigError(e) => write!(f, "Configuration error: {e}"),
64      RealmError::ProcessError(e) => write!(f, "Process error: {e}"),
65      RealmError::ProxyError(e) => write!(f, "Proxy error: {e}"),
66      RealmError::RuntimeError(e) => write!(f, "Runtime error: {e}"),
67      RealmError::TemplateError(e) => write!(f, "Template error: {e}"),
68      RealmError::IoError(msg) => write!(f, "IO error: {msg}"),
69      RealmError::NetworkError(msg) => write!(f, "Network error: {msg}"),
70      RealmError::ValidationError(msg) => write!(f, "Validation error: {msg}"),
71    }
72  }
73}
74
75impl fmt::Display for ConfigError {
76  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77    match self {
78      ConfigError::FileNotFound(path) => write!(f, "Config file not found: {path}"),
79      ConfigError::ParseError(msg) => write!(f, "Failed to parse config: {msg}"),
80      ConfigError::InvalidFormat(msg) => write!(f, "Invalid config format: {msg}"),
81      ConfigError::MissingField(field) => write!(f, "Missing required field: {field}"),
82    }
83  }
84}
85
86impl fmt::Display for ProcessError {
87  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88    match self {
89      ProcessError::StartFailed(name) => write!(f, "Failed to start process: {name}"),
90      ProcessError::StopFailed(name) => write!(f, "Failed to stop process: {name}"),
91      ProcessError::NotFound(name) => write!(f, "Process not found: {name}"),
92      ProcessError::AlreadyRunning(name) => write!(f, "Process already running: {name}"),
93      ProcessError::CommandParseError(cmd) => write!(f, "Invalid command: {cmd}"),
94      ProcessError::PermissionDenied(name) => write!(f, "Permission denied for process: {name}"),
95    }
96  }
97}
98
99impl fmt::Display for ProxyError {
100  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101    match self {
102      ProxyError::BindFailed(addr) => write!(f, "Failed to bind to address: {addr}"),
103      ProxyError::RouteNotFound(path) => write!(f, "No route found for path: {path}"),
104      ProxyError::UpstreamError(msg) => write!(f, "Upstream error: {msg}"),
105      ProxyError::InvalidPort(port) => write!(f, "Invalid port: {port}"),
106      ProxyError::RequestForwardError(msg) => write!(f, "Request forwarding failed: {msg}"),
107    }
108  }
109}
110
111impl fmt::Display for RuntimeError {
112  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
113    match self {
114      RuntimeError::NotInstalled(runtime) => write!(f, "Runtime not installed: {runtime}"),
115      RuntimeError::InstallationFailed(runtime) => write!(f, "Installation failed: {runtime}"),
116      RuntimeError::DownloadFailed(url) => write!(f, "Download failed: {url}"),
117      RuntimeError::UnsupportedPlatform(platform) => write!(f, "Unsupported platform: {platform}"),
118      RuntimeError::InvalidVersion(version) => write!(f, "Invalid version: {version}"),
119      RuntimeError::ExtractionFailed(msg) => write!(f, "Extraction failed: {msg}"),
120    }
121  }
122}
123
124impl fmt::Display for TemplateError {
125  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126    match self {
127      TemplateError::NotFound(name) => write!(f, "Template not found: {name}"),
128      TemplateError::CreationFailed(name) => write!(f, "Template creation failed: {name}"),
129      TemplateError::InvalidTemplate(name) => write!(f, "Invalid template: {name}"),
130      TemplateError::FileSystemError(msg) => write!(f, "File system error: {msg}"),
131    }
132  }
133}
134
135impl std::error::Error for RealmError {}
136impl std::error::Error for ConfigError {}
137impl std::error::Error for ProcessError {}
138impl std::error::Error for ProxyError {}
139impl std::error::Error for RuntimeError {}
140impl std::error::Error for TemplateError {}
141
142pub type Result<T> = std::result::Result<T, RealmError>;
143
144impl From<std::io::Error> for RealmError {
145  fn from(err: std::io::Error) -> Self {
146    RealmError::IoError(err.to_string())
147  }
148}
149
150impl From<reqwest::Error> for RealmError {
151  fn from(err: reqwest::Error) -> Self {
152    RealmError::NetworkError(err.to_string())
153  }
154}
155
156impl From<serde_yaml::Error> for RealmError {
157  fn from(err: serde_yaml::Error) -> Self {
158    RealmError::ConfigError(ConfigError::ParseError(err.to_string()))
159  }
160}
161
162impl From<anyhow::Error> for RealmError {
163  fn from(err: anyhow::Error) -> Self {
164    RealmError::ValidationError(err.to_string())
165  }
166}