Skip to content

Commit 23f3fdf

Browse files
committed
Handle timeouts for sync requests
1 parent 31b39ce commit 23f3fdf

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/RestClient.cls

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,26 @@ Public Function Execute(Request As RestRequest) As RestResponse
6262
Case Else
6363
Set Response.Data = RestHelpers.ParseJSON(Response.Content)
6464
End Select
65-
Set Execute = Response
6665

6766
ErrorHandling:
6867

6968
If Not http Is Nothing Then Set http = Nothing
70-
If Not Response Is Nothing Then Set Response = Nothing
7169

7270
If Err.Number <> 0 Then
73-
' Rethrow error
74-
Err.Raise Err.Number, Description:=Err.Description
71+
If InStr(Err.Description, "The operation timed out") > 0 Then
72+
' Return 504
73+
Response.StatusCode = StatusCodes.GatewayTimeout
74+
Response.StatusDescription = "Gateway Timeout"
75+
76+
Err.Clear
77+
Else
78+
' Rethrow error
79+
Err.Raise Err.Number, Description:=Err.Description
80+
End If
7581
End If
7682

83+
Set Execute = Response
84+
7785
End Function
7886

7987
''
@@ -102,7 +110,6 @@ Public Function ExecuteAsync(Request As RestRequest, Callback As String, Optiona
102110
Call http.send(Request.Body)
103111

104112
' Clean up and return
105-
' Set http = Nothing
106113
ExecuteAsync = True
107114
Exit Function
108115

@@ -130,7 +137,7 @@ Private Sub HttpSetup(ByRef http As Object, ByRef Request As RestRequest, Option
130137
Call Request.AddHeader("User-Agent", UserAgent)
131138
Call Request.AddHeader("Content-Type", Request.ContentType())
132139

133-
' If async, pass http to request and setup onreadystatechange
140+
' Pass http to request and setup onreadystatechange
134141
If useAsync Then
135142
Set Request.HttpRequest = http
136143
http.onreadystatechange = Request

0 commit comments

Comments
 (0)