Skip to content

Commit

Permalink
Adding additional error handling to upload_files_to_microservice func…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
jamiehewitt15 committed Jul 31, 2023
1 parent 8f2f3cc commit e9d8402
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
1 change: 0 additions & 1 deletion server/oceandbs/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class OceandbsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'oceandbs'

print(f"AppConfig: {AppConfig}")
def ready(self):
from . import tasks
tasks.start()
29 changes: 23 additions & 6 deletions server/oceandbs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from web3 import Web3
from web3.middleware import geth_poa_middleware
from eth_account.messages import encode_defunct
from requests.exceptions import RequestException

from .models import File

Expand Down Expand Up @@ -93,23 +94,39 @@ def create_allowance(quote, user_private_key, abi):
print(f"Unexpected error: {e}")
return Response(f"Unexpected error: {e}", status=500)

print(f"Transaction completed successfully")
return Response("Transaction completed successfully.", status=200)


# This function is used to upload the files to the target microservice
def upload_files_to_microservice(quote, params, files_reference):

# Initial data validations
if not quote or not hasattr(quote, 'storage') or not hasattr(quote.storage, 'url') or not hasattr(quote, 'quoteId'):
raise ValueError("Invalid quote object provided.")

if not params or 'nonce' not in params or 'signature' not in params:
raise ValueError("Invalid params provided.")

data = {
"quoteId": quote.quoteId,
"nonce": params['nonce'][0],
"signature": params['signature'][0],
"files": files_reference
}

response = requests.post(
quote.storage.url + 'upload/?quoteId=' +
str(quote.quoteId) + '&nonce=' +
data['nonce'] + '&signature=' + data['signature'],
data
)
url = quote.storage.url + 'upload/?quoteId=' + str(quote.quoteId) + '&nonce=' + data['nonce'] + '&signature=' + data['signature']

try:
print(f"Sending request to microservice url: {url}")
response = requests.post(url, data)
response.raise_for_status() # This will raise an HTTPError if the HTTP request returned an unsuccessful status code
except RequestException as e:
# Extract more detailed message from the response content, if available
detailed_message = e.response.text if hasattr(e, 'response') and hasattr(e.response, 'text') else "No detailed message provided."
raise RuntimeError(f"Error occurred while making the request: {str(e)}. Detailed message: {detailed_message}")
except Exception as e: # Catches any unforeseen exceptions
raise RuntimeError(f"Unexpected error occurred: {str(e)}")

return response

Expand Down
18 changes: 11 additions & 7 deletions server/oceandbs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,20 +458,24 @@ def post(self, request, format="multipart"):
return Response(f"Error uploading to micro-service: {str(e)}", status=500)

if (response.status_code == 200):
print("File upload to microservice succeeded.")
quote.status = UPLOAD_CODE[5][0]
try:
quote.save()
except Exception as e:
return Response(f"Error saving quote after successful upload: {str(e)}", status=500)
return Response("File upload succeeded.", status=200)

else:
print(f"Microservice upload failed with status code: {response.status_code}. Error message: {response.content}")
quote.status = UPLOAD_CODE[6][0]
try:
quote.save()
except Exception as e:
return Response(f"Error updating quote status after failed upload: {str(e)}", status=500)

return Response(f"Microservice upload failed with status code: {response.status_code}", status=401)

quote.status = UPLOAD_CODE[6][0]
try:
quote.save()
except Exception as e:
return Response(f"Error updating quote status after failed upload: {str(e)}", status=500)

return Response(f"Microservice upload failed with status code: {response.status_code}", status=401)


class QuoteLink(APIView):
Expand Down

0 comments on commit e9d8402

Please sign in to comment.