Skip to content

Commit 792133e

Browse files
committed
Update server::Error variants and conversion, rename Invalid vto NotFound and return 404, drop is_validation_error helper
Signed-off-by: declark1 <daniel.clark@ibm.com>
1 parent 65e6215 commit 792133e

File tree

8 files changed

+39
-56
lines changed

8 files changed

+39
-56
lines changed

src/clients.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ pub enum Error {
3434
Grpc(#[from] tonic::Status),
3535
#[error("{0}")]
3636
Http(#[from] reqwest::Error),
37-
#[error("invalid model id: {model_id}")]
38-
InvalidModelId { model_id: String },
37+
#[error("model not found: {model_id}")]
38+
ModelNotFound { model_id: String },
3939
}
4040

4141
impl Error {
@@ -61,18 +61,10 @@ impl Error {
6161
Some(code) => code,
6262
None => StatusCode::INTERNAL_SERVER_ERROR,
6363
},
64-
// Return 422 for invalid model id
65-
Error::InvalidModelId { .. } => StatusCode::UNPROCESSABLE_ENTITY,
64+
// Return 404 for model not found
65+
Error::ModelNotFound { .. } => StatusCode::NOT_FOUND,
6666
}
6767
}
68-
69-
/// Returns true for validation-type errors (400/422) and false for other types.
70-
pub fn is_validation_error(&self) -> bool {
71-
matches!(
72-
self.status_code(),
73-
StatusCode::BAD_REQUEST | StatusCode::UNPROCESSABLE_ENTITY
74-
)
75-
}
7668
}
7769

7870
#[derive(Clone)]

