經?吹揭恍┏鯇Wasp的朋友為了測試一個值到處用response.write來打印,
而要看頁面效果時再刪除這些語句或加上注釋,在正式版本出來以前要如此反復
多次。而有些人為了減少麻煩,干脆全當它是正確的,不做測試輸出,像這樣極
易出現各種各樣的問題,最常見的是如果要生成一條sql語句,需要使用變量,如
果不做測試打印,很難做到一次正確,在chinaasp論壇里經常見到這種問題,老
是有人問這條語句為什么會出錯等問題,實際上他只要打印出這條語句看一下語
法是否正確就行了,而不必到處追著人問。事實上好的編程習慣應該是在自己沒
有很大把握的情況下把生成的語句或變量值打印出來,但這樣做又費時費力,有
沒有比較好的解決方法呢? 在C里可以使用 _DEBUG這樣的測試開關來控制debug版本和release版本,但
asp中并沒有類似#define這種語句,那么是不是我們就沒有辦法了呢?其實我們
可以仿照c的這種做法,那就是在global.asa文件里定義一個application變量來
控制,象下面這個例子:
在global.asp里加上: application("DEBUG") = 1
然后做這么一個過程: '-------------------------------------------------------- 'Name:PRINT 'Argument:a_strPrint:打印字符串 'Return: 'Description:打。▋H在DEBUG狀態下運行) 'Hitory:Create by bigeagle '-------------------------------------------------------- Sub PRINT(a_strPrint) if Application("DEBUG") = 1 then Response.Write("<P aling=center>"+a_strPrint+"</P>") end if End Sub
這個過程的功能就是當測試開關打開時(application("DEBUG") = 1)打印
,而當測試開關關閉(application("DEBUG") = 0)就不會有打印輸出了。這樣
在程序調試期間,你可以打開測試開關,以觀察變量的值,而當要看頁面效果或
發布release版本時就可以關閉測試開關,這樣所有的測試輸出就不會出現在頁面
上。
以上談到的是變量的測試輸出,下面要談談正確性檢測問題。經常見到很多
人把頁面提交過來或數據庫取出的值想都不想就用,根本不做正確性檢測,那你
怎么能保證這些值的正確性呢?比如有一個input,提交后它的值應該是一個值包
含數字的字符串,但如果用戶的輸入包含其他字符,若不做正確性檢測,那當你
用cint或clng轉換時就會發生錯誤,整個程序崩潰。另外一種情況是這樣,當你
從數據庫中取值或諸如此類的操作,應該是不會發生問題,但如果出現數據庫出
錯等問題,那么用戶也只能見到一個諸如'odbc錯誤'等等的提示信息,對于一個
成熟的商品程序來說,這點是很不好的,其實現在包括國內很多知名站點也出現
這種問題。所以應該養成這樣一個習慣,那就是任何可能出問題的變量、參數在
使用之前都應該做正確性檢測,并且對數據庫操作后應當判斷否成功。這是就又出現一個版本問題,如果是DEBUG版則應顯示出錯信息以備修改,而release版則應該引導到一個統一頁面,如“本站暫時出現未知故障,請稍候再來"等等,原則上永遠不要給用戶一個系統出錯信息頁面。要實現上述功能,請看以下幾個函數和過程。
'-------------------------------------------------------- 'Name:ASSERT 'Argument:a_blnConditon:斷言條件 'a_FunctionName:調用函數 'a_ErrorString:錯誤描敘 'Return: 'Description:斷言 'Hitory:Create by Bigeagle '-------------------------------------------------------- Sub ASSERT(a_blnConditon,a_FunctionName,a_ErrorString) if Application("DEBUG") = 0 then if a_blnConditon <> TRUE then response.redirect("../include/bigerror.asp") end if else if a_blnConditon <> TRUE then call print("斷言錯誤:在<font color=red>" + a_FunctionName + "</font>出現:" + a_ErrorString) response.end
end if
end if
End Sub
這個過程的作用是檢測變量或參數有效性,如果條件a_blnCondition<>true,那么如果測試開關打開,則顯示錯誤信息,如果測試開關關閉,則重定向到錯誤處理頁面bigerror.asp。
'-------------------------------------------------------- 'Name:CheckError 'Argument: 'Return: 'Description:檢查錯誤 'Hitory:Create by Yaozhigang '-------------------------------------------------------- Function CheckError() Dim intErrNumber intErrNumber = Err.Number'保存錯誤代碼,因為在ERROR中將執行Err.Clear
if intErrNumber <> 0 then Call ERROR(-1, "")'Err錯誤的錯誤碼為-1
CheckError = intErrNumber End Function
'-------------------------------------------------------- 'Name:ERROR 'Argument:a_intErrCode:錯誤碼(-1時表示是系統錯誤,即Err.Number<>0) 'a_strErrText:錯誤描述 'Return: 'Description:錯誤處理 'Hitory:Create by Yaozhigang '-------------------------------------------------------- Sub ERROR(a_intErrCode, a_strErrText)
Dim strMsg Dim strLogMsg
'如果是Err錯誤,則一定執行錯誤頁 if a_intErrCode = -1 then
strMsg = strMsg + "*********************************************************************************************" + "<BR>" strMsg = strMsg + "錯誤時間:" + CStr(Now())+ "<BR>" strMsg = strMsg + "錯誤類型:Err錯誤"+ "<BR>" strMsg = strMsg + "錯誤號:" + CStr(Err.Number)+ "<BR>" strMsg = strMsg + "錯誤源:" + Err.Source+ "<BR>" strMsg = strMsg + "錯誤描述:" + Err.Description+ "<BR>" strMsg = strMsg + "*********************************************************************************************" + "<BR>"
strLogMsg = strLogMsg + "*********************************************************************************************" + Chr(13) + Chr(10) strLogMsg = strLogMsg + "錯誤時間:" + CStr(Now())+ Chr(13) + Chr(10) strLogMsg = strLogMsg + "錯誤類型:Err錯誤"+ Chr(13) + Chr(10) strLogMsg = strLogMsg + "錯誤號:" + CStr(Err.Number) + Chr(13) + Chr(10) strLogMsg = strLogMsg + "錯誤源:" + Err.Source+ Chr(13) + Chr(10) strLogMsg = strLogMsg + "錯誤描述:" + Err.Description+ Chr(13) + Chr(10) strLogMsg = strLogMsg + "*********************************************************************************************" + Chr(13) + Chr(10)
'清空Err Err.Clear
'在Redirect之前寫日志 WriteLog(strLogMsg)
strMsg = Server.UrlEncode(strMsg) '如果是調試則顯示錯誤代碼 if Application("DEBUG") = 1 then Response.Redirect("../include/error.asp?" + _ "ErrText="&strMsg&"") else Response.Redirect("../include/error.asp") end if
'如果是程序錯誤,則寫日志 else
strMsg = strMsg + "*********************************************************************************************" + "<BR>" strMsg = strMsg + "錯誤時間:" + CStr(Now())+ "<BR>"
strLogMsg = strLogMsg + "*********************************************************************************************" + Chr(13) + Chr(10) strLogMsg = strLogMsg + "錯誤時間:" + CStr(Now())+ Chr(13) + Chr(10)
Dim strWhichErr Select Case a_intErrCode
Case 99001'程序錯誤從99001開始 strWhichErr = "斷言錯誤" Case 99002 strWhichErr = "Case Else 錯誤" Case else strWhichErr = "未知的錯誤" End Select
strMsg = strMsg + "錯誤類型:" + strWhichErr + "<BR>" if a_strErrText <> "" then strMsg = strMsg + "錯誤描述:" + a_strErrText + "<BR>" end if strMsg = strMsg + "*********************************************************************************************" + "<BR>"
strLogMsg = strLogMsg + "錯誤類型:" + strWhichErr + Chr(13) + Chr(10) if a_strErrText <> "" then strLogMsg = strLogMsg + "錯誤描述:" + a_strErrText+ Chr(13) + Chr(10) end if strLogMsg = strLogMsg + "*********************************************************************************************" + Chr(13) + Chr(10)
'寫日志 WriteLog(strLogMsg)
'如果是調試狀態則指向錯誤頁 if Application("DEBUG") = 1 then strMsg = Server.UrlEncode(strMsg) Response.Redirect("http://server1/4biz/include/error.asp?" + _ "ErrText="&strMsg&"") end if end if End Sub
'-------------------------------------------------------- 'Name:WriteLog 'Argument:a_strMsg:日志內容 'Return: 'Description:錯誤處理 'Hitory:Create by Yaozhigang '-------------------------------------------------------- Function WriteLog(a_strMsg) Dim strFileName
if Application("g_strLogFileName") = "" then strFileName = "c:\AspLog.txt" else strFileName = Application("g_strLogFileName") end if
Dim objFSO Dim objFile Set objFSO = CreateObject("Scripting.FileSystemObject")
'只有一個人寫日志 Application.Lock
Set objFile = objFSO.OpenTextFile(strFileName, 8, True, 0) objFile.Write(a_strMsg) objFile.Close
Application.UnLock
Set objFile = Nothing Set objFSO = Nothing End Function
以上幾個函數及過程的作用是處理數據庫錯誤,注意使用時在global.asp里加上on eroor resume,以使錯誤處理程序能夠運行。其中可以顯示錯誤代碼、寫日志文件,詳細內容我就不做解釋了,請自己研究一下源代碼。
|