Skip to content

Commit

Permalink
add constants for error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
robertohuertasm committed Oct 31, 2024
1 parent 60150a6 commit 68ef20a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
8 changes: 6 additions & 2 deletions crates/bins/src/bin/datadog_static_analyzer_server/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ use tracing_appender::non_blocking::WorkerGuard;
use tracing_appender::rolling::RollingFileAppender;
use tracing_subscriber::EnvFilter;

use crate::datadog_static_analyzer_server::error_codes::{
ERROR_CHANNEL_DISCONNECTED, ERROR_SHUTDOWN_FAILURE,

Check failure on line 15 in crates/bins/src/bin/datadog_static_analyzer_server/cli.rs

View workflow job for this annotation

GitHub Actions / production_rules

unresolved imports `crate::datadog_static_analyzer_server::error_codes::ERROR_CHANNEL_DISCONNECTED`, `crate::datadog_static_analyzer_server::error_codes::ERROR_SHUTDOWN_FAILURE`

Check failure on line 15 in crates/bins/src/bin/datadog_static_analyzer_server/cli.rs

View workflow job for this annotation

GitHub Actions / staging_rules

unresolved imports `crate::datadog_static_analyzer_server::error_codes::ERROR_CHANNEL_DISCONNECTED`, `crate::datadog_static_analyzer_server::error_codes::ERROR_SHUTDOWN_FAILURE`

Check failure on line 15 in crates/bins/src/bin/datadog_static_analyzer_server/cli.rs

View workflow job for this annotation

GitHub Actions / production_rules

unresolved imports `crate::datadog_static_analyzer_server::error_codes::ERROR_CHANNEL_DISCONNECTED`, `crate::datadog_static_analyzer_server::error_codes::ERROR_SHUTDOWN_FAILURE`

Check failure on line 15 in crates/bins/src/bin/datadog_static_analyzer_server/cli.rs

View workflow job for this annotation

GitHub Actions / staging_rules

unresolved imports `crate::datadog_static_analyzer_server::error_codes::ERROR_CHANNEL_DISCONNECTED`, `crate::datadog_static_analyzer_server::error_codes::ERROR_SHUTDOWN_FAILURE`
};

use super::state::ServerState;
use super::utils::get_current_timestamp_ms;

Expand Down Expand Up @@ -237,7 +241,7 @@ pub fn prepare_rocket(tx_keep_alive_error: Sender<i32>) -> Result<RocketPreparat
"No graceful exit on first attempt. Trying another channel"
);
// signal the main thread to kill itself. wait for 10 secs and use abort if needed.
let _ = tx_keep_alive_error.send(101).await;
let _ = tx_keep_alive_error.send(ERROR_SHUTDOWN_FAILURE).await;
thread::sleep(Duration::from_secs(10));
tracing::error!(
"No graceful exit on suicide channel. Aborting the process"
Expand All @@ -249,7 +253,7 @@ pub fn prepare_rocket(tx_keep_alive_error: Sender<i32>) -> Result<RocketPreparat
} else {
tracing::error!("CRITICAL: The channel has disconnected");
// if the channel dies we're going to exit the process with a custom error code.
let _ = tx_keep_alive_error.send(100).await;
let _ = tx_keep_alive_error.send(ERROR_CHANNEL_DISCONNECTED).await;
};
});
}
Expand Down
15 changes: 9 additions & 6 deletions crates/bins/src/bin/datadog_static_analyzer_server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ use std::process;

