From 1244d0fafc3bf3873a2d11e1ade2b66066ef8350 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Mon, 18 Oct 2021 11:20:57 +0100 Subject: [PATCH] Returning error codes from post --- posts.py | 13 ++++++++++--- session.py | 14 +++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/posts.py b/posts.py index 9011d7d48..6e7878105 100644 --- a/posts.py +++ b/posts.py @@ -2095,10 +2095,12 @@ def threadSendPost(session, postJsonStr: str, federationList: [], if debug: print('Getting postJsonString for ' + inboxUrl) try: - postResult, unauthorized = \ + postResult, unauthorized, returnCode = \ postJsonString(session, postJsonStr, federationList, inboxUrl, signatureHeaderJson, debug) + if returnCode == 500: + break if debug: print('Obtained postJsonString for ' + inboxUrl + ' unauthorized: ' + str(unauthorized)) @@ -2410,12 +2412,17 @@ def sendPostViaServer(signingPrivateKeyPem: str, projectVersion: str, 'Authorization': authHeader } postDumps = json.dumps(postJsonObject) - postResult = \ + postResult, unauthorized, returnCode = \ postJsonString(session, postDumps, [], inboxUrl, headers, debug, 5, True) if not postResult: 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 if debug: diff --git a/session.py b/session.py index 5faade33f..577bc970c 100644 --- a/session.py +++ b/session.py @@ -296,7 +296,7 @@ def postJsonString(session, postJsonStr: str, headers: {}, debug: bool, 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 The second boolean returned is true if the send is unauthorized 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: if not quiet: print('WARN: error during postJsonString requests ' + str(e)) - return None, None + return None, None, 0 except SocketError as e: if not quiet and e.errno == errno.ECONNRESET: print('WARN: connection was reset during postJsonString') if not quiet: print('ERROR: postJsonString failed ' + inboxUrl + ' ' + postJsonStr + ' ' + str(headers)) - return None, None + return None, None, 0 except ValueError as e: if not quiet: 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 >= 400 and \ postResult.status_code <= 405 and \ @@ -330,14 +330,14 @@ def postJsonString(session, postJsonStr: str, print('WARN: Post to ' + inboxUrl + ' is unauthorized. Code ' + str(postResult.status_code)) - return False, True + return False, True, postResult.status_code else: if not quiet: print('WARN: Failed to post to ' + inboxUrl + ' with headers ' + str(headers) + ' status code ' + str(postResult.status_code)) - return False, False - return True, False + return False, False, postResult.status_code + return True, False, 0 def postImage(session, attachImageFilename: str, federationList: [],