Exercise - Error handling
In this exercise, you'll use a Try/Catch block to ensure the script stops responding early if a certain condition isn't met. You'll again work with your backup script.
Say you've noticed that you sometimes specify an erroneous path, which causes backup of files that shouldn't be backed up. You decide to add some error management.
Note
This exercise is optional. If you want to complete this exercise, you'll need to create an Azure subscription before you begin. If you don't have an Azure account or you don't want to create one at this time, you can read through the instructions so you understand the information that's being presented.
Note
In this unit, you use Azure Cloud Shell as a terminal. You can access Cloud Shell through the Azure portal or the Cloud Shell sign-in. You don't have to install anything on your PC or laptop to use it.
Note
Run the following commands only if you haven't completed any of the previous exercises in this module. We're assuming you've completed the previous exercises. If you haven't done so, you need a few files.
If you haven't completed the previous exercises in this module, run the following bash commands in a terminal:
mkdir webapp cd webapp touch index.html app.js cd ..These commands will create a directory that contains files typically associated with web development.
You also need a file named Backup.ps1. Run these commands:
touch Backup.ps1 code Backup.ps1Now that you have an editor running, add the required code. Paste this code into the editor and save the file:
Param( [string]$Path = './app', [string]$DestinationPath = './' ) If(-Not (Test-Path $Path)) { Throw "The source directory $Path does not exist, please specify an existing directory" } $date = Get-Date -format "yyyy-MM-dd" $DestinationFile = "$($DestinationPath + 'backup-' + $date + '.zip')" If (-Not (Test-Path $DestinationFile)) { Compress-Archive -Path $Path -CompressionLevel 'Fastest' -DestinationPath "$($DestinationPath + 'backup-' + $date)" Write-Host "Created backup at $($DestinationPath + 'backup-' + $date + '.zip')" } Else { Write-Error "Today's backup already exists" }
Implement a business requirement by using Try/Catch
Assume your company mostly builds web apps. These apps consist of HTML, CSS, and JavaScript files. You decide to optimize the script to recognize web apps.
Use an existing PowerShell shell, if you have one running. Otherwise, start one by typing
pwshin a terminal:pwshOpen Backup.ps1. In the
Paramsection, add a comma after the last parameter, and then add the following parameter:[switch]$PathIsWebAppYou've added a switch parameter. If this parameter is present when the script is invoked, you perform the check on the content. After that, you can determine if a backup file should be created.
Under the
Paramsection, add this code, then save the file:If ($PathIsWebApp -eq $True) { Try { $ContainsApplicationFiles = "$((Get-ChildItem $Path).Extension | Sort-Object -Unique)" -match '\.js|\.html|\.css' If ( -Not $ContainsApplicationFiles) { Throw "Not a web app" } Else { Write-Host "Source files look good, continuing" } } Catch { Throw "No backup created due to: $($_.Exception.Message)" } }The preceding code first checks if the parameter
$PathIsWebAppis provided at runtime. If it is, the code continues to get a list of file extensions from the directory specified by$Path. In our case, if you run that part of the code on the webapp directory, the following code will print a list of items:(Get-ChildItem $Path).Extension | Sort-Object -UniqueHere's the output:
.html .jsIn the full statement, we're using the
-matchoperator. The-matchoperator expects a regular expression pattern. In this case, the expression states "do any of the file extensions match.html,.js, or.css?" The result of the statement is saved to the variable$ContainsApplicationFiles.Then the
Ifblock checks whether the$ContainsApplicationFilesvariable isTrueorFalse. At this point, the code can take two paths:- If the source directory is for a web app, the script writes out "Source files look good, continuing."
- If the source directory isn't for a web app, the script throws an error that states "Not a web app." The error is caught in a
Catchblock. The script stops, and you rethrow the error with an improved error message.
Test the script by providing the switch
$PathIsWebApp:Note
Before you run the script, make sure there are no .zip files present. They might have been created when you completed previous exercises in this module. Use
Remove-Item *zipto remove them../Backup.ps1 -PathIsWebApp -Path './webapp'The script should print output that looks similar to this text:
Source files looks good, continuing Created backup at ./backup-2021-12-30.zipUsing your terminal, create a directory named python-app. In the new directory, create a file called script.py:
mkdir python-app cd python-app touch script.py cd ..Your directory should now look like this:
-| webapp/ ---| app.js ---| index.html -| python-app/ ---| script.py -| Backup.ps1In the PowerShell shell, run the script again, but this time change the
-Pathvalue to point to./python-app:./Backup.ps1 -PathIsWebApp -Path './python-app'Your script should now print this text:
No backup created due to: Not a web appThe output indicates that the check failed. It should have, because there are no files in the directory that have an .html, .js, or .css extension. Your code raised an exception that was caught by your
Catchblock, and the script stopped early.Congratulations! You've implemented a business requirement.