Let say I want to test several data before proceeding and if it fails return an error response directly to the person who made the request.
I have this:
def gerUserInfo(request):
if request.user.is_authenticated:
data = json.loads(request.body.decode('utf-8'))
info1 = data.get("info1") if data.get("info1") else ""
if info1.strip() == "":
return JsonResponse({"status":"fail"})
else:
#proceed...
info2 = data.get("info2") if data.get("info2") else ""
if info2.strip() == "":
return JsonResponse({"status":"fail"})
else:
#proceed...
...
I want this:
def gerUserInfo(request):
def secureGetData(data):
try:
data = data if data else ""
if data.strip() == "": return JsonResponse({"status":"fail"})
else: return data
except:
return JsonResponse({"status":"fail"})
if request.user.is_authenticated:
data = json.loads(request.body.decode('utf-8'))
info1 = secureGetData(data.get("info1"))
info2 = secureGetData(data.get("info2"))
...
# i can proceed without worring...
I want the server response the request from secureGetData(), but never do.
EDIT:
Basically what I'm trying to do is to make a return from a nested function like here in JS for a loop, I want to do it with a return, I'm even not sure it's possible in JS...