Capture and save the function code with ASP running errors

Source: Internet
Author: User

Process name: catch (str)
Usage: Copy codeThe Code is as follows: on error resume next
'Your code, such as database connection
Call catch ("prompt information displayed to the user ")

Function: Clear the IIS error message, return the custom error message to the user, and save the error information to the txt file (of course, you can also change it to the custom page)

Code:Copy codeThe Code is as follows: <%
Option explicit
'Example 1 ---------------------------
'Must be used with on error resume next, but it is best to comment out the webpage before it is officially released, so that the error details are not visible during debugging.
On error resume next
'I is not defined, and an error will occur. Use catch to clear the error and save it to notepad.
I
Call catch ("Page inaccessible ")
'-------------------------------
'Example 2 ---------------------------
Function conn ()
'Must be used with on error resume next
On error resume next
'...... Your connection database code
Call catch ("database opening error ")
End function
'-------------------------------
Sub catch (str)
If err. number <> 0 then
Dim tmp, path
'Absolute Error Log Path, for example, "/error_log.txt"
Path = "/table/error_log.txt"
Tmp = tmp & "error page:" & geturl & vbcrlf
Tmp = tmp & "error time:" & now () & vbcrlf
Tmp = tmp & "access IP:" & ip & vbcrlf
Tmp = tmp & "prompt:" & str & vbcrlf
Tmp = tmp & "error code:" & err. number & vbcrlf
Tmp = tmp & "error message:" & err. description & vbcrlf
Tmp = tmp & "Application:" & err. source & vbcrlf
Tmp = tmp & file_read (path)
Call file_save (tmp, path, 1)
Err. clear ()
Die (str)
End if
End sub
'Here are the functions used by catch --------------------
Sub echo (str)
Response. write (str)
End sub
Sub die (str)
Echo (str): response. end ()
End sub
Function ip ()
Ip = request. servervariables ("remote_addr ")
End function
'Get the current URL
Function geturl ()
Dim tmp
If lcase (request. servervariables ("https") = "off" then
Tmp = "http ://"
Else
Tmp = "https ://"
End if
Tmp = tmp & request. servervariables ("server_name ")
If request. servervariables ("server_port") <> 80 then
Tmp = tmp & ":" & request. servervariables ("server_port ")
End if
Tmp = tmp & request. servervariables ("url ")
If trim (request. querystring) <> "then
Tmp = tmp &"? "& Trim (request. queryString)
End if
Geturl = tmp
End function
'Function: Read the file content to a string
Function file_read (path)
Dim tmp: tmp = "false"
If not file_exists (path) then file_read = tmp: exit function
Dim stream: set stream = server. CreateObject ("ADODB. Stream ")
With stream
. Type = 2' text type
. Mode = 3' read/write mode
. Charset = "gb2312"
. Open
. Loadfromfile (server. MapPath (path ))
Tmp =. readtext ()
End
Stream. close: set stream = nothing
File_read = tmp
End function
'Function: Save the string to the file.
Function file_save (str, path, model)
If model <> 0 and model <> 1 then model = 1
If model = 0 and file_exists (path) then file_save = true: exit function
Dim stream: set stream = server. CreateObject ("ADODB. Stream ")
With stream
. Type = 2' text type
. Charset = "gb2312"
. Open
. Writetext str
. Savetofile (server. MapPath (path), model + 1
End
Stream. close: set stream = nothing
File_save = file_exists (path)
End function
'Function: checks whether a file or folder exists.
Function file_exists (path)
Dim tmp: tmp = false
Dim fso: set fso = server. CreateObject ("Scripting. FilesyStemObject ")
If fso. fileexists (server. MapPath (path) then tmp = true
If fso. folderexists (server. MapPath (path) then tmp = true
Set fso = nothing
File_exists = tmp
End function
%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.