Skip to content

Commit 3af6246

Browse files
committed
Better error handling
1 parent a3fa59f commit 3af6246

File tree

8 files changed

+55
-24
lines changed

8 files changed

+55
-24
lines changed

src/config.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,18 @@ impl Default for EpsilonConfig {
3939
impl EpsilonConfig {
4040
pub fn load(name: &str) -> Arc<EpsilonConfig> {
4141
Arc::new(match fs::read_to_string(name) {
42-
Ok(json) => serde_json::from_str::<EpsilonConfig>(&json).unwrap(),
42+
Ok(json) => {
43+
serde_json::from_str::<EpsilonConfig>(&json).expect("Failed to parse config")
44+
}
4345
Err(_) => {
4446
let config = EpsilonConfig::default();
4547

46-
fs::write(name, serde_json::to_string_pretty(&config).unwrap()).unwrap();
48+
fs::write(
49+
name,
50+
serde_json::to_string_pretty(&config)
51+
.expect("Failed to serialize default config"),
52+
)
53+
.expect("Failed to write default config");
4754

4855
config
4956
}

src/controller/definitions/epsilon_instance.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,10 @@ impl EpsilonInstance {
7373
}
7474

7575
pub async fn get_info(&self) -> Result<StatusResponse, EpsilonError> {
76-
let status = self
76+
let address = self
7777
.status
7878
.as_ref()
79-
.ok_or(EpsilonError::RetrieveStatusError)?;
80-
81-
let address = status
82-
.ip
83-
.as_ref()
79+
.and_then(|status| status.ip.as_ref())
8480
.ok_or(EpsilonError::RetrieveStatusError)?;
8581

8682
let config = ConnectionConfig::build(address);

src/epsilon/api/epsilon_api.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use tokio::sync::broadcast::{channel, Receiver, Sender};
22

33
use crate::epsilon::api::common::epsilon_events::EpsilonEvent;
4+
use crate::epsilon::epsilon_error::EpsilonError;
45

56
pub struct EpsilonApi {
67
channel: Sender<EpsilonEvent>,
@@ -13,8 +14,14 @@ impl EpsilonApi {
1314
}
1415
}
1516

16-
pub fn send(&self, event: EpsilonEvent) {
17-
self.channel.send(event).unwrap();
17+
pub fn send(&self, event: EpsilonEvent) -> Result<(), EpsilonError> {
18+
let event_name = event.to_string();
19+
20+
self.channel
21+
.send(event)
22+
.map_err(|_| EpsilonError::SendEventError(event_name))?;
23+
24+
Ok(())
1825
}
1926

2027
pub fn subscribe(&self) -> Receiver<EpsilonEvent> {

src/epsilon/epsilon_error.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ pub enum EpsilonError {
1111
#[error("Failed to parse json")]
1212
ParseJsonError,
1313

14-
#[error("Create instance error, template is ({0})")]
14+
#[error("Send event error {0}")]
15+
SendEventError(String),
16+
17+
#[error("Create instance error, template is {0}")]
1518
CreateInstanceError(String),
1619

1720
#[error("Remove instance error {0}")]
@@ -23,6 +26,9 @@ pub enum EpsilonError {
2326
#[error("Retrieve status error")]
2427
RetrieveStatusError,
2528

29+
#[error("Queue not found error {0}")]
30+
QueueNotFoundError(String),
31+
2632
#[error("Request error {0}")]
2733
RequestError(#[from] reqwest::Error),
2834

src/epsilon/queue/routes.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@ use std::sync::Arc;
33
use rocket::serde::json::Json;
44
use rocket::State;
55

6-
use crate::Context;
6+
use crate::epsilon::epsilon_error::EpsilonError;
77
use crate::epsilon::queue::common::group::Group;
8+
use crate::Context;
89

910
#[rocket::post("/push", data = "<body>")]
10-
pub async fn push(body: Json<Group>, context: &State<Arc<Context>>) {
11+
pub async fn push(body: Json<Group>, context: &State<Arc<Context>>) -> Result<(), EpsilonError> {
1112
let queue_provider = context.get_queue_provider();
1213

14+
let queue_name = &body.queue;
1315
let queue_map = queue_provider.get_queues();
14-
let mut queue = queue_map.get(&body.queue).unwrap().write().await;
16+
17+
let mut queue = queue_map
18+
.get(queue_name)
19+
.ok_or(EpsilonError::QueueNotFoundError(queue_name.to_owned()))?
20+
.write()
21+
.await;
1522

1623
info!(
1724
"Player {} added to queue {}",
@@ -20,4 +27,6 @@ pub async fn push(body: Json<Group>, context: &State<Arc<Context>>) {
2027
);
2128

2229
queue.push(body.into_inner());
30+
31+
Ok(())
2332
}

src/epsilon/server/instances/routes.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,19 @@ pub async fn close(instance: &str, context: &State<Arc<Context>>) -> Result<(),
4444
}
4545

4646
#[rocket::post("/in_game/<instance>")]
47-
pub async fn in_game(instance: &str, context: &State<Arc<Context>>) {
47+
pub async fn in_game(instance: &str, context: &State<Arc<Context>>) -> Result<(), EpsilonError> {
4848
let instance_provider = context.get_instance_provider();
4949

5050
instance_provider
5151
.enable_in_game_instance(instance)
5252
.await
5353
.map_err(|_| {
5454
EpsilonError::ApiServerError(format!("Failed to set in game instance ({})", instance))
55-
})
56-
.unwrap();
55+
})?;
5756

5857
info!("An instance is now in game (name={})", instance);
58+
59+
Ok(())
5960
}
6061

6162
#[rocket::get("/get/<template>")]
@@ -90,8 +91,7 @@ pub async fn get_all(context: &State<Arc<Context>>) -> Result<String, EpsilonErr
9091
let instances = instance_provider
9192
.get_instances(&InstanceType::Server, None, None)
9293
.await
93-
.map_err(|_| EpsilonError::ApiServerError("Failed to get every instance".to_string()))
94-
.unwrap()
94+
.map_err(|_| EpsilonError::ApiServerError("Failed to get every instance".to_string()))?
9595
.into_iter();
9696

9797
let mut json_array: Vec<InstanceJson> = Vec::new();
@@ -112,5 +112,6 @@ pub async fn get_from_name(
112112
let instance_provider = context.get_instance_provider();
113113
let instance = instance_provider.get_instance(instance_name).await?;
114114

115-
Ok(serde_json::to_string(&instance.to_json().await?).unwrap())
115+
Ok(serde_json::to_string(&instance.to_json().await?)
116+
.map_err(|_| EpsilonError::ParseJsonError)?)
116117
}

src/epsilon/server/templates/template_provider.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl TemplateProvider {
7070
fn get_template_host(&self, route: &str) -> String {
7171
format!(
7272
"http://{}:8000/{}",
73-
env::var("HOST_TEMPLATE").unwrap(),
73+
env::var("HOST_TEMPLATE").expect("Failed to get HOST_TEMPLATE Environment"),
7474
route
7575
)
7676
}

src/tasks/queue_task.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,14 @@ impl Task for QueueTask {
6262
let group_size = group.players.len() as i32;
6363

6464
if group_size <= available_slots {
65-
available_slots -= group_size;
66-
67-
epsilon_api.send(SendToServer(group, instance.get_name()));
65+
match epsilon_api.send(SendToServer(group, instance.get_name()))
66+
{
67+
Ok(_) => {
68+
available_slots -= group_size;
69+
Ok(())
70+
}
71+
Err(e) => Err(e),
72+
}?
6873
}
6974
}
7075
}

0 commit comments

Comments
 (0)