我運行的Apache在生產服務器上運行我的Django的項目時有subprocess.CalledProcessError
一個奇怪的問題:CalledProcessError導致500錯誤Apache的,即使它是(或者應該是)抓住
我的代碼(更新:增加捕 - 全異常處理 - 行爲是不變)如下:
try:
command_string = 'gcc -O0 -g3 -Wall -c -fmessage-length=0 ' + cfile + ' -o ' + ofile
compile_result = subprocess.check_output(command_string,stderr=subprocess.STDOUT,shell=True)
#logger.warning(compile_result)
if compile_result != "": #Dann gab es einen Fehler bzw. ein Compiler-Warning --> Abbruch!
self.ausgabe = u"Compile:\n"
self.ausgabe += unicode(compile_result, "utf-8")
return
except subprocess.CalledProcessError as e:
self.ausgabe = u"Compilierfehler (Returncode {0}):\n".format(e.returncode)
self.ausgabe += unicode(e.output, "utf-8")
logger.error("CPE" + unicode(e.returncode, "utf-8") + unicode(e.output, "utf-8"))
return #die weiteren Schritte müssen gar nicht erst ausgeführt werden...
except:
logger.error(str(sys.exc_info()))
self.ausgabe = u"Compilieren nicht erfolgreich. Fehler:\n" + unicode(sys.exc_info(), "utf-8")
return
這一切如預期,當我在我的Windows開發機器和djange TESTSERVER上運行的工作。當命令執行失敗時捕獲異常,錯誤處理按預期工作。
當我將代碼移動到我的生產服務器(ubuntu,apache)時,當命令執行失敗時出現「內部服務器錯誤500」,這不是所需的行爲。 apache error.log不是很有幫助,因爲它不顯示任何錯誤。
我的配置是: 的Apache/2.2.22(Ubuntu的)PHP/5.4.9-4ubuntu2.3的mod_wsgi/3.4的Python/2.7.4
(是的,我重新啓動Apache和我敢肯定,我運行相同的代碼)。
對此有何意見?
如果catch-all異常處理沒有捕獲到這個,那麼無論是* not *運行這個代碼,還是生產中的異常發生在'try'塊的外部*。 500甚至可能是由於**以外的完全錯誤**。 –
在這部分代碼中引入故意的錯誤,並查看您是否在生產中看到行爲更改。這至少可以讓你排除運行陳舊代碼的可能性。 –
謝謝@MartijnPieters!我介紹了ZeroDevisionError和 - tadaa - 同樣的行爲。這激勵我從不同的角度檢查我的異常處理 - 簡而言之,另一個編碼問題被發現。我會更詳細地解釋這個問題的答案。 – OBu