Skip to content

Commit 429ec4b

Browse files
committed
Exception handling for file download flow
1 parent 621fd37 commit 429ec4b

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

Client/ApiClient.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -342,19 +342,26 @@ public Object CallApi(
342342
HttpWebResponse responseT = (HttpWebResponse)requestT.GetResponse();
343343
using (Stream responseStream = responseT.GetResponseStream())
344344
{
345-
//setting high timeout to accomodate large files till 2GB, need to revisit for a dynamic approach
346-
responseStream.ReadTimeout = 8000000;
347-
responseStream.WriteTimeout = 9000000;
348-
using (Stream fileStream = File.OpenWrite(@DownloadReponseFileName))
345+
try
349346
{
350-
byte[] buffer = new byte[4096];
351-
int bytesRead = responseStream.Read(buffer, 0, 4096);
352-
while (bytesRead > 0)
347+
//setting high timeout to accomodate large files till 2GB, need to revisit for a dynamic approach
348+
responseStream.ReadTimeout = 8000000;
349+
responseStream.WriteTimeout = 9000000;
350+
using (Stream fileStream = File.OpenWrite(@DownloadReponseFileName))
353351
{
354-
fileStream.Write(buffer, 0, bytesRead);
355-
bytesRead = responseStream.Read(buffer, 0, 4096);
352+
byte[] buffer = new byte[4096];
353+
int bytesRead = responseStream.Read(buffer, 0, 4096);
354+
while (bytesRead > 0)
355+
{
356+
fileStream.Write(buffer, 0, bytesRead);
357+
bytesRead = responseStream.Read(buffer, 0, 4096);
358+
}
356359
}
357360
}
361+
catch (Exception)
362+
{
363+
throw new ApiException(-1, $"Error writing to path : {DownloadReponseFileName}");
364+
}
358365
}
359366

360367
//setting the generic response with response headers

generator/cybersource-csharp-template/ApiClient.mustache

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -389,19 +389,26 @@ namespace {{packageName}}.Client
389389
HttpWebResponse responseT = (HttpWebResponse)requestT.GetResponse();
390390
using (Stream responseStream = responseT.GetResponseStream())
391391
{
392-
//setting high timeout to accomodate large files till 2GB, need to revisit for a dynamic approach
393-
responseStream.ReadTimeout = 8000000;
394-
responseStream.WriteTimeout = 9000000;
395-
using (Stream fileStream = File.OpenWrite(@DownloadReponseFileName))
392+
try
396393
{
397-
byte[] buffer = new byte[4096];
398-
int bytesRead = responseStream.Read(buffer, 0, 4096);
399-
while (bytesRead > 0)
394+
//setting high timeout to accomodate large files till 2GB, need to revisit for a dynamic approach
395+
responseStream.ReadTimeout = 8000000;
396+
responseStream.WriteTimeout = 9000000;
397+
using (Stream fileStream = File.OpenWrite(@DownloadReponseFileName))
400398
{
401-
fileStream.Write(buffer, 0, bytesRead);
402-
bytesRead = responseStream.Read(buffer, 0, 4096);
399+
byte[] buffer = new byte[4096];
400+
int bytesRead = responseStream.Read(buffer, 0, 4096);
401+
while (bytesRead > 0)
402+
{
403+
fileStream.Write(buffer, 0, bytesRead);
404+
bytesRead = responseStream.Read(buffer, 0, 4096);
405+
}
403406
}
404407
}
408+
catch (Exception)
409+
{
410+
throw new ApiException(-1, $"Error writing to path : {DownloadReponseFileName}");
411+
}
405412
}
406413

407414
//setting the generic response with response headers
@@ -825,11 +832,11 @@ namespace {{packageName}}.Client
825832
int proxyPortTest;
826833
827834
if (!string.IsNullOrWhiteSpace(merchantConfig.ProxyAddress) && int.TryParse(merchantConfig.ProxyPort, out proxyPortTest))
828-
{
835+
{
829836
WebProxy proxy = new WebProxy(merchantConfig.ProxyAddress, proxyPortTest);
830837
831838
if (!string.IsNullOrWhiteSpace(merchantConfig.ProxyUsername) && !string.IsNullOrWhiteSpace(merchantConfig.ProxyPassword))
832-
{
839+
{
833840
proxy.Credentials = new NetworkCredential(merchantConfig.ProxyUsername, merchantConfig.ProxyPassword);
834841
}
835842

0 commit comments

Comments
 (0)