Capture and save the function code for ASP running errors. Friends who need to obtain asp code running errors can refer to the process name: catch(str)
How to use:
Copy the code code as follows:
on error resume next
'Your code, such as database connection
call catch (prompt message displayed to the user)
Function: Clear the IIS error message, customize the error message and return it to the user, and save the error message to a txt file (of course you can also make slight modifications to redirect to a custom page, etc.)
Code:
Copy the code code as follows:
<%
option explicit
'Example 1--------------------------
'Must be used together with on error resume next, but it is best to comment it out before the web page is officially released, so as not to not see the error details 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(the page cannot be accessed)
'---------------------------------
'Example 2---------------------------
functionconn()
'Must be used together with on error resume next
on error resume next
'.........your code to connect to the database
call catch (database opening error)
end function
'---------------------------------
sub catch(str)
if err.number <> 0 then
dim tmp,path
'The absolute path of the error log, such as /error_log.txt
path = /table/error_log.txt
tmp = tmp & error page: & geturl & vbcrlf
tmp = tmp & error time: & now() & vbcrlf
tmp = tmp & visiting IP: & ip & vbcrlf
tmp = tmp & prompt message: & str & vbcrlf
tmp = tmp & error code: & err.number & vbcrlf
tmp = tmp & error message: & err.description & vbcrlf
tmp = tmp & application: & err.source & vbcrlf & vbcrlf & vbcrlf
tmp = tmp & file_read(path)
call file_save(tmp,path,1)
err.clear()
die(str)
end if
end sub
'The following are the functions used by catch--------------------
sub echo(str)
response.write(str)
end sub
subdie(str)
echo(str) : response.end()
end sub
functionip()
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 file content into 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 and write mode
.charset = gb2312
.open
.loadfromfile(server.MapPath(path))
tmp = .readtext()
end with
stream.close : set stream = nothing
file_read = tmp
end function
'Function: save string to 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 with
stream.close : set stream = nothing
file_save = file_exists(path)
end function
'Function: detect whether the file/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
%>