Skip to content

Commit

Permalink
Merge pull request #420 from georgejecook/request-try-catch
Browse files Browse the repository at this point in the history
added try catch block for execute request method
  • Loading branch information
georgejecook authored Dec 4, 2023
2 parents 85585d2 + 0ceb84d commit 654628c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
11 changes: 9 additions & 2 deletions src/source/core/Request.bs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,15 @@ namespace mc.net
else if headers <> invalid
m.log.error("received illegal headers type", type(headers))
end if

return m.runRequest(method, url, args)
result = new mc.net.NetResult(-1, invalid, invalid, headers)

try
result = m.runRequest(method, url, args)
m.log.info(`request for method ${method} with url ${url} and params ${params} was executed successfully`)
catch error
m.log.warn("there was an error while executing request for method", method, "with url", url, "and params", params, error)
end try
return result
end function

function logAsCurl(method as string, url as string, headers as mc.types.assocarray, data as string)
Expand Down
40 changes: 29 additions & 11 deletions src/source/core/Request.spec.bs
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,24 @@ namespace tests
@describe("executeRequest")
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

@it("does not pass parmas or headers if not set")
@it("does not pass params or headers if not set")
@params("get", "u1", invalid, invalid)
@params("put", "u2", invalid, invalid)
@params("get", "u1", "illegal", invalid)
@params("put", "u2", invalid, "illegal")
@params("put", "u2", "illegal", "illegal")
function _(method, url, params, headers)

expectedArgs = {
}

result = { "id": "result" }
m.expectOnce(m.request, "runRequest", [method, url, expectedArgs], result)

m.assertEqual(m.request.executeRequest(method, url, params, headers), result)
end function


@it("passes headers values to request, if set")
@params("put", "u2", { "i": 1 }, { "i": 2 })
function _(method, url, params, headers)

expectedArgs = {
params: params
headers: headers
Expand Down Expand Up @@ -150,6 +146,28 @@ namespace tests
m.assertEqual(m.request.executeRequest("method", "url", params, headers, otherArgs), result)
end function

@it("returns mc.net.NetResult if runRequest throws an error")
function _()
m.request.runRequest = function(method, url, args)
throw "error"
end function
expectedResult = new mc.net.NetResult(-1, invalid, invalid, invalid)
result = m.request.executeRequest("GET", "url")
m.assertEqual(result, expectedResult)
end function

@it("returns mc.net.NetResult if type miss match error was caught by try catch block")
@params(invalid, invalid)
@params(invalid, "url")
@params("GET", invalid)
@params("", invalid)
@params([], {})
function _(method, url)
expectedResult = new mc.net.NetResult(-1, invalid, invalid, invalid)
result = m.request.executeRequest(method, url)
m.assertEqual(result, expectedResult)
end function

'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@describe("_execute")
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Expand All @@ -158,7 +176,7 @@ namespace tests
@params("GET", "u", { id: "h" }, invalid, 100)
@params("GET", "u", { id: "h" }, invalid, 200)
@params("GET", "u", { id: "h" }, invalid, 299)
function _(method, url, headers, data, repsonseCode)
function _(method, url, headers, data, responseCode)
transfer = { "id": "transfer" }
m.expectOnce(m.request, "createUrlTransfer", [true, true, "crt"], transfer)

Expand All @@ -168,7 +186,7 @@ namespace tests
event = { "id": "event" }
m.expectOnce(m.request, "sendTransfer", [transfer, 30, method, data], event)

m.expectOnce(event, "getResponseCode", [], repsonseCode)
m.expectOnce(event, "getResponseCode", [], responseCode)

netResult = { "id": "netResult" }

Expand All @@ -186,7 +204,7 @@ namespace tests
@params("POST", "u", { id: "h" }, "data", 200)
@params("PUT", "u", { id: "h" }, invalid, 200)
@params("PUT", "u", { id: "h" }, "data", 200)
function _(method, url, headers, data, repsonseCode)
function _(method, url, headers, data, responseCode)
transfer = { "id": "transfer" }
m.expectOnce(m.request, "createUrlTransfer", [true, true, "crt"], transfer)

Expand All @@ -199,7 +217,7 @@ namespace tests
'time out doubles each time
event = { "id": "event" }
m.expectOnce(m.request, "sendTransfer", [transfer, 60, method, data], event)
m.expectOnce(event, "getResponseCode", [], repsonseCode)
m.expectOnce(event, "getResponseCode", [], responseCode)


netResult = { "id": "netResult" }
Expand Down Expand Up @@ -242,7 +260,7 @@ namespace tests

@it("executes request and logs as curl if isLoggingAsCurl set to true")
@params("GET", "u", { id: "h" }, { id: "data" }, 100)
function _(method, url, headers, data, repsonseCode)
function _(method, url, headers, data, responseCode)
m.request.isLoggingAsCurl = true
transfer = { "id": "transfer" }

Expand All @@ -255,7 +273,7 @@ namespace tests
event = { "id": "event" }
m.expectCalled(m.request.sendTransfer(transfer, 30, method, data), event)

m.expectCalled(event.getResponseCode(), repsonseCode)
m.expectCalled(event.getResponseCode(), responseCode)

netResult = { "id": "netResult" }

Expand Down

0 comments on commit 654628c

Please sign in to comment.