Skip to content

Commit 366dfd7

Browse files
committed
change model exception from sigv4 client
1 parent 5e54128 commit 366dfd7

File tree

1 file changed

+34
-33
lines changed
  • crates/chat-cli/src/api_client

1 file changed

+34
-33
lines changed

crates/chat-cli/src/api_client/mod.rs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ impl ApiClient {
338338
status_code,
339339
});
340340
}
341+
341342
if is_quota_breach {
342343
return Err(ApiClientError::QuotaBreach {
343344
message: "quota has reached its limit",
@@ -376,36 +377,34 @@ impl ApiClient {
376377
{
377378
Ok(response) => Ok(SendMessageOutput::QDeveloper(response)),
378379
Err(err) => {
379-
use amzn_qdeveloper_streaming_client::operation::send_message::SendMessageError::ThrottlingError as OperationThrottlingError;
380-
use amzn_qdeveloper_streaming_client::types::ThrottlingExceptionReason;
381-
use amzn_qdeveloper_streaming_client::types::error::ThrottlingError;
382-
383380
let status_code = err.raw_response().map(|res| res.status().as_u16());
384381
let is_quota_breach = status_code.is_some_and(|status| status == 429);
385382
let is_context_window_overflow = err.as_service_error().is_some_and(|err| {
386383
matches!(err, err if err.meta().code() == Some("ValidationException") && err.meta().message() == Some("Input is too long."))
387384
});
388385

389-
let is_model_unavailable =
390-
// Handling the updated error response
391-
err.as_service_error().is_some_and(|err| {
392-
matches!(
393-
err,
394-
OperationThrottlingError(ThrottlingError {
395-
reason: Some(ThrottlingExceptionReason::InsufficientModelCapacity),
396-
..
397-
})
398-
)
399-
})
400-
// Legacy error response
386+
let is_model_unavailable = {
387+
// check if ThrottlingException
388+
let is_throttling_exception = err
389+
.as_service_error()
390+
.is_some_and(|service_err| service_err.meta().code() == Some("ThrottlingException"));
391+
392+
// check if the response contains INSUFFICIENT_MODEL_CAPACITY
393+
let has_insufficient_capacity = err
394+
.raw_response()
395+
.and_then(|resp| resp.body().bytes())
396+
.and_then(|bytes| String::from_utf8(bytes.to_vec()).ok())
397+
.is_some_and(|body| body.contains("INSUFFICIENT_MODEL_CAPACITY"));
398+
399+
(is_throttling_exception && has_insufficient_capacity)
400+
// Legacy error response fallback
401401
|| (model_id_opt.is_some()
402-
&& status_code.is_some_and(|status| status == 500)
403-
&& err.as_service_error().is_some_and(|err| {
404-
err.meta().message()
405-
== Some(
406-
"Encountered unexpectedly high load when processing the request, please try again.",
407-
)
408-
}));
402+
&& status_code.is_some_and(|status| status == 500)
403+
&& err.as_service_error().is_some_and(|err| {
404+
err.meta().message() == Some(
405+
"Encountered unexpectedly high load when processing the request, please try again.",
406+
)}))
407+
};
409408

410409
let is_monthly_limit_err = err
411410
.raw_response()
@@ -416,6 +415,18 @@ impl ApiClient {
416415
})
417416
.unwrap_or(false);
418417

418+
// Both ModelOverloadedError and QuotaBreach return 429,
419+
// so check is_model_unavailable first.
420+
if is_model_unavailable {
421+
return Err(ApiClientError::ModelOverloadedError {
422+
request_id: err
423+
.as_service_error()
424+
.and_then(|err| err.meta().request_id())
425+
.map(|s| s.to_string()),
426+
status_code,
427+
});
428+
}
429+
419430
if is_quota_breach {
420431
return Err(ApiClientError::QuotaBreach {
421432
message: "quota has reached its limit",
@@ -427,16 +438,6 @@ impl ApiClient {
427438
return Err(ApiClientError::ContextWindowOverflow { status_code });
428439
}
429440

430-
if is_model_unavailable {
431-
return Err(ApiClientError::ModelOverloadedError {
432-
request_id: err
433-
.as_service_error()
434-
.and_then(|err| err.meta().request_id())
435-
.map(|s| s.to_string()),
436-
status_code,
437-
});
438-
}
439-
440441
if is_monthly_limit_err {
441442
return Err(ApiClientError::MonthlyLimitReached { status_code });
442443
}

0 commit comments

Comments
 (0)