Skip to content

Commit 5c5346e

Browse files
committed
Implement error handling
Signed-off-by: declark1 <daniel.clark@ibm.com>
1 parent e45a89e commit 5c5346e

File tree

10 files changed

+142
-102
lines changed

10 files changed

+142
-102
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ rustls-webpki = "0.102.2"
2525
serde = { version = "1.0.200", features = ["derive"] }
2626
serde_json = "1.0.116"
2727
serde_yml = "0.0.5"
28-
thiserror = "1.0.59"
2928
tokio = { version = "1.37.0", features = ["rt", "rt-multi-thread", "parking_lot", "signal", "sync", "fs"] }
3029
tokio-stream = "0.1.14"
3130
tonic = { version = "0.11.0", features = ["tls"] }
@@ -34,6 +33,7 @@ tracing-subscriber = { version = "0.3.18", features = ["json", "env-filter"] }
3433
url = "2.5.0"
3534
validator = { version = "0.18.1", features = ["derive"] } # For API validation
3635
uuid = { version = "1.8.0", features = ["v4", "fast-rng"] }
36+
anyhow = "1.0.83"
3737

3838
[build-dependencies]
3939
tonic-build = "0.11.0"

src/clients.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(dead_code)]
22
use std::{collections::HashMap, time::Duration};
33

4+
use anyhow::{Context, Error};
45
use futures::future::try_join_all;
56
use ginepro::LoadBalancedChannel;
67
use url::Url;
@@ -26,18 +27,6 @@ pub const DEFAULT_DETECTOR_PORT: u16 = 8080;
2627
const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_secs(5);
2728
const DEFAULT_REQUEST_TIMEOUT: Duration = Duration::from_secs(10);
2829

