Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more user friendly message for sftp errors #129

Merged
merged 2 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src-tauri/src/conn_pool/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ops::{Deref, DerefMut};
use std::sync::Mutex;
use std::time::Duration;

use libssh_rs::{AuthStatus, LogLevel, Session, SshKey, SshOption};
use libssh_rs::{AuthStatus, Session, SshKey, SshOption};
use uuid::Uuid;

use crate::conn_pool::DeviceConnection;
Expand Down
50 changes: 43 additions & 7 deletions src-tauri/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use libssh_rs::Error as SshError;
use serde::{Serialize, Serializer};
use std::error::Error as ErrorTrait;
use std::fmt::{Debug, Display, Formatter};
use std::io::ErrorKind;
use std::str::FromStr;

use libssh_rs::{Error as SshError, SftpError};
use serde::{Serialize, Serializer};

#[derive(Debug, Serialize, Clone)]
#[serde(tag = "reason")]
Expand All @@ -19,7 +22,7 @@ pub enum Error {
},
IO {
#[serde(serialize_with = "as_debug_string")]
code: std::io::ErrorKind,
code: ErrorKind,
message: String,
},
Message {
Expand All @@ -42,6 +45,12 @@ impl Error {
message: String::from("Bad configuration"),
};
}
pub fn io(kind: ErrorKind) -> Error {
return Error::IO {
code: kind,
message: kind.to_string(),
};
}
}

impl ErrorTrait for Error {}
Expand Down Expand Up @@ -85,7 +94,7 @@ impl From<SshError> for Error {
message: format!("SSH Error: {s}"),
},
SshError::TryAgain => Error::IO {
code: std::io::ErrorKind::WouldBlock,
code: ErrorKind::WouldBlock,
message: format!("Would block"),
},
SshError::Fatal(s) => {
Expand All @@ -94,13 +103,13 @@ impl From<SshError> for Error {
Error::Disconnected
} else {
Error::IO {
code: std::io::ErrorKind::Other,
code: ErrorKind::Other,
message: String::from(socket_error),
}
};
} else if s == "Connection refused" {
return Error::IO {
code: std::io::ErrorKind::ConnectionRefused,
code: ErrorKind::ConnectionRefused,
message: format!("Connection refused by host"),
};
} else if s.starts_with("Timeout connecting to ") {
Expand All @@ -112,11 +121,38 @@ impl From<SshError> for Error {
message: format!("SSH Error: {s}"),
}
}
SshError::Sftp(e) => Error::new(format!("SFTP Error: {:?}", e)),
SshError::Sftp(e) => e.into(),
};
}
}

impl From<SftpError> for Error {
fn from(value: SftpError) -> Self {
let message = value.to_string();
if let Some(code) = message
.strip_prefix("Sftp error code ")
.and_then(|s| u32::from_str(s).ok())
{
return match code {
libssh_rs_sys::SSH_FX_EOF => Error::io(ErrorKind::UnexpectedEof),
libssh_rs_sys::SSH_FX_NO_SUCH_FILE => Error::io(ErrorKind::NotFound),
libssh_rs_sys::SSH_FX_PERMISSION_DENIED => Error::io(ErrorKind::PermissionDenied),
libssh_rs_sys::SSH_FX_FAILURE => Error::new("Failed to perform this operation"),
libssh_rs_sys::SSH_FX_NO_CONNECTION => Error::Disconnected,
libssh_rs_sys::SSH_FX_CONNECTION_LOST => Error::Disconnected,
libssh_rs_sys::SSH_FX_NO_SUCH_PATH => Error::io(ErrorKind::NotFound),
libssh_rs_sys::SSH_FX_FILE_ALREADY_EXISTS => Error::io(ErrorKind::AlreadyExists),
libssh_rs_sys::SSH_FX_WRITE_PROTECT => Error::IO {
code: ErrorKind::Other,
message: String::from("SSH_FX_WRITE_PROTECT"),
},
_ => Error::Message { message },
};
}
return Error::Message { message };
}
}

impl From<Box<dyn ErrorTrait>> for Error {
fn from(value: Box<dyn ErrorTrait>) -> Self {
return Error::new(format!("{:?}", value));
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/plugins/devmode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async fn valid_token<R: Runtime>(
.await
.unwrap()?;
let token = String::from_utf8(data).map_err(|_| Error::IO {
code: std::io::ErrorKind::Other,
code: std::io::ErrorKind::InvalidData,
message: format!("Can\'t read dev mode token"),
})?;
let regex = Regex::new("^[0-9a-zA-Z]+$").unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class AppComponent {
if (remoteVer.compare(curVer) > 0) {
await this.notifyUpdate(info, remoteVer);
}
});
}).catch(noop);
deviceManager.load();
}

Expand Down
8 changes: 3 additions & 5 deletions src/app/core/services/backend-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ export abstract class BackendClient {
tauri.invoke(cmd, args)
.then(result => {
console.debug('invoke', `${this.category}/${method}`, 'result', result);
return result;
this.zone.run(() => resolve(result as any));
})
.catch(reason => {
console.warn('invoke', `${this.category}/${method}`, 'error', reason);
throw new BackendError(reason);
})
.then(result => this.zone.run(() => resolve(result as any)))
.catch(reason => this.zone.run(() => reject(reason)));
this.zone.run(() => reject(new BackendError(reason)));
});
});
}

Expand Down
12 changes: 11 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {AppConfig} from './environments/environment';
import ReleaseInfo from './release.json';
import * as Sentry from "@sentry/angular-ivy";
import {Breadcrumb, defaultStackParser} from "@sentry/angular-ivy";
import {remove} from "lodash";

Sentry.init({
dsn: "https://93c623f5a47940f0b7bac7d0d5f6a91f@o4504977150377984.ingest.sentry.io/4504978685689856",
Expand All @@ -15,7 +16,7 @@ Sentry.init({
routingInstrumentation: Sentry.routingInstrumentation,
})
],
enabled: !!ReleaseInfo.version,
enabled: true,
environment: AppConfig.environment,
release: ReleaseInfo.version || 'local',
stackParser: (stack: string, skipFirst?: number) => {
Expand All @@ -26,6 +27,15 @@ Sentry.init({
beforeBreadcrumb: (breadcrumb: Breadcrumb) => {
return breadcrumb.level !== 'debug' ? breadcrumb : null;
},
beforeSend: (event) => {
if (event.exception?.values) {
const filtered = remove(event.exception?.values, e => e.mechanism?.handled !== true);
if (filtered.length === 0) {
return null;
}
}
return event;
},
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
Expand Down