Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

readFile and writeFile do not return an error #18

Open
MorganPeterson opened this issue Aug 21, 2020 · 0 comments
Open

readFile and writeFile do not return an error #18

MorganPeterson opened this issue Aug 21, 2020 · 0 comments

Comments

@MorganPeterson
Copy link

MorganPeterson commented Aug 21, 2020

Lua 5.4.0 on Arch Linux

Current readPath and writePath follow a pattern that if the file does not exist it wraps the io.open return value in an assert which then calls Lua's error stack. In one instance this caused me to have to write my own file handling. It would be nice if readFile and writeFile would return nil or an error so I could handle it myself.

A contrived example of the case:

binser = require("binser")
t = { data = "someData"}
data = binser.readFile("myfile")

if data then
  data[#data + 1] = t
end

binser.writeFile("myFile", table.unpack(data))

In my opinion, what I would like to see from readFile/writeFile might be something like this:

local function readFile(path)
  local file, err = io.open(path, "rb")
  if not file then return nil end
  -- assert(file, err)
  local str = file:read("*all")
  file:close()
  return deserialize(str)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant