Solved

foxpro error handling code for InternetExplorer.Application object

Posted on 2013-01-16
6
994 Views
Last Modified: 2013-01-23
Does anyone have foxpro code for error handling and InternetExplorer.Application object?
0
Comment
Question by:esak2000
  • 3
  • 2
6 Comments
 
LVL 41

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 41

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 41

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
In 2017, ransomware will become so virulent and widespread that if you aren’t a victim yourself, you will know someone who is.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

930 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now