When scripts are writen they should be written defensively. This means that any situation that is unexpected should be coded for and appropriate actioun taken.
For example the following script assumes that a UDO namedmyname
exists:
defexecute(){def udoService = flexSdkClient.userDefinedObjectServicedef udoObjectQuery = UserDefinedObjectApiQuery.builder().name('myname').build()// Should always return a UDO
def udos = udoService.getUserDefinedObjects('reels', udoObjectQuery)// get the first UDO
def udo = udos.getObjects().find{true}def assetPl = NewAssetPlaceholder.builder().type('media-asset').name('demo asset').build()def assetService = flexSdkClient.assetServicedef asset = assetService.createAsset(assetPl)// add the asset as a child to the UDO
udoService.addChildObject('reals', udo.id, asset.id)}
The problem with this is that if no UDO namedmyname
exists the job will fail with a NullPointerException. A better solution is to throw an exception with a relevant message:
defexecute(){def udoService = flexSdkClient.userDefinedObjectServicedef udoObjectQuery = UserDefinedObjectApiQuery.builder().name('myname').build()// Should always return a UDO
def udos = udoService.getUserDefinedObjects('reels', udoObjectQuery)if(udos.totalCount===0){thrownewException("UDO named 'mynane' does not exist")}// get the first UDO
def udo = udos.getObjects().find{true}def assetPl = NewAssetPlaceholder.builder().type('media-asset').name('demo asset').build()def assetService = flexSdkClient.assetServicedef asset = assetService.createAsset(assetPl)// add the asset as a child to the UDO
udoService.addChildObject('reals', udo.id, asset.id)}
Defensive programming is especially important when assuming the existence of certain data. For example, if a script performs an asset search and expects a single result, exceptions should be thrown if zero or multiple assets are found.
Comments
0 comments
Please sign in to leave a comment.