More exception handling

main
Bob Mottram 2020-06-23 22:39:19 +01:00
parent 306af20d45
commit b7653b9eb1
2 changed files with 32 additions and 10 deletions

View File

@ -6,7 +6,6 @@ __maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net" __email__ = "bob@freedombone.net"
__status__ = "Production" __status__ = "Production"
import random
import json import json
import html import html
import datetime import datetime
@ -14,6 +13,7 @@ import os
import shutil import shutil
import sys import sys
import time import time
from socket import error as SocketError
from time import gmtime, strftime from time import gmtime, strftime
from collections import OrderedDict from collections import OrderedDict
from threads import threadWithTrace from threads import threadWithTrace
@ -2173,7 +2173,7 @@ def sendToFollowersThread(session, baseDir: str,
print('WARN: socket error while starting ' + print('WARN: socket error while starting ' +
'thread to send to followers. ' + str(e)) 'thread to send to followers. ' + str(e))
return None return None
except BaseException: except ValueError as e:
print('WARN: error while starting ' + print('WARN: error while starting ' +
'thread to send to followers. ' + str(e)) 'thread to send to followers. ' + str(e))
return None return None

View File

@ -20,8 +20,8 @@ def createSession(proxyType: str):
session = None session = None
try: try:
session = requests.session() session = requests.session()
except ValueError as e: except requests.exceptions.RequestException as e:
print('WARN: error during createSession') print('WARN: requests error during createSession')
print(e) print(e)
return None return None
except SocketError as e: except SocketError as e:
@ -31,6 +31,10 @@ def createSession(proxyType: str):
print('WARN: socket error during createSession') print('WARN: socket error during createSession')
print(e) print(e)
return None return None
except ValueError as e:
print('WARN: error during createSession')
print(e)
return None
if not session: if not session:
return None return None
if proxyType == 'tor': if proxyType == 'tor':
@ -70,6 +74,11 @@ def getJson(session, url: str, headers: {}, params: {},
try: try:
result = session.get(url, headers=sessionHeaders, params=sessionParams) result = session.get(url, headers=sessionHeaders, params=sessionParams)
return result.json() return result.json()
except requests.exceptions.RequestException as e:
print('ERROR: getJson failed\nurl: ' + str(url) + '\n' +
'headers: ' + str(sessionHeaders) + '\n' +
'params: ' + str(sessionParams) + '\n')
print(e)
except ValueError as e: except ValueError as e:
print('ERROR: getJson failed\nurl: ' + str(url) + '\n' + print('ERROR: getJson failed\nurl: ' + str(url) + '\n' +
'headers: ' + str(sessionHeaders) + '\n' + 'headers: ' + str(sessionHeaders) + '\n' +
@ -99,8 +108,8 @@ def postJson(session, postJsonObject: {}, federationList: [],
session.post(url=inboxUrl, session.post(url=inboxUrl,
data=json.dumps(postJsonObject), data=json.dumps(postJsonObject),
headers=headers) headers=headers)
except ValueError as e: except requests.exceptions.RequestException as e:
print('ERROR: postJson failed ' + inboxUrl + ' ' + print('ERROR: postJson requests failed ' + inboxUrl + ' ' +
json.dumps(postJsonObject) + ' ' + str(headers)) json.dumps(postJsonObject) + ' ' + str(headers))
print(e) print(e)
return None return None
@ -108,6 +117,11 @@ def postJson(session, postJsonObject: {}, federationList: [],
if e.errno == errno.ECONNRESET: if e.errno == errno.ECONNRESET:
print('WARN: connection was reset during postJson') print('WARN: connection was reset during postJson')
return None return None
except ValueError as e:
print('ERROR: postJson failed ' + inboxUrl + ' ' +
json.dumps(postJsonObject) + ' ' + str(headers))
print(e)
return None
if postResult: if postResult:
return postResult.text return postResult.text
return None return None
@ -136,8 +150,8 @@ def postJsonString(session, postJsonStr: str,
try: try:
postResult = \ postResult = \
session.post(url=inboxUrl, data=postJsonStr, headers=headers) session.post(url=inboxUrl, data=postJsonStr, headers=headers)
except ValueError as e: except requests.exceptions.RequestException as e:
print('WARN: error during postJsonString') print('WARN: error during postJsonString requests')
print(e) print(e)
return None, None return None, None
except SocketError as e: except SocketError as e:
@ -146,6 +160,10 @@ def postJsonString(session, postJsonStr: str,
print('ERROR: postJsonString failed ' + inboxUrl + ' ' + print('ERROR: postJsonString failed ' + inboxUrl + ' ' +
postJsonStr + ' ' + str(headers)) postJsonStr + ' ' + str(headers))
return None, None return None, None
except ValueError as e:
print('WARN: error during postJsonString')
print(e)
return None, None
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 \
@ -195,8 +213,8 @@ def postImage(session, attachImageFilename: str, federationList: [],
try: try:
postResult = session.post(url=inboxUrl, data=mediaBinary, postResult = session.post(url=inboxUrl, data=mediaBinary,
headers=headers) headers=headers)
except ValueError as e: except requests.exceptions.RequestException as e:
print('WARN: error during postImage') print('WARN: error during postImage requests')
print(e) print(e)
return None return None
except SocketError as e: except SocketError as e:
@ -206,6 +224,10 @@ def postImage(session, attachImageFilename: str, federationList: [],
str(headers)) str(headers))
print(e) print(e)
return None return None
except ValueError as e:
print('WARN: error during postImage')
print(e)
return None
if postResult: if postResult:
return postResult.text return postResult.text
return None return None