Skip to content

Commit

Permalink
fix(instance): send error instead of panick
Browse files Browse the repository at this point in the history
Signed-off-by: Mauran <[email protected]>
  • Loading branch information
thomas-mauran committed Aug 31, 2023
1 parent 60e1e61 commit d891879
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions controller/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ pub enum ApiError {

#[error("Scheduling error")]
SchedulingError(#[from] tonic::Status),

#[error("Instance not created")]
InstanceNotCreated{message: String},
}

impl IntoResponse for ApiError {
Expand All @@ -32,6 +35,7 @@ impl IntoResponse for ApiError {
ApiError::SerializationError(e) => (StatusCode::BAD_REQUEST, e.to_string()),
ApiError::DatabaseError(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()),
ApiError::SchedulingError(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()),
ApiError::InstanceNotCreated{message} => (StatusCode::BAD_REQUEST, message.to_string()),
};

let payload = json!({
Expand Down
9 changes: 7 additions & 2 deletions controller/src/routes/instances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::types::instance_request::InstanceRequest;
use crate::types::instance_status::InstanceStatus;
use axum::extract::Path;
use axum::Json;
use log::{error, trace};
use log::{trace, warn, error};
use orka_proto::scheduler_controller::{SchedulingRequest, Workload, WorkloadInstance};
use serde_json::{self, json, Value};
use validator::Validate;
Expand Down Expand Up @@ -94,8 +94,13 @@ pub async fn post_instance(body: String) -> anyhow::Result<Json<Value>, ApiError

let mut stream = client.schedule_workload(request).await?;

stream.message().await.map_err(|e|{
warn!("Error while creating the instance: {:?}", e);
ApiError::InstanceNotCreated{message: format!("Instance not created {:?}", e)}
})?;

tokio::spawn(async move {
while let Some(status) = stream.message().await.unwrap() {
while let Ok(Some(status)) = stream.message().await {
trace!("STATUS={:?}", status);
let result = DB_BATCH.lock().unwrap().batch.set(
&status.instance_id,
Expand Down

0 comments on commit d891879

Please sign in to comment.