Solved

foxpro error handling code for InternetExplorer.Application object

Posted on 2013-01-16
6
981 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:esak2000
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Find out what Office 365 Transport Rules are, how they work and their limitations managing Office 365 signatures.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

13 Experts available now in Live!

Get 1:1 Help Now