Skip to content

Commit

Permalink
fix: error handling in http_v2.lua when using copas
Browse files Browse the repository at this point in the history
  • Loading branch information
AMD-NICK committed Sep 8, 2024
1 parent b2b5f35 commit 10d9874
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lua/http_async.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ end
-- t headers, s body (for POST), s type, i timeout
function http.request(parameters)
return copas.addnamedthread("http_request", function(r, b, h)
copas.setErrorHandler(function(msg, co, skt)
copas.seterrorhandler(function(msg, co, skt)
if parameters.failed then
local suberror = tostring(msg):match("TLS/SSL handshake failed: (.*)$") or tostring(msg) -- closed/System error/{}
parameters.failed("copas_error:" .. suberror) -- can be parsed if needed
Expand Down
17 changes: 13 additions & 4 deletions lua/http_v2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,20 @@ local function request(reqt)
-- reqt.method = reqt.method or "POST" -- PUT, PATCH?
end

local ok, code, headers, status = http.request(reqt)
if ok then
-- pcall is to handle copas errors like: TLS/SSL handshake failed: closed or "module 'ssl' not found"
local pcall_ok, http_ok_or_pcall_error, code, headers, status = pcall(http.request, reqt)
if not pcall_ok then
local pcall_error = http_ok_or_pcall_error
-- Однажды попал на /usr/local/share/lua/5.1/copas.lua:70: /usr/local/share/lua/5.1/copas.lua:740: module 'ssl' not found:
-- Не обработал и долго не мог понять почему запрос не делается. Оказалось luasec..
local suberror = tostring(pcall_error):match("TLS/SSL handshake failed: (.*)$") or tostring(pcall_error) -- closed/System error/{}
reqt.copas_error = "copas_error:" .. suberror -- can be parsed if needed
end

if pcall_ok and http_ok_or_pcall_error then
return table.concat(t), code, headers, status
else
return nil, code
else -- pcall error or http error
return nil, reqt.copas_error or code
end
end

Expand Down

0 comments on commit 10d9874

Please sign in to comment.