Kyrylo Kravets
Kyrylo Kravets

Reputation: 543

What is the logic behind AWS Sagemaker error responses using boto3 python?

I am working with AWS Sagemaker API using boto3, python 3.10. One of my goals is to implement proper error handling for several cases to make method call idempotent.

However, while exploring the errors, I found that they are inconsistent.

So my question is that what logic is behind implementing such errors or where I can read about their implementation logic by AWS?

For example, I am trying to call API action for resources which do not exist:

Request:

import boto3
s = boto3.Session()
sm = s.client("sagemaker")

sm.delete_pipeline(PipelineName="tressdgfsd")

Response - expected, ResourceNotFound:

botocore.errorfactory.ResourceNotFound: An error occurred (ResourceNotFound) when calling the DeletePipeline operation: Pipeline '***' does not exist.

Request:

sm.list_trial_components(TrialName="sdfds")

Response - expected, ResourceNotFound:

botocore.errorfactory.ResourceNotFound: An error occurred (ResourceNotFound) when calling the ListTrialComponents operation: Trial '***' does not exist.

Request:

sm.delete_trial(TrialName='sdfgdsfgds')

Response - expected, ResourceNotFound:

An error occurred (ResourceNotFound) when calling the DeleteTrial operation: Trial '****' does not exist.

Request:

sm.delete_model(ModelName="testfdgdfgfd")

Response - NOT EXPECTED, ClientError:

botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the DeleteModel operation: Could not find model "***".

Request:

sm.delete_endpoint_config(EndpointConfigName="testdfgdfgdf")

Response - NOT EXPECTED, ClientError:

botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the DeleteEndpointConfig operation: Could not find endpoint configuration "***".

Request:

sm.delete_model_package_group(ModelPackageGroupName="testasfsd")

Response - NOT EXPECTED, ClientError:

botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the DeleteModelPackageGroup operation: ModelPackageGroup *** does not exist.

Request:

sm.delete_model_package(ModelPackageName="testasfsd")

Response - NOT EXPECTED, ClientError:

botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the DeleteModelPackage operation: ModelPackage *** does not exist.

To find more information,

Upvotes: 1

Views: 622

Answers (1)

Payton Staub
Payton Staub

Reputation: 602

You're right about the inconsistency. In general, newer SageMaker APIs will return ResourceNotFound (404) for cases like these, while older APIs may return a ValidationException (400).

You can determine which APIs do and don't return ResourceNotFound exceptions by checking the API documentation. For example, DeletePipeline has ResourceNotFound listed under its error types, while DeleteModelPackageGroup does not. If an API does not have ResourceNotFound listed in its error types, it will return a ValidationException instead.

Unfortunately for proper error handling you will need to handle this inconsistency, either by checking each API's documentation or, more easily, by writing some generic logic to handle this. For example:

try:
  # boto3_method_call()
except ResourceNotFound:
  # 404 logic
except ClientError as error:
  if error.response['Error']['Code'] == 'ValidationException':
    404_strings = ['does not exist', 'could not find']
    if any([x in error.response['Error']['Message'] for x in 404_strings]):
      # 404 logic  

Upvotes: 1

Related Questions