User friendly(-ish) error-handling

- Completely changed exception handling to be more user-friendly (Note:
This broke error-logging, possible Python-related issue.)
This commit is contained in:
Fierelier 2017-08-16 15:09:53 +02:00
parent e4569f1c57
commit 45318f36c1

View File

@ -65,45 +65,65 @@ def exceptionCleanup():
if success == False:
logtext = logtext + "\nCleanup not fully successful. Please review the errors, and go to X for a guide on how to reset your game manually. Sorry for the inconvenience, I tried :("
return logtext
return success,logtext
def logError(logFilePath,textList):
logFile = False
try:
logFile = open(logFilePath,"w")
except:
return False
try:
for line in textList:
logFile.write(line + "\n")
except:
logFile.close()
return False
logFile.close()
return True
def exceptionHandler(exc_type, exc_value, tb):
import traceback
while True:
clear()
print("An exception has occurred:")
traceback.print_exception(exc_type, exc_value, tb)
cleanupLog = exceptionCleanup()
print("\n" +cleanupLog)
choice = input("\nWould you like to log this exception? (y/n)\n")
if choice.lower() == "y":
success = False
fileCreated = False
errorFilePath = os.path.join(scriptPath,"error.log")
try:
errorFile = open(errorFilePath,"w")
fileCreated = True
try:
traceback.print_exception(exc_type, exc_value, tb, None, errorFile)
errorFile.write("\n" +cleanupLog)
success = True
except:
input("Writing to errorlog-file failed. Press ENTER to continue.")
except:
input("Creating errorlog-file failed. Press ENTER to continue.")
clear()
print("An error occurred, trying to revert everything...")
cleanupSuccess,cleanupLog = exceptionCleanup()
logFile = os.path.join(scriptPath,"error.log")
logSuccess = logError(logFile,[
"An exception has occurred:",
traceback.format_exc(),
"",
cleanupLog
])
if cleanupSuccess == True:
while True:
clear()
print("An error occured, game has been reset to default.")
print("C) Continue")
if logSuccess == True: print("S) Show more info")
choice = input("Choice: ")
if fileCreated: errorFile.close()
if success:
try:
openFileWithStandardApp(errorFilePath)
except:
input("Opening errorlog-file failed. You can find it at '" +errorFilePath+ "'. Press ENTER to continue.")
elif choice.lower() != "n":
continue
if choice.lower() == "c": return
if logSuccess == True:
if choice.lower() == "s": openFileWithStandardApp(logFile)
else:
clear()
print("An error occured and error-correction failed. This is a major exception.\n")
sys.exit(-1)
if logSuccess == True:
print("You might need to reset some stuff by yourself; we printed a report into '" +logFile+ "' which will help you resolve this issue, this file will be opened for you automagically after you exit.")
input("\nPress ENTER to exit.")
openFileWithStandardApp(logFile)
sys.exit(-1)
else:
print("You might need to reset some stuff by yourself; we tried printing a report, but failed. Please check out X for help, we will open that website for you automagically after you exit.")
input("\nPress ENTER to exit.")
sys.exit(-1)
sys.excepthook = exceptionHandler