Skip to content

Commit

Permalink
compiler: log and retry on json decode error
Browse files Browse the repository at this point in the history
  • Loading branch information
seljin committed Dec 2, 2021
1 parent 6fe0987 commit 5e54a2d
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions rest_models/backend/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
import re
from collections import defaultdict, namedtuple
from json import JSONDecodeError

import six
from django.core.exceptions import ImproperlyConfigured
Expand Down Expand Up @@ -990,13 +991,15 @@ def execute_sql(self, result_type=MULTI, chunked_fetch=False, chunk_size=None):

pk, params = self.build_params_and_pk()
url = get_resource_path(self.query.model, pk)
response = self.connection.cursor().get(
url,
params=params
)
self.raise_on_response(url, params, response)
response = self.make_request(params, url)

try:
json = response.json()
except JSONDecodeError:
extra = {'params': params, 'response': response}
logger.error('json decode error while calling {}; retrying'.format(url), extra=extra)
json = self.make_request(params, url).json()

json = response.json()
meta = self.get_meta(json, response)
if meta:
# pagination and others thing
Expand Down Expand Up @@ -1038,6 +1041,14 @@ def next_from_query():
result = self.result_iter(response_reader)
return result

def make_request(self, params, url):
response = self.connection.cursor().get(
url,
params=params
)
self.raise_on_response(url, params, response)
return response


class SQLInsertCompiler(SQLCompiler):
def resolve_data_n_files(self, obj):
Expand Down

0 comments on commit 5e54a2d

Please sign in to comment.