Reputation: 3282
I have a model that looks like this:
class Topic(models.Model):
name = models.CharField(max_length=50, unique=True)
def indexing(self):
try:
connections.create_connection()
obj = TopicIndex(
meta={'id': self.id},
name=self.name,
)
obj.save()
return obj.to_dict(include_meta=True)
except ConnectionError:
raise ValidationError("Something is wrong.")
Whenever a new Topic is saved, it also saves it into ElasticSearch. However, let's say ElasticSearch is down. If I save a new Topic, I'll start getting errors like this:
elasticsearch.exceptions.ConnectionError: ConnectionError(: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it)
The error makes sense, however, I never catch it. "Something is wrong." never gets displayed. Instead, a connection tries to be made over and over again.
Upvotes: 1
Views: 1374
Reputation: 88629
You have to catch the elasticsearch
's exception class as,
from elasticsearch.exceptions import ConnectionError as ElasticConnectionError
class Topic(models.Model):
name = models.CharField(max_length=50, unique=True)
def indexing(self):
try:
# your try clause
except ElasticConnectionError:
raise ValidationError("Something is wrong.")
In your case, you were catching the Python's built-in ConnectionError
exception
Upvotes: 2