浏览代码

Catch leaked exception.

Miku AuahDark 3 年之前
父节点
当前提交
de4fe93cbb
共有 1 个文件被更改,包括 15 次插入5 次删除
  1. 15 5
      src/lua/main.cpp

+ 15 - 5
src/lua/main.cpp

@@ -94,6 +94,7 @@ static int w_request(lua_State *L)
 	auto url = w_checkstring(L, 1);
 	HTTPSClient::Request req(url);
 
+	std::string errorMessage("No applicable implementation found");
 	bool foundClient = false;
 	bool advanced = false;
 
@@ -124,10 +125,21 @@ static int w_request(lua_State *L)
 	for (size_t i = 0; clients[i]; ++i)
 	{
 		HTTPSClient &client = *clients[i];
+		HTTPSClient::Reply reply;
+
 		if (!client.valid())
 			continue;
 
-		auto reply = client.request(req);
+		try
+		{
+			reply = client.request(req);
+		}
+		catch(const std::exception& e)
+		{
+			errorMessage = e.what();
+			break;
+		}
+		
 		lua_pushinteger(L, reply.responseCode);
 		w_pushstring(L, reply.body);
 
@@ -149,12 +161,10 @@ static int w_request(lua_State *L)
 	if (!foundClient)
 	{
 		lua_pushnil(L);
-		lua_pushstring(L, "No applicable implementation found");
-		if (advanced)
-			lua_pushnil(L);
+		lua_pushstring(L, errorMessage.c_str());
 	}
 
-	return advanced ? 3 : 2;
+	return (advanced && foundClient) ? 3 : 2;
 }
 
 extern "C" int DLLEXPORT luaopen_https(lua_State *L)