try exception update does not work

57 views
Skip to first unread message

Yebach

unread,
Mar 26, 2015, 10:30:38 AM3/26/15
to web...@googlegroups.com
Hello

In my controller function I have a couple of try excepts. In case of except I want to update db record status to different value based on where the error occurred. somehow update record is not executed

any suggestions why?

here is my controller  function

def runWoshi(scriptId, script, outPath, org, path,llsPath):
    

    print "scriptId ", scriptId
    db.script_activity.insert(sa_id_script = scriptId, sa_start = 'now', sa_organization = org, sa_location =  llsPath);
    
    ##validiramo json če ima kakršne koli napake idt.
    try:
        print "tukej validiram neki"
        valJson = validateJson(script.sc_menu_data)
    
    except Exception as e:
        "Če je napaka v validaciji Jsona je status v  92"
        script.update_record(sc_status = 92)
        print db._lastsql
        print "je biu error scripta id " ,scriptId
        
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        errmsg = ["error v validaciji Jsona", exc_type, fname, exc_tb.tb_lineno,e.__doc__ , e.message]
        print errmsg
        print(exc_type, fname, exc_tb.tb_lineno)
        print e.__doc__
        print e.message
        return dict(status = 672, errmsg = errmsg)
     
            
#    if not valJson:
#        script.update_record(sc_status = 92)
#        #validacija ni uspela vrnemo error 300 
#        return dict(status = 300)
#    #nov json - z vsemi errorji zapiđšemo v sc_menu_data
#    else:
    script.update_record(sc_menu_data = valJson["formJson"])
    
    #če obstajjo errorji pol konc
    if valJson["errors"]:
        #skripta gre v status 91, kar pomen da je user ne more dat računat
        update = db(db.scripts.id == scriptId).update(sc_status = 91)
        print "Json errors " , valJson["errors"]
        return dict(status = 200)
    
    
    
    updateScripts = db(db.scripts.id == scriptId).update(sc_engine_output = None)

    ##kreiram LLS        
    try:
        transData = TranslateData(script.sc_menu_data, path, scriptId, org)
    
    except Exception as e:
        
        "Status 91 pomen da je bila napaka pri kreiranju lls skripte"
        update = db(db.scripts.id == scriptId).update(sc_status = 90)
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        errmsg = ["Creating LLS from Json failed ", exc_type, fname, exc_tb.tb_lineno,e.__doc__ , e.message]
        print "errmsg ", errmsg
        print(exc_type, fname, exc_tb.tb_lineno)
        print e.__doc__
        print e.message
        return dict(status = 668, errmsg = errmsg)
    
    
    #Se prav se je zgodu error pri kreiranju datoteke
#    if transData["data"]["type"] == 666:
#        #status damo v 90 in vrnemo kar je v error
#        update = db(db.scripts.id == scriptId).update(sc_status = 90)
#        #print 'transData["data"] ' , transData["data"]
#        #transDataError = transData["data"]
#        return  dict(status = 664)   
#        
#    try:
        #Pobiršem out file če obstaja v mapi engine/e1 in sc_engine_out
    count = 0
    while ( count < 10 and ( os.path.isfile(outPath))):
        count += 1 
#            print count
#            print "brisem out..."
        os.remove(outPath)
        time.sleep(0.05)
    
    # Run woshi engine
    path_1 =  os.path.join(path, 'e1')
    os.chdir(path_1)
    #pot =  path_1 + "\\woshi_engine.exe"
    #print pot
    #p = subprocess.Popen(['w_parameter.bat', id], shell=True, stdout = subprocess.PIPE)
    p = subprocess.Popen(['woshi_engine.exe', scriptId], shell=True, stdout = subprocess.PIPE)
    
    return dict(status = 1)

Niphlod

unread,
Mar 26, 2015, 10:35:16 AM3/26/15
to web...@googlegroups.com
web2py commits by default at the end of a successful request, and rollbacks in case of any exception is raised.

Vid Ogris

unread,
Mar 26, 2015, 10:37:06 AM3/26/15
to web...@googlegroups.com
Possible solution?

2015-03-26 22:35 GMT+08:00 Niphlod <nip...@gmail.com>:
web2py commits by default at the end of a successful request, and rollbacks in case of any exception is raised.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/nGsyQTxdQhk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Lep pozdrav 

Vid Ogris


Anthony

unread,
Mar 26, 2015, 4:13:03 PM3/26/15
to web...@googlegroups.com
Looks like the exception is being caught and not re-raised, so there shouldn't be a rollback unless an exception is raised somewhere else that we can't see.

Anyway, if you want to commit just a particular update but rollback anything done previously, you can first call db.rollback() explicitly to rollback all previous operations, then do your update, and then call db.commit() to commit the update. You can then re-raise the exception if needed.

Anthony
Reply all
Reply to author
Forward
0 new messages