Skip to content

Commit

Permalink
handling not resolvable url
Browse files Browse the repository at this point in the history
  • Loading branch information
burnout87 committed Oct 2, 2024
1 parent d0b647b commit 46af8a2
Showing 1 changed file with 81 additions and 42 deletions.
123 changes: 81 additions & 42 deletions cdci_data_analysis/analysis/drupal_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1496,55 +1496,94 @@ def resolve_name(local_name_resolver_url: str, external_name_resolver_url: str,
resolved_obj = {}
if name is not None:
quoted_name = urllib.parse.quote(name.strip())
res = requests.get(local_name_resolver_url.format(quoted_name))
local_name_resolver_url_formatted = local_name_resolver_url.format(quoted_name)
try:
res = requests.get(local_name_resolver_url_formatted)
if res.status_code == 200:
returned_resolved_obj = res.json()
if 'success' in returned_resolved_obj:
resolved_obj['name'] = name.replace('_', ' ')
if returned_resolved_obj['success']:
logger.info(f"object {name} successfully resolved")
if 'ra' in returned_resolved_obj:
resolved_obj['RA'] = float(returned_resolved_obj['ra'])
if 'dec' in returned_resolved_obj:
resolved_obj['DEC'] = float(returned_resolved_obj['dec'])
if 'object_ids' in returned_resolved_obj:
resolved_obj['object_ids'] = returned_resolved_obj['object_ids']
if 'object_type' in returned_resolved_obj:
resolved_obj['object_type'] = returned_resolved_obj['object_type']
resolved_obj['entity_portal_link'] = entities_portal_url.format(quoted_name)
resolved_obj['message'] = f'{name} successfully resolved'
elif not returned_resolved_obj['success']:
logger.info(f"resolution of the object {name} unsuccessful")
resolved_obj['message'] = f'{name} could not be resolved'
else:
logger.warning("There seems to be some problem in completing the request for the resolution of the object"
f" \"{name}\" using the local resolver.\n"
f"The request lead to the error {res.text}, "
"this might be due to an error in the url or the service "
"requested is currently not available. The external resolver will be used.")
if sentry_dsn is not None:
sentry.capture_message(f'Failed to resolve object "{name}" using the local resolver. '
f'URL: {local_name_resolver_url_formatted} '
f'Status Code: {res.status_code} '
f'Response: {res.text}')
except (ConnectionError,
requests.exceptions.ConnectionError,
requests.exceptions.Timeout) as e:
logger.warning(f'An exception occurred while trying to resolve the object "{name}" using the local resolver. '
f'using the url: {local_name_resolver_url_formatted}. Exception details: {str(e)}')
if sentry_dsn is not None:
sentry.capture_message(f'An exception occurred while trying to resolve the object "{name}" using the local resolver. '
f'URL: {local_name_resolver_url_formatted} '
f"Exception details: {str(e)}")
res = requests.get(external_name_resolver_url.format(quoted_name))
if res.status_code == 200:
returned_resolved_obj = res.json()
if 'success' in returned_resolved_obj:
resolved_obj['name'] = name.replace('_', ' ')
if returned_resolved_obj['success']:
logger.info(f"object {name} successfully resolved")
if 'ra' in returned_resolved_obj:
resolved_obj['RA'] = float(returned_resolved_obj['ra'])
if 'dec' in returned_resolved_obj:
resolved_obj['DEC'] = float(returned_resolved_obj['dec'])
if 'object_ids' in returned_resolved_obj:
resolved_obj['object_ids'] = returned_resolved_obj['object_ids']
if 'object_type' in returned_resolved_obj:
resolved_obj['object_type'] = returned_resolved_obj['object_type']
resolved_obj['entity_portal_link'] = entities_portal_url.format(quoted_name)
resolved_obj['message'] = f'{name} successfully resolved'
elif not returned_resolved_obj['success']:
logger.info(f"resolution of the object {name} unsuccessful")
root = ET.fromstring(res.text)
resolved_obj['name'] = name.replace('_', ' ')
resolver_tag = root.find('.//Resolver')
if resolver_tag is not None:
ra_tag = resolver_tag.find('.//jradeg')
dec_tag = resolver_tag.find('.//jdedeg')
if ra_tag is None or dec_tag is None:
info_tag = root.find('.//INFO')
resolved_obj['message'] = f'{name} could not be resolved'
if info_tag is not None:
message_info = info_tag.text
resolved_obj['message'] += f': {message_info}'
else:
resolved_obj['RA'] = float(ra_tag.text)
resolved_obj['DEC'] = float(dec_tag.text)
resolved_obj['entity_portal_link'] = entities_portal_url.format(quoted_name)
else:
warning_msg = ("There seems to be some problem in completing the request for the resolution of the object"
f" \"{name}\" using the external resolver.")
resolved_obj['message'] = f'{name} could not be resolved'
info_tag = root.find('.//INFO')
if info_tag is not None:
warning_msg += (f"The request lead to the error {info_tag.text}, "
"this might be due to an error in the name of the object that ha been provided.")
resolved_obj['message'] += f': {info_tag.text}'
logger.warning(warning_msg)
if sentry_dsn is not None:
sentry.capture_message(f'Failed to resolve object "{name}" using the remote resolver. '
f'URL: {local_name_resolver_url.format(quoted_name)} '
f'Status Code: {res.status_code} '
f'Response: {res.text}'
f"Info returned from the resolver: {resolved_obj['message']}")
else:
logger.warning("There seems to be some problem in completing the request for the resolution of the object"
f" \"{name}\" using the local resolver.\n"
f" \"{name}\" using the external resolver.\n"
f"The request lead to the error {res.text}, "
"this might be due to an error in the url or the service "
"requested is currently not available. The external resolver will be used.")
"requested is currently not available. The object could not be resolved.")
if sentry_dsn is not None:
sentry.capture_message(f'Issue in resolving the object {name} using the local resolver\n{res.text}')
res = requests.get(external_name_resolver_url.format(quoted_name))
if res.status_code == 200:
root = ET.fromstring(res.text)
resolver_tag = root.find('.//Resolver')
if resolver_tag is not None:
ra_tag = resolver_tag.find('.//jradeg')
resolved_obj['RA'] = float(ra_tag.text)

dec_tag = resolver_tag.find('.//jdedeg')
resolved_obj['DEC'] = float(dec_tag.text)
else:
logger.warning("There seems to be some problem in completing the request for the resolution of the object"
f" \"{name}\" using the external resolver.\n"
f"The request lead to the error {res.text}, "
"this might be due to an error in the url or the service "
"requested is currently not available. The object could not be resolved.")
if sentry_dsn is not None:
sentry.capture_message(f'Issue in resolving the object {name} using the external resolver\n{res.text}')
raise InternalError('issue when performing a request to the external resolver',
status_code=500,
payload={'drupal_helper_error_message': res.text})
sentry.capture_message(f'Failed to resolve object "{name}" using the remote resolver. '
f'URL: {local_name_resolver_url.format(quoted_name)} '
f'Status Code: {res.status_code} '
f'Response: {res.text}')
resolved_obj['message'] = f'{name} could not be resolved: {res.text}'
return resolved_obj


Expand Down

0 comments on commit 46af8a2

Please sign in to comment.