优化http出错抛出更详细的错误信息 (#1900)

* 优化http出错抛出更详细的错误信息

* 优化http err_info写法
This commit is contained in:
华仔
2024-04-10 13:40:41 +08:00
committed by GitHub
parent 1d47bfa0b6
commit 22df64244a
4 changed files with 42 additions and 8 deletions

View File

@@ -2,8 +2,12 @@ local skynet = require "skynet"
local socket = require "http.sockethelper"
local internal = require "http.internal"
local dns = require "skynet.dns"
local string = string
local table = table
local pcall = pcall
local error = error
local pairs = pairs
local httpc = {}

View File

@@ -2,6 +2,11 @@ local internal = require "http.internal"
local string = string
local type = type
local assert = assert
local tonumber = tonumber
local pcall = pcall
local ipairs = ipairs
local pairs = pairs
local httpd = {}

View File

@@ -1,5 +1,11 @@
local table = table
local type = type
local string = string
local tonumber = tonumber
local pcall = pcall
local assert = assert
local error = error
local pairs = pairs
local M = {}

View File

@@ -1,11 +1,26 @@
local socket = require "skynet.socket"
local skynet = require "skynet"
local coroutine = coroutine
local error = error
local tostring = tostring
local readbytes = socket.read
local writebytes = socket.write
local sockethelper = {}
local socket_error = setmetatable({} , { __tostring = function() return "[Socket Error]" end })
local socket_error = setmetatable({} , {
__tostring = function(self)
local info = self.err_info
self.err_info = nil
return info or "[Socket Error]"
end,
__call = function (self, info)
self.err_info = "[Socket Error] : " .. tostring(info)
return self
end
})
sockethelper.socket_error = socket_error
@@ -27,7 +42,7 @@ local function preread(fd, str)
if ret then
return str .. ret
else
error(socket_error)
error(socket_error("read failed fd = " .. fd))
end
end
end
@@ -36,7 +51,7 @@ local function preread(fd, str)
if ret then
return ret
else
error(socket_error)
error(socket_error("read failed fd = " .. fd))
end
end
end
@@ -51,7 +66,7 @@ function sockethelper.readfunc(fd, pre)
if ret then
return ret
else
error(socket_error)
error(socket_error("read failed fd = " .. fd))
end
end
end
@@ -62,24 +77,27 @@ function sockethelper.writefunc(fd)
return function(content)
local ok = writebytes(fd, content)
if not ok then
error(socket_error)
error(socket_error("write failed fd = " .. fd))
end
end
end
function sockethelper.connect(host, port, timeout)
local fd
local fd, err
local is_time_out = false
if timeout then
is_time_out = true
local drop_fd
local co = coroutine.running()
-- asynchronous connect
skynet.fork(function()
fd = socket.open(host, port)
fd, err = socket.open(host, port)
if drop_fd then
-- sockethelper.connect already return, and raise socket_error
socket.close(fd)
else
-- socket.open before sleep, wakeup.
is_time_out = false
skynet.wakeup(co)
end
end)
@@ -89,13 +107,14 @@ function sockethelper.connect(host, port, timeout)
drop_fd = true
end
else
is_time_out = false
-- block connect
fd = socket.open(host, port)
end
if fd then
return fd
end
error(socket_error)
error(socket_error("connect failed host = " .. host .. ' port = '.. port .. ' timeout = ' .. timeout .. ' err = ' .. tostring(err) .. ' is_time_out = '.. tostring(is_time_out)))
end
function sockethelper.close(fd)