Returning error codes from post

main
Bob Mottram 2021-10-18 11:20:57 +01:00
parent ee11752b0b
commit 1244d0fafc
2 changed files with 17 additions and 10 deletions

View File

@ -2095,10 +2095,12 @@ def threadSendPost(session, postJsonStr: str, federationList: [],
if debug: if debug:
print('Getting postJsonString for ' + inboxUrl) print('Getting postJsonString for ' + inboxUrl)
try: try:
postResult, unauthorized = \ postResult, unauthorized, returnCode = \
postJsonString(session, postJsonStr, federationList, postJsonString(session, postJsonStr, federationList,
inboxUrl, signatureHeaderJson, inboxUrl, signatureHeaderJson,
debug) debug)
if returnCode == 500:
break
if debug: if debug:
print('Obtained postJsonString for ' + inboxUrl + print('Obtained postJsonString for ' + inboxUrl +
' unauthorized: ' + str(unauthorized)) ' unauthorized: ' + str(unauthorized))
@ -2410,12 +2412,17 @@ def sendPostViaServer(signingPrivateKeyPem: str, projectVersion: str,
'Authorization': authHeader 'Authorization': authHeader
} }
postDumps = json.dumps(postJsonObject) postDumps = json.dumps(postJsonObject)
postResult = \ postResult, unauthorized, returnCode = \
postJsonString(session, postDumps, [], postJsonString(session, postDumps, [],
inboxUrl, headers, debug, 5, True) inboxUrl, headers, debug, 5, True)
if not postResult: if not postResult:
if debug: if debug:
print('DEBUG: POST failed for c2s to ' + inboxUrl) if unauthorized:
print('DEBUG: POST failed for c2s to ' +
inboxUrl + ' unathorized')
else:
print('DEBUG: POST failed for c2s to '
+ inboxUrl + ' return code ' + str(returnCode))
return 5 return 5
if debug: if debug:

View File

@ -296,7 +296,7 @@ def postJsonString(session, postJsonStr: str,
headers: {}, headers: {},
debug: bool, debug: bool,
timeoutSec: int = 30, timeoutSec: int = 30,
quiet: bool = False) -> (bool, bool): quiet: bool = False) -> (bool, bool, int):
"""Post a json message string to the inbox of another person """Post a json message string to the inbox of another person
The second boolean returned is true if the send is unauthorized The second boolean returned is true if the send is unauthorized
NOTE: Here we post a string rather than the original json so that NOTE: Here we post a string rather than the original json so that
@ -310,18 +310,18 @@ def postJsonString(session, postJsonStr: str,
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
if not quiet: if not quiet:
print('WARN: error during postJsonString requests ' + str(e)) print('WARN: error during postJsonString requests ' + str(e))
return None, None return None, None, 0
except SocketError as e: except SocketError as e:
if not quiet and e.errno == errno.ECONNRESET: if not quiet and e.errno == errno.ECONNRESET:
print('WARN: connection was reset during postJsonString') print('WARN: connection was reset during postJsonString')
if not quiet: if not quiet:
print('ERROR: postJsonString failed ' + inboxUrl + ' ' + print('ERROR: postJsonString failed ' + inboxUrl + ' ' +
postJsonStr + ' ' + str(headers)) postJsonStr + ' ' + str(headers))
return None, None return None, None, 0
except ValueError as e: except ValueError as e:
if not quiet: if not quiet:
print('WARN: error during postJsonString ' + str(e)) print('WARN: error during postJsonString ' + str(e))
return None, None return None, None, 0
if postResult.status_code < 200 or postResult.status_code > 202: if postResult.status_code < 200 or postResult.status_code > 202:
if postResult.status_code >= 400 and \ if postResult.status_code >= 400 and \
postResult.status_code <= 405 and \ postResult.status_code <= 405 and \
@ -330,14 +330,14 @@ def postJsonString(session, postJsonStr: str,
print('WARN: Post to ' + inboxUrl + print('WARN: Post to ' + inboxUrl +
' is unauthorized. Code ' + ' is unauthorized. Code ' +
str(postResult.status_code)) str(postResult.status_code))
return False, True return False, True, postResult.status_code
else: else:
if not quiet: if not quiet:
print('WARN: Failed to post to ' + inboxUrl + print('WARN: Failed to post to ' + inboxUrl +
' with headers ' + str(headers) + ' with headers ' + str(headers) +
' status code ' + str(postResult.status_code)) ' status code ' + str(postResult.status_code))
return False, False return False, False, postResult.status_code
return True, False return True, False, 0
def postImage(session, attachImageFilename: str, federationList: [], def postImage(session, attachImageFilename: str, federationList: [],