The update_service(...) method of the ECS client returns a ServiceNotFoundException if there is no service with the given serviceArn.
What is the best way to catch this specific exception?
Initially, I was checking if the string ServiceNotFound occurs in str(e) but that is a very hacky way of doing things. So, I am trying to catch the exception using ClientError but it doesn't work.
try:
client = boto3.client('ecs')
client.update_service(cluster=cluster, serviceArn=service_arn)
except ClientError as e:
if "ServiceNotFound" in e.response['Error']['Code']:
logger.error("Service not found!")
This method does not work - although a ClientError occurs, the string "ServiceNotFound" is not contained in the e.response['Error']['Code'].
I tried to find documentation regarding this but was unsuccessful in finding anything particularly useful.
I went through some service-2.json files but their format didn't help me to understand what error code I must look for exactly for this exception.
Can I get some help regarding what are the possible error codes for various types of clients? Thanks! :)
I'm having this issue as well, it's not clear which module to import to find this particular exception
The clearest way to catch those exceptions is like this:
import boto3
ecs = boto3.client('ecs')
try:
ecs.update_service(cluster='test', service='bad_service_arn')
except ecs.exceptions.ClusterNotFoundException as e:
print(e)
except ecs.exceptions.ServiceNotFoundException as e:
print(e)
Output:
$ bad_cluster.py
An error occurred (ClusterNotFoundException) when calling the UpdateService operation: Cluster not found.
$ bad_service.py
An error occurred (ServiceNotFoundException) when calling the UpdateService operation: Service not found.
ok, that makes sense.
From the docs it wasn't clear where to import the exceptions from. I tried searching for the text ServiceNotFoundException in the codebase and couldn't find it either...
Thank you @stealthycoin
Most helpful comment
The clearest way to catch those exceptions is like this:
Output: