Skip to content

Commit fb78bad

Browse files
committed
Show detailed errors in Python 2 & 3 libraries
1 parent 6be2906 commit fb78bad

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

cryptolens_python2.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import urllib
2020
import hashlib
2121
from subprocess import Popen, PIPE
22+
from urllib2 import URLError, HTTPError
2223

2324
class HelperMethods:
2425

@@ -230,8 +231,12 @@ def activate(token, rsa_pub_key, product_id, key, machine_code, fields_to_return
230231
"MaxOverdraft": max_overdraft,\
231232
"Sign":"True",\
232233
"SignMethod":1}))
234+
except HTTPError as e:
235+
response = Response.from_string(e.read())
236+
except URLError as e:
237+
return (None, "Could not contact the server. Error message: " + e.reason)
233238
except Exception:
234-
return (None, "Could not contact the server.")
239+
return (None, "Could not contact the server. Error message: " + e.reason)
235240

236241
pubkey = RSAPublicKey.from_string(rsa_pub_key)
237242

@@ -269,8 +274,12 @@ def get_key(token, rsa_pub_key, product_id, key, fields_to_return = 0,\
269274
"FloatingTimeInterval": floating_time_interval,\
270275
"Sign":"True",\
271276
"SignMethod":1}))
277+
except HTTPError as e:
278+
response = Response.from_string(e.read())
279+
except URLError as e:
280+
return (None, "Could not contact the server. Error message: " + e.reason)
272281
except Exception:
273-
return (None, "Could not contact the server.")
282+
return (None, "Could not contact the server. Error message: " + e.reason)
274283

275284
pubkey = RSAPublicKey.from_string(rsa_pub_key)
276285

@@ -301,8 +310,12 @@ def create_trial_key(token, product_id, machine_code):
301310
response = HelperMethods.send_request("key/createtrialkey", {"token":token,\
302311
"ProductId":product_id,\
303312
"MachineCode":machine_code})
313+
except HTTPError as e:
314+
response = Response.from_string(e.read())
315+
except URLError as e:
316+
return (None, "Could not contact the server. Error message: " + e.reason)
304317
except Exception:
305-
return (None, "Could not contact the server.")
318+
return (None, "Could not contact the server. Error message: " + e.reason)
306319

307320
jobj = json.loads(response)
308321

@@ -335,8 +348,12 @@ def deactivate(token, product_id, key, machine_code, floating = False):
335348
"Key" : key,\
336349
"Floating" : floating,\
337350
"MachineCode":machine_code})
351+
except HTTPError as e:
352+
response = Response.from_string(e.read())
353+
except URLError as e:
354+
return (None, "Could not contact the server. Error message: " + e.reason)
338355
except Exception:
339-
return (False, "Could not contact the server.")
356+
return (None, "Could not contact the server. Error message: " + e.reason)
340357

341358
jobj = json.loads(response)
342359

licensing/methods.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from licensing.internal import HelperMethods
1212
from licensing.models import *
1313
import json
14+
from urllib.error import URLError, HTTPError
1415

1516
class Key:
1617

@@ -44,8 +45,12 @@ def activate(token, rsa_pub_key, product_id, key, machine_code, fields_to_return
4445
"MaxOverdraft": max_overdraft,\
4546
"Sign":"True",\
4647
"SignMethod":1}))
48+
except HTTPError as e:
49+
response = Response.from_string(e.read())
50+
except URLError as e:
51+
return (None, "Could not contact the server. Error message: " + e.reason)
4752
except Exception:
48-
return (None, "Could not contact the server.")
53+
return (None, "Could not contact the server. Error message: " + e.reason)
4954

5055
pubkey = RSAPublicKey.from_string(rsa_pub_key)
5156

@@ -83,8 +88,12 @@ def get_key(token, rsa_pub_key, product_id, key, fields_to_return = 0,\
8388
"FloatingTimeInterval": floating_time_interval,\
8489
"Sign":"True",\
8590
"SignMethod":1}))
91+
except HTTPError as e:
92+
response = Response.from_string(e.read())
93+
except URLError as e:
94+
return (None, "Could not contact the server. Error message: " + e.reason)
8695
except Exception:
87-
return (None, "Could not contact the server.")
96+
return (None, "Could not contact the server. Error message: " + e.reason)
8897

8998
pubkey = RSAPublicKey.from_string(rsa_pub_key)
9099

@@ -115,8 +124,12 @@ def create_trial_key(token, product_id, machine_code):
115124
response = HelperMethods.send_request("key/createtrialkey", {"token":token,\
116125
"ProductId":product_id,\
117126
"MachineCode":machine_code})
127+
except HTTPError as e:
128+
response = Response.from_string(e.read())
129+
except URLError as e:
130+
return (None, "Could not contact the server. Error message: " + e.reason)
118131
except Exception:
119-
return (None, "Could not contact the server.")
132+
return (None, "Could not contact the server. Error message: " + e.reason)
120133

121134
jobj = json.loads(response)
122135

@@ -146,8 +159,12 @@ def deactivate(token, product_id, key, machine_code, floating = False):
146159
"Key" : key,\
147160
"Floating" : floating,\
148161
"MachineCode":machine_code})
162+
except HTTPError as e:
163+
response = Response.from_string(e.read())
164+
except URLError as e:
165+
return (None, "Could not contact the server. Error message: " + e.reason)
149166
except Exception:
150-
return (False, "Could not contact the server.")
167+
return (None, "Could not contact the server. Error message: " + e.reason)
151168

152169
jobj = json.loads(response)
153170

0 commit comments

Comments
 (0)