Can I handle errors from raw SearchQueries?

I wanted to do something like this:

try:
    my_results = Records.objects.filter(my_search_vector=SearchQuery(search_text, config='english', search_type='raw'))
except Exception as e:
    my_results = Records.objects.filter(my_search_vector=SearchQuery(search_text, config='english', search_type='websearch'))

The idea being that raw search queries can throw syntax errors if the query is malformed, but we want to allow raw search queries when possible because they allow grouping of terms where websearch cannot. However, we also want the websearch functionality where it never throws a syntax error, but will always return a query set.

Is there a way to catch errors from SearchQuery and then run a backup SearchQuery?

.filter() doesn’t perform any querying because QuerySets are lazy

You can force the query to execute with e.g. my_results = list(Records.objects.filter(...)), and catch the exception then. Use a specific exception type rather than Exception.