Skip to content

Commit 8ec4f11

Browse files
committed
Fixed FetchStream in a try-catch block
1 parent 071e07b commit 8ec4f11

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

tools/ImportExamples.ps1

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,11 @@ function Start-WebScrapping {
137137
$ExampleLinks = New-Object -TypeName 'System.Collections.ArrayList';
138138
$Snippets = New-Object -TypeName 'System.Collections.ArrayList';
139139
try {
140-
($readStream, $HttpWebResponse) = FetchStream -GraphDocsUrl $GraphDocsUrl
141-
140+
($readStream, $HttpWebResponse, $IsSuccess) = FetchStream -GraphDocsUrl $GraphDocsUrl
141+
if($IsSuccess -eq $False) {
142+
Write-Host "The url $GraphDocsUrl is invalid. Please ensure that the url is correct"
143+
return
144+
}
142145
while (-not $readStream.EndOfStream) {
143146
$Line = $readStream.ReadLine()
144147
if ($Line -match "^### Example") {
@@ -223,12 +226,21 @@ function FetchStream {
223226
param(
224227
[string]$GraphDocsUrl
225228
)
229+
try{
226230
$HttpWebRequest = [System.Net.WebRequest]::Create($GraphDocsUrl)
227231
$HttpWebResponse = $HttpWebRequest.GetResponse()
228232
$ReceiveStream = $HttpWebResponse.GetResponseStream()
229233
$Encode = [System.Text.Encoding]::GetEncoding("utf-8")
230234
$ReadStream = [System.IO.StreamReader]::new($ReceiveStream, $Encode)
231-
return ($ReadStream, $HttpWebResponse)
235+
return ($ReadStream, $HttpWebResponse, $True)
236+
}
237+
catch {
238+
return ($null, $null, $False)
239+
Write-Host "`nError Message: " $_.Exception.Message
240+
Write-Host "`nError in Line: " $_.InvocationInfo.Line
241+
Write-Host "`nError in Line Number: "$_.InvocationInfo.ScriptLineNumber
242+
Write-Host "`nError Item Name: "$_.Exception.ItemName
243+
}
232244
}
233245
function Update-ExampleFile {
234246
param(

0 commit comments

Comments
 (0)