fundamentum_edge_pfwd/
errors.rs1use std::{io, num::TryFromIntError};
4
5use displaydoc::Display;
6use fundamentum_portforwarding_proto_rust::ErrorCode;
7use thiserror::Error;
8use tokio::sync::mpsc::error::SendError;
9use tracing::error;
10use uuid::Uuid;
11
12use crate::protocol_sdk::ProtocolSdk;
13
14#[derive(Debug, Error)]
16pub enum Error {
17 #[error(transparent)]
19 HeaderParse(#[from] HeaderParseError),
20
21 #[error(transparent)]
23 InternalError(#[from] InternalError),
24
25 #[error(transparent)]
27 CloudOperationError(#[from] CloudOperationError),
28}
29
30#[derive(Debug, Error, Display)]
32pub enum InternalError {
33 SendMessage(String),
35
36 ReceiveMessage(String),
38
39 MessageIsNotPortForwarding,
41}
42
43#[derive(Debug, Error, Display)]
45pub enum CloudOperationError {
46 InstanceNotFound,
48
49 SendDataToInstance(SendError<Vec<u8>>, Uuid),
51
52 #[cfg(unix)]
54 SpawnTty(pty_process::Error),
55
56 #[cfg(unix)]
58 FailedToSpawnTtyCommand(pty_process::Error),
59
60 #[cfg(unix)]
62 FailedToResizeTty(pty_process::Error),
63
64 SpawnTcp(io::Error),
66
67 InvalidRowsColsValueForTty(TryFromIntError),
69
70 InvalidPort(TryFromIntError),
72
73 WriteDataToServer(io::Error),
75
76 UnsupportedPlatform,
78
79 UnexpectedOperation,
81}
82
83#[derive(Debug, Error, Display)]
85pub enum HeaderParseError {
86 VersionNotFound,
88
89 InvalidVersion(String),
91
92 UsidNotFound,
94
95 InvalidUsid(String),
97
98 OperationNotFound,
100
101 InvalidOperation(String),
103
104 InvalidOperationCode(String),
106}
107
108pub async fn send_or_log_error<P: ProtocolSdk>(sender: P, err: Error, instance_id: Uuid) {
114 match err {
115 Error::CloudOperationError(sendable_err) => {
116 let err_string = sendable_err.to_string();
117 sender
118 .send_error(instance_id, sendable_err.into(), &err_string)
119 .await
120 .unwrap_or_else(|e| error!(error = %e, "failed to send error to the cloud"));
121 }
122 Error::InternalError(InternalError::MessageIsNotPortForwarding) => {}
123 _ => error!(error = %err, "unexpected error occured"),
124 };
125}
126
127impl From<CloudOperationError> for ErrorCode {
128 fn from(val: CloudOperationError) -> Self {
129 match val {
130 CloudOperationError::InstanceNotFound => Self::InstanceNotFound,
131 CloudOperationError::SendDataToInstance(_, _) => Self::InstanceCorrupted,
132 CloudOperationError::SpawnTcp(_) => Self::SpawnTcp,
133 CloudOperationError::InvalidRowsColsValueForTty(_) => Self::InvalidRowsColsValueForTty,
134 CloudOperationError::InvalidPort(_) => Self::InvalidPort,
135 CloudOperationError::WriteDataToServer(_) => Self::WriteDataToServer,
136 CloudOperationError::UnsupportedPlatform => Self::UnsupportedPlatform,
137 CloudOperationError::UnexpectedOperation => Self::UnexpectedOperation,
138 #[cfg(unix)]
139 CloudOperationError::SpawnTty(_) => Self::SpawnTty,
140 #[cfg(unix)]
141 CloudOperationError::FailedToSpawnTtyCommand(_) => Self::SpawnTtyCommand,
142 #[cfg(unix)]
143 CloudOperationError::FailedToResizeTty(_) => Self::ResizeTty,
144 }
145 }
146}