use cli::{CliError, RocketPreparation};
use endpoints::EndpointError;
use error_codes::{
ERROR_BAD_ADDRESS, ERROR_CHANNEL_SENDER_DROPPED, ERROR_GENERAL, ERROR_INVALID_ARGUMENT,

Check failure on line 6 in crates/bins/src/bin/datadog_static_analyzer_server/mod.rs

View workflow job for this annotation

GitHub Actions / production_rules

unresolved imports `error_codes::ERROR_BAD_ADDRESS`, `error_codes::ERROR_CHANNEL_SENDER_DROPPED`, `error_codes::ERROR_GENERAL`, `error_codes::ERROR_INVALID_ARGUMENT`

Check failure on line 6 in crates/bins/src/bin/datadog_static_analyzer_server/mod.rs

View workflow job for this annotation

GitHub Actions / staging_rules

unresolved imports `error_codes::ERROR_BAD_ADDRESS`, `error_codes::ERROR_CHANNEL_SENDER_DROPPED`, `error_codes::ERROR_GENERAL`, `error_codes::ERROR_INVALID_ARGUMENT`

Check failure on line 6 in crates/bins/src/bin/datadog_static_analyzer_server/mod.rs

View workflow job for this annotation

GitHub Actions / production_rules

unresolved imports `error_codes::ERROR_BAD_ADDRESS`, `error_codes::ERROR_CHANNEL_SENDER_DROPPED`, `error_codes::ERROR_GENERAL`, `error_codes::ERROR_INVALID_ARGUMENT`

Check failure on line 6 in crates/bins/src/bin/datadog_static_analyzer_server/mod.rs

View workflow job for this annotation

GitHub Actions / staging_rules

unresolved imports `error_codes::ERROR_BAD_ADDRESS`, `error_codes::ERROR_CHANNEL_SENDER_DROPPED`, `error_codes::ERROR_GENERAL`, `error_codes::ERROR_INVALID_ARGUMENT`
};
use rocket::tokio::sync::mpsc::channel;

mod cli;
mod endpoints;
mod error_codes;

Check failure on line 12 in crates/bins/src/bin/datadog_static_analyzer_server/mod.rs

View workflow job for this annotation

GitHub Actions / production_rules

file not found for module `error_codes`

Check failure on line 12 in crates/bins/src/bin/datadog_static_analyzer_server/mod.rs

View workflow job for this annotation

GitHub Actions / staging_rules

file not found for module `error_codes`

Check failure on line 12 in crates/bins/src/bin/datadog_static_analyzer_server/mod.rs

View workflow job for this annotation

GitHub Actions / production_rules

file not found for module `error_codes`

Check failure on line 12 in crates/bins/src/bin/datadog_static_analyzer_server/mod.rs

View workflow job for this annotation

GitHub Actions / staging_rules

file not found for module `error_codes`
mod fairings;
mod ide;
mod state;
Expand All @@ -31,8 +35,8 @@ pub async fn start() {
Err(e) => {
eprintln!("Error found: {e}");
match e {
CliError::Parsing(_) => process::exit(22), // invalid argument code
_ => process::exit(1), // generic error code
CliError::Parsing(_) => process::exit(ERROR_INVALID_ARGUMENT),
_ => process::exit(ERROR_GENERAL),
}
}
Ok(RocketPreparation::NoServerInteraction) => {
Expand All @@ -53,13 +57,12 @@ pub async fn start() {
*rocket = rocket.attach(fairings::KeepAlive);
}

// launch the rocket

// launch the rocket and check if we receive a keep-alive error
let result = rocket::tokio::select! {
a = endpoints::launch_rocket_with_endpoints(*rocket, tx_rocket_shutdown) => a,
b = rx_keep_alive_error.recv() => match b {
Some(c) => Err(c.into()),
_ => Err(EndpointError::ExitCode(102))
_ => Err(EndpointError::ExitCode(ERROR_CHANNEL_SENDER_DROPPED))
},
};

Expand All @@ -76,7 +79,7 @@ pub async fn start() {
EndpointError::ExitCode(code) => process::exit(code),
EndpointError::RocketError(re) => {
if let rocket::error::ErrorKind::Bind(_) = re.kind() {
process::exit(14); // bad address error code
process::exit(ERROR_BAD_ADDRESS);
}
}
_ => (),
Expand Down

0 comments on commit 68ef20a

Please sign in to comment.