Skip to content

Commit

Permalink
Refactor error handling (#7)
Browse files Browse the repository at this point in the history
* refactor error handling

* feat. adding additional classification for code defects

* GitHub/pr/refactor error handling (#11)

* Update rust-tag-release.yml

* Update rust-tag-release.yml

* Update rust-tag-release.yml

* Update rust-tag-release.yml

* Update rust-tag-release.yml

* Update rust-tag-release.yml

* Update rust-tag-release.yml

* Update rust-tag-release.yml

* Update rust-tag-release.yml

* Update rust-tag-release.yml

* Update rust-tag-release.yml

* Pr/juliusl/mirror updates (#8)

* feat. update dev script

* Update pipeline

* fix. dev tool, vm img path

* Feat. adding support for rpm packages

* fix. enable additional logging

* Enable legacy support workaround (#9)

* feat. adding a workaround for specifying tenant in image path

* Feat. adding support for ctrd version under 1.7
- allows for image references to specify tenant in path

* fixup.

* chore. remove unused files
feat. add token cache invalidation
  • Loading branch information
juliusl authored Feb 7, 2023
1 parent 0d5b90b commit b828a0a
Show file tree
Hide file tree
Showing 15 changed files with 350 additions and 510 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ resolver = "2"

[features]
default = []
editor = [ "lifec/editor", "imgui" ]
editor = ["lifec/editor", "imgui"]

[dependencies]
# Optional
Expand Down Expand Up @@ -46,6 +46,7 @@ tracing-test = "0.2.3"
sha2 = "0.10.6"
serde_urlencoded = "0.7.1"
async-trait = "0.1.64"
base64-url = "1.4.13"

[[bin]]
name = "acr-dev"
Expand Down Expand Up @@ -78,4 +79,4 @@ assets = [
{ source = "lib/sh/overlaybd/enable.sh", dest = "/opt/acr/tools/overlaybd/enable.sh", mode = "755" },
{ source = "lib/sh/overlaybd/enable-file-auth.sh", dest = "/opt/acr/tools/overlaybd/enable-file-auth.sh", mode = "755" },
{ source = "lib/sh/overlaybd/enable-http-auth.sh", dest = "/opt/acr/tools/overlaybd/enable-http-auth.sh", mode = "755" },
]
]
36 changes: 8 additions & 28 deletions lib/runmd/.runmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
```
+ .config start.mirror
: app_host .symbol localhost:8578
: teleport .symbol overlaybd
: skip_hosts_dir_check .false
: enable_guest_agent .false
: enable_guest_agent_dispatcher .false
```

# Resolve manifest handler (/v2/../manifests/..)
Expand All @@ -16,7 +12,7 @@

```
+ overlaybd .operation manifests.resolve
: .login access_token
: .login token_cache
: .authn
: .request
: .resolve
Expand All @@ -31,28 +27,12 @@
# Download blob handler (/v2/../blobs/..)
```
+ .operation blobs.download
: .login access_token
: .authn
: .request
```

# Setup guest storage
```
+ .operation setup.guest
: .remote_registry
: .process sh setup-guest-storage.sh
```

# Open guest
```
+ .operation open.guest
: .remote_registry
: .remote_guest
```
: .login token_cache
: .authn
: .request

# Test operation
```
+ .operation print
: .remote_registry
: .println
+ overlaybd .operation blobs.download
: .login token_cache
: .authn
: .request
```
4 changes: 2 additions & 2 deletions src/content/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl Registry {
if let Err(err) = mirror_hosts_config.install(None::<String>) {
error!("Unable to enable mirror host config for, {}, {:?}", namespace, err);
} else {
// debug!("Enabled mirror host config for {}")
debug!("Enabled mirror host config for {}", namespace);
}
}

Expand All @@ -103,7 +103,7 @@ impl Registry {
w.to_owned()
}
})
.unwrap();
.expect("should return a workspace by this point");

let operation_name = operation_name.into();
let operation = workspace.find_operation(&operation_name).expect(&format!(
Expand Down
82 changes: 81 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt::Display;
use std::{fmt::Display, string};

use hyper::{http::uri::InvalidUri, StatusCode};
use tracing::{error, warn};
Expand Down Expand Up @@ -65,6 +65,23 @@ impl Error {
category: ErrorCategory::SystemEnvironment,
}
}

/// Returns an error that indicates a coding error,
///
pub fn code_defect() -> Self {
Error {
category: ErrorCategory::CodeDefect
}
}

/// Returns true if the category is recoverable,
///
pub fn is_recoverable(&self) -> bool {
match self.category {
ErrorCategory::RecoverableError(_) => true,
_ => false,
}
}
}

#[derive(Debug)]
Expand All @@ -74,6 +91,7 @@ enum ErrorCategory {
ExternalDependency,
ExternalDependencyWithStatusCode(StatusCode),
SystemEnvironment,
CodeDefect,
InvalidOperation(&'static str),
RecoverableError(&'static str),
}
Expand Down Expand Up @@ -126,4 +144,66 @@ impl From<hyper::http::Error> for Error {
error!("Error making http request, {value}");
Self::external_dependency()
}
}

impl From<tokio::sync::oneshot::error::RecvError> for Error {
fn from(value: tokio::sync::oneshot::error::RecvError) -> Self {
error!("Error receiving result from a oneshot channel, likely a code-defect {value}");
Self::code_defect()
}
}

impl From<string::FromUtf8Error> for Error {
fn from(value: string::FromUtf8Error) -> Self {
error!("Error converting from bytes to string, input was not utf8, {value}");
Self::data_format()
}
}

impl From<base64_url::base64::DecodeError> for Error {
fn from(value: base64_url::base64::DecodeError) -> Self {
error!("Error decoding base64 string, input was not base64, {value}");
Self::data_format()
}
}

impl From<std::time::SystemTimeError> for Error {
fn from(value: std::time::SystemTimeError) -> Self {
error!("Could not convert system time to duration, {value}");
Self::code_defect()
}
}

impl From<Error> for lifec::error::Error {
fn from(value: Error) -> lifec::error::Error {
match &value.category {
ErrorCategory::Authentication => lifec::error::Error::invalid_operation("authentication failure"),
ErrorCategory::DataFormat => lifec::error::Error::invalid_operation("invalid data format"),
ErrorCategory::ExternalDependency => lifec::error::Error::invalid_operation("external dependency failure"),
ErrorCategory::ExternalDependencyWithStatusCode(status_code) => {
if let Some(reason) = status_code.canonical_reason() {
lifec::error::Error::invalid_operation(reason)
} else {
lifec::error::Error::invalid_operation("http error")
}
},
ErrorCategory::CodeDefect => lifec::error::Error::invalid_operation("code defect"),
ErrorCategory::SystemEnvironment => lifec::error::Error::invalid_operation("system environment error"),
ErrorCategory::InvalidOperation(reason) => lifec::error::Error::invalid_operation(reason),
ErrorCategory::RecoverableError(message) if message.starts_with("skip") => lifec::error::Error::skip(message),
ErrorCategory::RecoverableError(message) => lifec::error::Error::recoverable(message),
}
}
}

#[allow(unused_imports)]
mod tests {
use crate::Error;

#[test]
fn test_is_recoverable() {
let e = Error::recoverable_error("test");

assert!(e.is_recoverable());
}
}
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ pub use plugins::Mirror;
pub use plugins::Artifact;
pub use plugins::Authenticate;
pub use plugins::Login;
pub use plugins::LoginACR;
pub use plugins::LoginOverlayBD;
pub use plugins::Discover;
pub use plugins::Teleport;
pub use plugins::Resolve;
Expand Down
Loading

0 comments on commit b828a0a

Please sign in to comment.