Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Initial hacky error handling when running via script. #2

Merged
merged 1 commit into from about 2 years ago

2 participants

Eric Helgeson Carlos Eberhardt
Eric Helgeson

Slightly anti-pattern as there's no error handling pattern yet, maybe should throw the error up or handle. Wont work if you change interface from script to something else, but at least the concept is there.

Carlos Eberhardt carloseberhardt merged commit b9dae5c into from
Carlos Eberhardt carloseberhardt closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Feb 22, 2012
Eric Helgeson Initial hacky error handling when running via script. c87c66f
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 19 additions and 7 deletions. Show diff stats Hide diff stats

  1. 26  owaagenda.py
26  owaagenda.py
... ...
@@ -1,9 +1,10 @@
1 1
 """
2 2
 Screen scrape a single day view of OWA calendar
3 3
 	example call
4  
-	python agenda.py "https://mail.example.com/owa/?ae=Folder&t=IPF.Appointment" username password
  4
+	python agenda.py "https://mail.example.com/owa/?ae=Folder&t=IPF.Appointment" username
5 5
 """
6 6
 import sys, urllib2, urlparse, json, getpass
  7
+from urllib2 import URLError
7 8
 from bs4 import BeautifulSoup
8 9
 
9 10
 
@@ -15,10 +16,19 @@ def getHtml(url, user, pwd):
15 16
 	pwdmgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
16 17
 	pwdmgr.add_password(None, getBaseUrl(url), user, pwd)
17 18
 	handler = urllib2.HTTPBasicAuthHandler(pwdmgr)
18  
-	opener = urllib2.build_opener(handler)
19  
-	request = urllib2.Request(url)
20  
-	calendar = opener.open(request)
21  
-	html = calendar.read()
  19
+	html = None
  20
+	try:
  21
+		opener = urllib2.build_opener(handler)
  22
+		request = urllib2.Request(url)
  23
+		calendar = opener.open(request)
  24
+		html = calendar.read()
  25
+	except URLError, e:
  26
+		if hasattr(e, 'reason'):
  27
+			print 'We failed to reach a server.'
  28
+			print 'Reason: ', e.reason
  29
+		elif hasattr(e, 'code'):
  30
+			print 'The server couldn\'t fulfill the request.'
  31
+			print 'Error code: ', e.code
22 32
 	return html
23 33
 
24 34
 def txt_class_with_link(tag):
@@ -45,6 +55,8 @@ def jsonFormatter(thelist):
45 55
 	url = sys.argv[1] 
46 56
 	user = sys.argv[2] 
47 57
 	pwd = getpass.getpass("Password: ")
48  
-	html = getHtml(url,user,pwd) 
  58
+	html = getHtml(url,user,pwd)
  59
+	if html == None:
  60
+		sys.exit(1)
49 61
 	agenda = scrapeAgenda(html)
50  
-	print textFormatter(agenda)
  62
+	print textFormatter(agenda)
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.