src/clients/chunker.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl ChunkerClient {
3535
Ok(self
3636
.clients
3737
.get(model_id)
38-
.ok_or_else(|| Error::InvalidModelId {
38+
.ok_or_else(|| Error::ModelNotFound {
3939
model_id: model_id.to_string(),
4040
})?
4141
.clone())

src/clients/detector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl DetectorClient {
2222
Ok(self
2323
.clients
2424
.get(model_id)
25-
.ok_or_else(|| Error::InvalidModelId {
25+
.ok_or_else(|| Error::ModelNotFound {
2626
model_id: model_id.to_string(),
2727
})?
2828
.clone())

src/clients/nlp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl NlpClient {
3838
Ok(self
3939
.clients
4040
.get(model_id)
41-
.ok_or_else(|| Error::InvalidModelId {
41+
.ok_or_else(|| Error::ModelNotFound {
4242
model_id: model_id.to_string(),
4343
})?
4444
.clone())

src/clients/tgis.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl TgisClient {
3535
Ok(self
3636
.clients
3737
.get(model_id)
38-
.ok_or_else(|| Error::InvalidModelId {
38+
.ok_or_else(|| Error::ModelNotFound {
3939
model_id: model_id.to_string(),
4040
})?
4141
.clone())

src/orchestrator.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ async fn chunk_and_detect(
189189
let chunker_id =
190190
ctx.config
191191
.get_chunker_id(detector_id)
192-
.ok_or_else(|| Error::InvalidDetectorId {
192+
.ok_or_else(|| Error::DetectorNotFound {
193193
detector_id: detector_id.clone(),
194194
})?;
195195
Ok::<String, Error>(chunker_id)
@@ -235,11 +235,12 @@ async fn detect(
235235
let ctx = ctx.clone();
236236
let detector_id = detector_id.clone();
237237
let detector_params = detector_params.clone();
238-
let chunker_id = ctx.config.get_chunker_id(&detector_id).ok_or_else(|| {
239-
Error::InvalidDetectorId {
240-
detector_id: detector_id.clone(),
241-
}
242-
})?;
238+
let chunker_id =
239+
ctx.config
240+
.get_chunker_id(&detector_id)
241+
.ok_or_else(|| Error::DetectorNotFound {
242+
detector_id: detector_id.clone(),
243+
})?;
243244
let chunks = chunks.get(&chunker_id).unwrap().clone();
244245
Ok(tokio::spawn(async move {
245246
handle_detection_task(ctx, detector_id, detector_params, chunks).await

src/orchestrator/errors.rs

+2-24
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
use reqwest::StatusCode;
2-
31
use crate::clients;
42

53
/// Orchestrator errors.
64
#[derive(Debug, thiserror::Error)]
75
pub enum Error {
8-
#[error("invalid detector: detector_id={detector_id}")]
9-
InvalidDetectorId { detector_id: String },
10-
#[error("invalid chunker: chunker_id={chunker_id}")]
11-
InvalidChunkerId { chunker_id: String },
6+
#[error("detector not found: {detector_id}")]
7+
DetectorNotFound { detector_id: String },
128
#[error("detector request failed for detector_id={detector_id}: {error}")]
139
DetectorRequestFailed {
1410
detector_id: String,
@@ -35,24 +31,6 @@ pub enum Error {
3531
Other(String),
3632
}
3733

38-
impl Error {
39-
/// Returns true for validation-type errors and false for other types.
40-
pub fn is_validation_error(&self) -> bool {
41-
use Error::*;
42-
match self {
43-
InvalidDetectorId { .. } | InvalidChunkerId { .. } => true,
44-
DetectorRequestFailed { error, .. }
45-
| ChunkerRequestFailed { error, .. }
46-
| GenerateRequestFailed { error, .. }
47-
| TokenizeRequestFailed { error, .. } => matches!(
48-
error.status_code(),
49-
StatusCode::BAD_REQUEST | StatusCode::UNPROCESSABLE_ENTITY
50-
),
51-
_ => false,
52-
}
53-
}
54-
}
55-
5634
impl From<tokio::task::JoinError> for Error {
5735
fn from(error: tokio::task::JoinError) -> Self {
5836
if error.is_cancelled() {

src/server.rs

+22-10
Original file line numberDiff line numberDiff line change
@@ -136,21 +136,32 @@ async fn shutdown_signal() {
136136
}
137137

138138
/// High-level errors to return to clients.
139-
/// Validation errors are forwarded from downstream clients.
140139
#[derive(Debug, thiserror::Error)]
141140
pub enum Error {
142141
#[error("{0}")]
143-
ValidationError(String),
142+
Validation(String),
143+
#[error("{0}")]
144+
NotFound(String),
144145
#[error("unexpected error occured while processing request")]
145-
UnexpectedError,
146+
Unexpected,
146147
}
147148

148149
impl From<orchestrator::Error> for Error {
149150
fn from(error: orchestrator::Error) -> Self {
150-
if error.is_validation_error() {
151-
Self::ValidationError(error.to_string())
152-
} else {
153-
Self::UnexpectedError
151+
use orchestrator::Error::*;
152+
match error {
153+
DetectorNotFound { .. } => Self::NotFound(error.to_string()),
154+
DetectorRequestFailed { error, .. }
155+
| ChunkerRequestFailed { error, .. }
156+
| GenerateRequestFailed { error, .. }
157+
| TokenizeRequestFailed { error, .. } => match error.status_code() {
158+
StatusCode::BAD_REQUEST | StatusCode::UNPROCESSABLE_ENTITY => {
159+
Self::Validation(error.to_string())
160+
}
161+
StatusCode::NOT_FOUND => Self::NotFound(error.to_string()),
162+
_ => Self::Unexpected,
163+
},
164+
_ => Self::Unexpected,
154165
}
155166
}
156167
}
@@ -159,12 +170,13 @@ impl IntoResponse for Error {
159170
fn into_response(self) -> Response {
160171
use Error::*;
161172
let (code, message) = match self {
162-
ValidationError(_) => (StatusCode::UNPROCESSABLE_ENTITY, self.to_string()),
163-
UnexpectedError => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
173+
Validation(_) => (StatusCode::UNPROCESSABLE_ENTITY, self.to_string()),
174+
NotFound(_) => (StatusCode::NOT_FOUND, self.to_string()),
175+
Unexpected => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
164176
};
165177
let error = serde_json::json!({
166178
"code": code.as_u16(),
167-
"message": message,
179+
"details": message,
168180
});
169181
(code, Json(error)).into_response()
170182
}

0 commit comments

Comments
 (0)