Skip to content

Commit 7e3d0a4

Browse files
committed
Merge pull request #104 from VBA-tools/offline-error
Handle offline errors
2 parents 70a0999 + b18da29 commit 7e3d0a4

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

specs/Specs_WebClient.bas

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,41 @@ Public Function Specs() As SpecSuite
294294
InlineRunner.RunSuite Specs
295295
End Function
296296

297+
Public Function OfflineSpecs() As SpecSuite
298+
' Disconnect from the internet before running these specs
299+
300+
Set OfflineSpecs = New SpecSuite
301+
OfflineSpecs.Description = "WebClient - Offline"
302+
303+
Dim Client As New WebClient
304+
Dim Request As WebRequest
305+
Dim Response As WebResponse
306+
307+
Client.BaseUrl = HttpbinBaseUrl
308+
309+
With OfflineSpecs.It("should handle resolve errors as timeout")
310+
Client.TimeoutMs = 500
311+
312+
Set Request = New WebRequest
313+
Request.Resource = "/get"
314+
315+
Set Response = Client.Execute(Request)
316+
.Expect(Response.StatusCode).ToEqual 408
317+
.Expect(Response.StatusDescription).ToEqual "Request Timeout"
318+
End With
319+
320+
With OfflineSpecs.It("should not crash with auto-proxy resolve error")
321+
Client.EnableAutoProxy = True
322+
Client.TimeoutMs = 500
323+
324+
Set Request = New WebRequest
325+
Request.Resource = "/get"
326+
327+
Set Response = Client.Execute(Request)
328+
.Expect(Response.StatusCode).ToEqual 408
329+
.Expect(Response.StatusDescription).ToEqual "Request Timeout"
330+
End With
331+
332+
InlineRunner.RunSuite OfflineSpecs
333+
End Function
334+

src/WebClient.cls

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ Public Function Execute(Request As WebRequest) As WebResponse
262262
Case 5, 6, 7
263263
' 5 = CURLE_COULDNT_RESOLVE_PROXY
264264
' 6 = CURLE_COULDNT_RESOLVE_HOST
265+
Err.Raise 208615 + vbObjectError, "WebClient.Execute", "The server name or address could not be resolved"
266+
Case 7
265267
' 7 = CURLE_COULDNT_CONNECT
266268
Err.Raise 208637 + vbObjectError, "WebClient.Execute", "A connection with the server could not be established"
267269
Case 12, 28
@@ -304,7 +306,7 @@ web_ErrorHandling:
304306
Dim web_ErrorDescription As String
305307

306308
Select Case Err.Number - vbObjectError
307-
Case 208610, 208637
309+
Case 208610, 208615, 208637
308310
' Return 408
309311
web_Response.StatusCode = WebStatusCode.RequestTimeout
310312
web_Response.StatusDescription = "Request Timeout"

0 commit comments

Comments
 (0)