luvit/luvit

Problem with print with json-styled strings

zachup100 opened this issue · 2 comments

I am running into an issue where I cannot debug the results of a SQL query. The output contains a JSON string. Oddly, only when the function allows the passthrough of the second argument, this issue occurs. If I were to remove the second argument, 'out', prints work as intended. The string in question is shown below. The variable READ_SQL is from the Luvit-MySQL github linked below.

Luvit-MySQL: https://github.com/kengonakajima/luvit-mysql

{"Party": {"Lobby": "0", "Category": "0", "Template": "{USERNAME}'s Party"}, "Prefix": "!"}

function GetGuildInfo(Id)
  local Guild = Guilds[Id]
  local Finished = false
  if not Guild then --// load if not in memory
    READ_SQL:query(string.format("SELECT Config FROM Guilds WHERE Id='%s'", Id), function(err, out)
      if out then
        Guilds[Id] = {}
        Guilds[Id].Info = json.parse(out[1].Config)
        Guilds[Id].LastPing = os.time()
        Guild = Guilds[Id]
        Finished = true
      end
      if type(err) == "table" then print("Unable to select config for GetGuildInfo() => ("..err.number..") "..err.message) end
      Finished = true
    end)
  end
  print(Finished)
  return Guild.Info
end
GetGuildInfo("612506098779553793")

Edit: Forgot the error code

Uncaught Error: [string "bundle:/deps/pretty-print.lua"]:302: bad argument #1 to 'guess_handle' (number expected, got nil)
stack traceback:
        [C]: in function 'guess_handle'
        [string "bundle:/deps/pretty-print.lua"]:302: in function 'console_write'
        [string "bundle:/deps/pretty-print.lua"]:328: in function 'print'
        E:\Robert\Documents\Github\LuaBot\main.lua:26: in function 'cb'
        E:/Robert/Documents/Github/LuaBot/sql/client.lua:236: in function 'cb'
        E:/Robert/Documents/Github/LuaBot/sql/query.lua:42: in function 'emit'
        E:/Robert/Documents/Github/LuaBot/sql/query.lua:65: in function 'handlePacket'
        E:/Robert/Documents/Github/LuaBot/sql/client.lua:170: in function 'handlePacket'
        E:/Robert/Documents/Github/LuaBot/sql/client.lua:134: in function 'cb'
        E:/Robert/Documents/Github/LuaBot/sql/parser.lua:121: in function 'emitPacket'
        E:/Robert/Documents/Github/LuaBot/sql/parser.lua:598: in function 'receive'
        E:/Robert/Documents/Github/LuaBot/sql/client.lua:125: in function 'handler'
        [string "bundle:/deps/core.lua"]:248: in function 'emit'
        [string "bundle:/deps/stream/stream_readable.lua"]:172: in function 'push'
        [string "bundle:/deps/net.lua"]:122: in function <[string "bundle:/deps/net.lua"]:117>
        [C]: in function 'run'
        [string "bundle:/init.lua"]:88: in function 'fn'
        [string "bundle:deps/require.lua"]:310: in function <[string "bundle:deps/require.lua"]:266>

Windows 10 20H2 OS Build 19042.685
luvit version: 2.17.0
luvi version: v2.10.1
libuv version: 1.34.1
rex version: 8.37 2015-04-28
winsvc version: 1.0.0
ssl version: OpenSSL 1.1.1d 10 Sep 2019, lua-openssl 0.7.7

Seems like uv.fileno is returning nil here:

if uv.guess_handle(uv.fileno(fs))=='tty' then

Not sure why that would be, or how the out parameter could possibly have an effect. From the fileno docs:

Gets the platform dependent file descriptor equivalent.
The following handles are supported: TCP, pipes, TTY, UDP and poll. Passing any other handle type will fail with EINVAL.
If a handle doesn't have an attached file descriptor yet or the handle itself has been closed, this function will return EBADF.

Well, messing around with the arguments, the only time I can't access print is when I define the second argument in the function. As well as I think it might be failing to finish the function as 'Finished' variable doesn't change. I even tried replacing the apostrophe with a replacement string such as {APOS} to see if it's an escape problem. But the problem still occurs afterwards.

The platform I am using while debugging and testing on is Windows 10 20H2 OS Build 19042.685
luvit version: 2.17.0
luvi version: v2.10.1
libuv version: 1.34.1
rex version: 8.37 2015-04-28
winsvc version: 1.0.0
ssl version: OpenSSL 1.1.1d 10 Sep 2019, lua-openssl 0.7.7

(Added to original post)