Solved

foxpro error handling code for InternetExplorer.Application object

Posted on 2013-01-16
6
1,012 Views
Last Modified: 2013-01-23
Does anyone have foxpro code for error handling and InternetExplorer.Application object?
0
Comment
Question by:esak2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 42

Expert Comment

by:pcelba
ID: 38783093
Once the webpage is ready and completely read then TRY CATCH structure is sufficient enough.

Or do you mean how to recognize 404 etc. errors on web pages? It is individual and depends on page design.
0
 

Author Comment

by:esak2000
ID: 38783116
yes,  I would like code that catches potential errors like 404 errors or other common errors that may occur when using the InternetExplorer.Application object.

I'm familiar with try/catch, I was hoping that someone has more detailed code that I can use.

Thank you
0
 
LVL 42

Expert Comment

by:pcelba
ID: 38783135
No this is not possible. 4xx and 5xx errors are server errors and the client cannot recognize them except of the detail page contents analysis. Each server returns a valid web page and it is your responsibility to recognize the wrong contents.

So you may search the page text for "404" and "error" and some other strings but you would need to do it for each domain and language separately...

What you could do is to request some randomly named page which surely does not exist. Such a page should return an "error template" which you could use to identify real errors. Some search engines do not return 404 because they just try to search the wrong page name...
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

Author Comment

by:esak2000
ID: 38783182
thank you for your response. How about code that catches errors  of the InternetExplorer.Application itself, like the error that would occur if there's lost internet connection or a document.form(0).item is not valid?
0
 
LVL 42

Accepted Solution

by:
pcelba earned 500 total points
ID: 38783245
The most problematic part is web page reading. I am doing it in a loop with timeouts. Once the page is ready then I don't care about the connection:
LOCAL oIE, cWWW, cErrMsg, cStatMsg
STORE '' TO cErrMsg, cStatMsg
cWWW="http://www.  ..."

IF ReadURL(@oIE, m.cWWW, @cErrMsg, @cStatMsg) <> 0
  *-- Error
  RETURN
ENDIF

*-- Process web page
nDocCnt = oie.document.all.length
* etc.

	
*---------------------------------------------------------------------------------
PROCEDURE ReadURL
LPARAMETERS cURL, oIE, cErrMsg, cStatMsg

LOCAL loEx AS Exception, llIEerror, ltStartTime, lnRetVal
cErrMsg = ''
lnRetVal = 0

IF VARTYPE(m.cStatMsg) <> 'C'
  cStatMsg = ''
ENDIF

TRY
	
	IF TYPE("oie.Height") <> 'N'
	  oie = NULL
	  DOEVENTS FORCE
	  oie = NEWOBJECT("internetexplorer.application")
	ENDIF
	
	TRY
	  oie.navigate2(m.cURL)
	CATCH
	  llIEerror = .T.
	ENDTRY
	
	IF m.llIEerror
	  TRY
	    oie.Stop
	    oie.Quit
	  CATCH
	    WAIT WINDOW "IE.Quit failed" TIMEOUT 5
	  ENDTRY
	  oie = NULL
	  DOEVENTS FORCE
	  oie = NEWOBJECT("internetexplorer.application")
	  WAIT WINDOW "IE Restart" TIMEOUT 5
	  IF VARTYPE(oIE) = 'O'
	    oie.navigate2(m.cURL)
	  ENDIF
	ENDIF
	
	IF VARTYPE(oIE) = 'O' 
		
		oie.Visible=.t.
		ltStartTime = DATETIME()
		
		WAIT 'Reading data ' + m.cStatMsg WINDOW NOWAIT
		DO WHILE oie.busy
			DOEVENTS
			WAIT 'Reading data ' + m.cStatMsg + ' ... busy ' + TRANSFORM(DATETIME()-m.ltStartTime) + ' s'  WINDOW TIMEOUT (IIF(DATETIME()-m.ltStartTime<1,0.1,0.5))
			IF DATETIME()-m.ltStartTime > 60
			  TRY
			    oie.Stop
			    oie.Quit
			    WAIT "Doc stopped" TIMEOUT 1
			    *SKIP -1
			  CATCH
			    oie = NULL
			  ENDTRY
			  lnRetVal = -2
			  cErrMsg = 'IE busy > 60 sec'
			  oie = NULL
			  EXIT
			ENDIF
		ENDDO
		
		DO WHILE VARTYPE(oIE) = 'O' AND oie.document.readyState#"complete"
			DOEVENTS
			WAIT 'Reading data ' + m.cStatMsg + ' ... not complete ' + TRANSFORM(DATETIME()-m.ltStartTime) + ' s'  WINDOW TIMEOUT (IIF(DATETIME()-m.ltStartTime<1,0.1,0.5))
			IF DATETIME()-m.ltStartTime > 60
			  TRY
			    oie.Stop
			    oie.Quit
			    WAIT "Doc stopped" TIMEOUT 1
			    *SKIP -1
			  CATCH
			    oie = NULL
			  ENDTRY
			  lnRetVal = -3
			  cErrMsg = 'IE not ready > 60 sec'
			  oie = NULL
			  EXIT
			ENDIF
		ENDDO
		
	ELSE
		lnRetVal = -1
		cErrMsg = 'IE Re/start failed'
	ENDIF
	
CATCH TO loEx
  lnRetVal = loEx.ErrorNo
  cErrMsg = loEx.Message + ' line No. ' + loEx.LineNo
ENDTRY

RETURN m.lnRetVal

Open in new window

0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 38811391
If you want direct error messages from a web request, don't automate the IE, automate eg MsXml2.XmlHttp.

loRequest = Createobject('MsXml2.XmlHttp')
loRequest.open("GET","http://www.google.com/sdfsdg",.f.)
loRequest.send(.null.)
?loRequest.status, loRequest.statusText

Open in new window

But I think you are talking about other erros like javascript errors or such things. Then even Pavels code won't suppress them.

Bye, Olaf.

Edit:
Specifically errors, which happen when you address elements of the loaded document like in document.form(0).item can easily be catched via Try...Catch.

For example:
Local loIE, loDoc, loForm, loEx
loIE = CreateObject("internetexplorer.application")
loIE.Navigate2("about:blank")
Do While loIE.ReadyState <>4
   Doevents 
EndDo 
Try
   loDoc = loIE.Document
   ? "number of forms in the document:", loDoc.forms.length
   loForm = loDoc.forms(0).item
Catch To loEx
   ? loEx.ErrorNo, loEx.Message, loEx.LineNo
EndTry 

Open in new window


As "about:blank" by definition is a blank page, it has no html form in it, so even forms(0) does not exist. But you can detect that even without trying to access loDoc.forms(0).item, because loDoc.forms.length is then 0. Length is a typical property of any DOM collection like forms is, that contains a count of the collection items, With 0 you know there even is no first form.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Visual FoxPro (short VFP) is a programming language with it’s own IDE and database, ranking somewhat between Access and VB.NET + SQL Server (Express). Product Description: http://msdn.microsoft.com/en-us/vfoxpro/default.aspx (http://msd…
Postmortem reporting allow us to examine mistakes in a way that focuses on the situational aspects of a failure’s mechanism and the decision-making process of individuals proximate to the failure. Read our guide on how to handle IT post-mortem repor…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question