30
30
import requests
31
31
32
32
import application_creator
33
- from exceptiondef import ConflictingState , NotFound , ExceptionThatShouldBeDisplayedToCaller
33
+ from exceptiondef import ConflictingState , NotFound
34
34
from package_parser import PackageParser
35
35
from async_dispatcher import AsyncDispatcher
36
36
from lifecycle_states import ApplicationState , PackageDeploymentState
@@ -151,7 +151,6 @@ def deploy_package(self, package):
151
151
# this function will be executed in the background:
152
152
def _do_deploy ():
153
153
# if this value is not changed, then it is assumed that the operation never completed
154
- deploy_status = {"state" : PackageDeploymentState .NOTDEPLOYED , "information" : "Error deploying " + package }
155
154
try :
156
155
package_file = package + '.tar.gz'
157
156
logging .info ("deploy: %s" , package )
@@ -165,12 +164,9 @@ def _do_deploy():
165
164
deploy_status = {"state" : PackageDeploymentState .DEPLOYED ,
166
165
"information" : "Deployed " + package + " at " + self .utc_string ()}
167
166
logging .info ("deployed: %s" , package )
168
- except ExceptionThatShouldBeDisplayedToCaller as ex :
169
- # log error to screen:
170
- logging .error (ex .msg )
171
- # prepare human readable message
172
- error_message = package + " " + str (type (ex ).__name__ ) + ", details: " + json .dumps (ex .msg )
173
- # set the status:
167
+ except Exception as ex :
168
+ logging .error (str (ex ))
169
+ error_message = "Error deploying " + package + " " + str (type (ex ).__name__ ) + ", details: " + json .dumps (str (ex ))
174
170
deploy_status = {"state" : PackageDeploymentState .NOTDEPLOYED , "information" : error_message }
175
171
raise
176
172
finally :
@@ -190,26 +186,21 @@ def utc_string(self):
190
186
def undeploy_package (self , package ):
191
187
# this function will be executed in the background:
192
188
def do_undeploy ():
193
- undeploy_failed = True
194
- # this will be the default error message if it is not clear what the error was:
195
- deploy_status = {"state" : PackageDeploymentState .DEPLOYED , "information" : "Error undeploying " + package }
189
+ deploy_status = None
196
190
try :
197
-
198
191
logging .info ("undeploy: %s" , package )
199
192
self ._package_registrar .delete_package (package )
200
193
logging .info ("undeployed: %s" , package )
201
- deploy_status = None
202
- undeploy_failed = False
203
- except ExceptionThatShouldBeDisplayedToCaller as ex :
194
+ except Exception as ex :
204
195
# log error to screen:
205
- logging .error (ex . msg )
196
+ logging .error (str ( ex ) )
206
197
# prepare human readable message
207
- error_message = package + " " + str (type (ex ).__name__ ) + ", details: " + json .dumps (ex . msg )
198
+ error_message = "Error undeploying " + package + " " + str (type (ex ).__name__ ) + ", details: " + json .dumps (str ( ex ) )
208
199
# set the status:
209
- deploy_status = {"state" : PackageDeploymentState .NOTDEPLOYED , "information" : error_message }
200
+ deploy_status = {"state" : PackageDeploymentState .DEPLOYED , "information" : error_message }
210
201
raise
211
202
finally :
212
- if undeploy_failed :
203
+ if deploy_status is not None :
213
204
# persist any errors in the database, but still throw them:
214
205
self ._package_registrar .set_package_deploy_status (package , deploy_status )
215
206
@@ -303,15 +294,9 @@ def do_work():
303
294
create_data = self ._application_registrar .get_create_data (application )
304
295
self ._application_creator .start_application (application , create_data )
305
296
self ._application_registrar .set_application_status (application , ApplicationState .STARTED )
306
- except ExceptionThatShouldBeDisplayedToCaller as ex :
307
- self ._handle_application_error (application , ex , ApplicationState .CREATED )
308
- raise
309
297
except Exception as ex :
310
- # report the error:
311
- self ._application_registrar .set_application_status (application , ApplicationState .CREATED ,
312
- "Error starting application: " + application )
313
- # re-throw the exception after reporting it
314
- raise Exception (ex )
298
+ self ._handle_application_error (application , ex , ApplicationState .CREATED , "starting" )
299
+ raise
315
300
finally :
316
301
self ._clear_package_progress (application )
317
302
self ._state_change_event_application (application )
@@ -331,15 +316,9 @@ def do_work():
331
316
create_data = self ._application_registrar .get_create_data (application )
332
317
self ._application_creator .stop_application (application , create_data )
333
318
self ._application_registrar .set_application_status (application , ApplicationState .CREATED )
334
- except ExceptionThatShouldBeDisplayedToCaller as ex :
335
- self ._handle_application_error (application , ex , ApplicationState .STARTED )
336
- raise
337
319
except Exception as ex :
338
- # report the error:
339
- self ._application_registrar .set_application_status (application , ApplicationState .STARTED ,
340
- "Error stopping application: " + application )
341
- # re-throw the exception after reporting it
342
- raise Exception (ex )
320
+ self ._handle_application_error (application , ex , ApplicationState .STARTED , "stopping" )
321
+ raise
343
322
finally :
344
323
self ._clear_package_progress (application )
345
324
self ._state_change_event_application (application )
@@ -388,14 +367,10 @@ def do_work():
388
367
package_data_path , package_metadata , application , overrides )
389
368
self ._application_registrar .set_create_data (application , create_data )
390
369
self ._application_registrar .set_application_status (application , ApplicationState .CREATED )
391
- except ExceptionThatShouldBeDisplayedToCaller as ex :
392
- self ._handle_application_error (application , ex , ApplicationState .NOTCREATED )
393
- raise
394
370
except Exception as ex :
395
- self ._application_registrar .set_application_status (application , ApplicationState .NOTCREATED ,
396
- "Error creating application: " + application )
371
+ self ._handle_application_error (application , ex , ApplicationState .NOTCREATED , "creating" )
397
372
logging .error (traceback .format_exc (ex ))
398
- raise ex
373
+ raise
399
374
finally :
400
375
# clear inner locks:
401
376
self ._clear_package_progress (application )
@@ -404,7 +379,7 @@ def do_work():
404
379
405
380
self .dispatcher .run_as_asynch (task = do_work )
406
381
407
- def _handle_application_error (self , application , ex , app_status ):
382
+ def _handle_application_error (self , application , ex , app_status , operation ):
408
383
"""
409
384
Use to handle application exceptions which should be relayed back to the user
410
385
Sets the application state to an error
@@ -413,9 +388,9 @@ def _handle_application_error(self, application, ex, app_status):
413
388
:param app_status: The status the app should be at following the error.
414
389
"""
415
390
# log error to screen:
416
- logging .error (ex . msg )
391
+ logging .error (str ( ex ) )
417
392
# prepare human readable message
418
- error_message = application + " " + str (type (ex ).__name__ ) + ", details: " + json .dumps (ex . msg )
393
+ error_message = "Error %s " % operation + application + " " + str (type (ex ).__name__ ) + ", details: " + json .dumps (str ( ex ) )
419
394
# set the status:
420
395
self ._application_registrar .set_application_status (application , app_status , error_message )
421
396
@@ -432,15 +407,9 @@ def do_work():
432
407
create_data = self ._application_registrar .get_create_data (application )
433
408
self ._application_creator .destroy_application (application , create_data )
434
409
self ._application_registrar .delete_application (application )
435
- except ExceptionThatShouldBeDisplayedToCaller as ex :
436
- self ._handle_application_error (application , ex , ApplicationState .STARTED )
437
- raise
438
410
except Exception as ex :
439
- # report the error:
440
- self ._application_registrar .set_application_status (application , ApplicationState .STARTED ,
441
- "Error deleting application: " + application )
442
- # re-throw the exception after reporting it
443
- raise Exception (ex )
411
+ self ._handle_application_error (application , ex , ApplicationState .STARTED , "deleting" )
412
+ raise
444
413
finally :
445
414
self ._clear_package_progress (application )
446
415
self ._state_change_event_application (application )
0 commit comments