29-
#[derive(Debug, thiserror::Error)]
30-
pub enum Error {
31-
#[error("model not found: {0}")]
32-
ModelNotFound(String),
33-
#[error(transparent)]
34-
ReqwestError(#[from] reqwest::Error),
35-
#[error(transparent)]
36-
TonicError(#[from] tonic::Status),
37-
#[error(transparent)]
38-
IoError(#[from] std::io::Error),
39-
}
40-
4130
#[derive(Clone)]
4231
pub enum GenerationClient {
4332
Tgis(TgisClient),
@@ -89,7 +78,11 @@ pub async fn create_http_clients(
8978
let identity = reqwest::Identity::from_pem(&cert_pem)?;
9079
builder = builder.use_rustls_tls().identity(identity);
9180
}
92-
let client = builder.build()?;
81+
let client = builder.build().with_context(|| {
82+
format!(
83+
"error creating http client, name={name}, service_config={service_config:?}"
84+
)
85+
})?;
9386
let client = HttpClient::new(base_url, client);
9487
Ok((name.clone(), client)) as Result<(String, HttpClient), Error>
9588
})
@@ -140,7 +133,7 @@ async fn create_grpc_clients<C>(
140133
if let Some(client_tls_config) = client_tls_config {
141134
builder = builder.with_tls(client_tls_config);
142135
}
143-
let channel = builder.channel().await.unwrap(); // TODO: handle error
136+
let channel = builder.channel().await.with_context(|| format!("error creating grpc client, name={name}, service_config={service_config:?}"))?;
144137
Ok((name.clone(), new(channel))) as Result<(String, C), Error>
145138
})
146139
.collect::<Vec<_>>();

src/clients/chunker.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use std::{collections::HashMap, pin::Pin};
22

3+
use anyhow::{Context, Error};
34
use futures::{Stream, StreamExt};
45
use ginepro::LoadBalancedChannel;
56
use tokio::sync::mpsc;
67
use tokio_stream::wrappers::ReceiverStream;
78
use tonic::Request;
89

9-
use super::{create_grpc_clients, Error};
10+
use super::create_grpc_clients;
1011
use crate::{
1112
config::ServiceConfig,
1213
pb::{
@@ -35,7 +36,7 @@ impl ChunkerClient {
3536
Ok(self
3637
.clients
3738
.get(model_id)
38-
.ok_or_else(|| Error::ModelNotFound(model_id.into()))?
39+
.context(format!("model not found, model_id={model_id}"))?
3940
.clone())
4041
}
4142

src/clients/detector.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::collections::HashMap;
22

3+
use anyhow::{Context, Error};
34
use serde::{Deserialize, Serialize};
45

5-
use super::{create_http_clients, Error, HttpClient};
6+
use super::{create_http_clients, HttpClient};
67
use crate::config::ServiceConfig;
78

89
const DETECTOR_ID_HEADER_NAME: &str = "detector-id";
@@ -23,7 +24,7 @@ impl DetectorClient {
2324
Ok(self
2425
.clients
2526
.get(model_id)
26-
.ok_or_else(|| Error::ModelNotFound(model_id.into()))?
27+
.context(format!("model not found, model_id={model_id}"))?
2728
.clone())
2829
}
2930

src/clients/nlp.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use std::collections::HashMap;
22

3+
use anyhow::{Context, Error};
34
use futures::StreamExt;
45
use ginepro::LoadBalancedChannel;
56
use tokio::sync::mpsc;
67
use tokio_stream::wrappers::ReceiverStream;
78
use tonic::Request;
89

9-
use super::{create_grpc_clients, Error};
10+
use super::create_grpc_clients;
1011
use crate::{
1112
config::ServiceConfig,
1213
pb::{
@@ -38,7 +39,7 @@ impl NlpClient {
3839
Ok(self
3940
.clients
4041
.get(model_id)
41-
.ok_or_else(|| Error::ModelNotFound(model_id.into()))?
42+
.context(format!("model not found, model_id={model_id}"))?
4243
.clone())
4344
}
4445

src/clients/tgis.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use std::collections::HashMap;
22

3+
use anyhow::{Context, Error};
34
use futures::StreamExt;
45
use ginepro::LoadBalancedChannel;
56
use tokio::sync::mpsc;
67
use tokio_stream::wrappers::ReceiverStream;
78

8-
use super::{create_grpc_clients, Error};
9+
use super::create_grpc_clients;
910
use crate::{
1011
config::ServiceConfig,
1112
pb::fmaas::{
@@ -36,7 +37,7 @@ impl TgisClient {
3637
Ok(self
3738
.clients
3839
.get(model_id)
39-
.ok_or_else(|| Error::ModelNotFound(model_id.into()))?
40+
.context(format!("model not found, model_id={model_id}"))?
4041
.clone())
4142
}
4243

src/config.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,10 @@ impl OrchestratorConfig {
108108
todo!()
109109
}
110110

111-
pub fn get_chunker_id(&self, detector_id: &str) -> String {
112-
self.detectors.get(detector_id).unwrap().chunker_id.clone()
111+
pub fn get_chunker_id(&self, detector_id: &str) -> Option<String> {
112+
self.detectors
113+
.get(detector_id)
114+
.map(|detector_config| detector_config.chunker_id.clone())
113115
}
114116
}
115117

@@ -126,8 +128,9 @@ fn service_tls_name_to_config(
126128

127129
#[cfg(test)]
128130
mod tests {
131+
use anyhow::Error;
132+
129133
use super::*;
130-
use crate::Error;
131134

132135
#[test]
133136
fn test_deserialize_config() -> Result<(), Error> {

src/lib.rs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,8 @@
11
#![allow(clippy::iter_kv_map, clippy::enum_variant_names)]
22

3-
use axum::{http::StatusCode, Json};
4-
53
mod clients;
64
mod config;
75
mod models;
86
mod orchestrator;
97
mod pb;
108
pub mod server;
11-
12-
#[derive(Debug, thiserror::Error)]
13-
pub enum Error {
14-
#[error(transparent)]
15-
ClientError(#[from] crate::clients::Error),
16-
#[error(transparent)]
17-
IoError(#[from] std::io::Error),
18-
#[error(transparent)]
19-
YamlError(#[from] serde_yml::Error),
20-
}
21-
22-
// TODO: create better errors and properly convert
23-
impl From<Error> for (StatusCode, Json<String>) {
24-
fn from(value: Error) -> Self {
25-
use Error::*;
26-
match value {
27-
ClientError(error) => match error {
28-
clients::Error::ModelNotFound(message) => {
29-
(StatusCode::UNPROCESSABLE_ENTITY, Json(message))
30-
}
31-
clients::Error::ReqwestError(error) => {
32-
(StatusCode::INTERNAL_SERVER_ERROR, Json(error.to_string()))
33-
}
34-
clients::Error::TonicError(error) => {
35-
(StatusCode::INTERNAL_SERVER_ERROR, Json(error.to_string()))
36-
}
37-
clients::Error::IoError(_) => todo!(),
38-
},
39-
IoError(_) => todo!(),
40-
YamlError(_) => todo!(),
41-
}
42-
}
43-
}

0 commit comments

Comments
 (0)