[英]I don't know how to only show error message once with my Python statement
I'm fairly new to Python/Flask and I am trying to get a code block to only show an error message in the UI once - (and to stop it from repeating depending on how many errors are found).我对 Python/Flask 还很陌生,我试图让代码块只在 UI 中显示一条错误消息 - (并根据发现的错误数量来阻止它重复)。 I've seen other questions answered about this issue, most responses using some form of break after a print statement...but from what I've tried, that isn't working for my code because I'm not just printing the error - the error message is being put into a variable that will then be shown to the user on the screen.我已经看到有关此问题的其他问题的回答,大多数响应在打印语句后使用某种形式的中断...但是从我的尝试来看,这不适用于我的代码,因为我不只是打印错误- 错误消息被放入一个变量中,然后将在屏幕上显示给用户。
error_messages = []
designer_returned_an_error = isinstance(result, str)
if designer_returned_an_error:
print("Designer returned an error, not running additional checks")
return {"message": result}, 400
# checks for screens with length of 1 or 0 in the list of lists
list1 = result[0]
print(list1)
for screens in list1:
print(len(screens))
if len(screens) <= 1:
error_message = 'Based on these parameters you do not have enough items per screen to create a design.'
error_messages.append(error_message)
Maybe use a text file that is written to when an error is thrown, and in your if statement check if the written text is in the text file, so it only says there is an error when there isn't anything in the file.也许使用一个在抛出错误时写入的文本文件,并在你的 if 语句中检查写入的文本是否在文本文件中,所以它只会在文件中没有任何内容时说存在错误。 Like so:像这样:
f = open('examplename.txt', 'r+')
fileInfo = f.read()
error_messages = []
designer_returned_an_error = isinstance(result, str)
if designer_returned_an_error and fileInfo=="":
print("Designer returned an error, not running additional checks")
return {"message": result}, 400
f.write("error already thrown")
# checks for screens with length of 1 or 0 in the list of lists
list1 = result[0]
print(list1)
for screens in list1:
print(len(screens))
if len(screens) <= 1:
error_message = 'Based on these parameters you do not have enough items per screen to create a design.'
error_messages.append(error_message)
You can break the loop if there is an error because is a blocked error, for example just add a break
inside the error condition, also you can define your errors by code and set the message outside the loop:如果出现错误,您可以中断循环,因为这是一个阻塞错误,例如只需在错误条件内添加一个break
,您也可以通过代码定义错误并将消息设置在循环外:
error_messages = []
designer_returned_an_error = isinstance(result, str)
if designer_returned_an_error:
print("Designer returned an error, not running additional checks")
return {"message": result}, 400
# checks for screens with length of 1 or 0 in the list of lists
list1 = result[0]
print(list1)
for screens in list1:
print(len(screens))
if len(screens) <= 1:
error_message = 'Based on these parameters you do not have enough items per screen to create a design.'
error_messages.append(error_message)
break
Or you can do the following:或者您可以执行以下操作:
error_messages = []
items_with_errors = {'screen':[]}
designer_returned_an_error = isinstance(result, str)
if designer_returned_an_error:
print("Designer returned an error, not running additional checks")
return {"message": result}, 400
# checks for screens with length of 1 or 0 in the list of lists
list1 = result[0]
print(list1)
for screens in list1:
print(len(screens))
if len(screens) <= 1:
items_with_errors['screen'].append(screens)
if len(items_with_errors['screen']) > 0:
error_messages.append(f'Based on these parameters you do not have enough items per screen to create a design. You must have at least {len(items_with_errors['screen'])} item(s) per screen')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.