diff options
author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2021-01-29 12:28:16 +0300 |
---|---|---|
committer | Thijs Schreijer <thijs@thijsschreijer.nl> | 2021-01-31 10:14:59 +0300 |
commit | 05555c6913285deeacc525560c7e773b2e42f112 (patch) | |
tree | 9600cd3b44632cb20fbc3167e7d5517b56ea8ccd /lua | |
parent | 64093619cf37591bc57f6799d2019cc066c097dc (diff) |
feat(path) wrap lfs functions to provide better error message
Diffstat (limited to 'lua')
-rw-r--r-- | lua/pl/path.lua | 55 |
1 files changed, 49 insertions, 6 deletions
diff --git a/lua/pl/path.lua b/lua/pl/path.lua index 5204056..66d83f6 100644 --- a/lua/pl/path.lua +++ b/lua/pl/path.lua @@ -32,6 +32,13 @@ local link_attrib = lfs.symlinkattributes local path = {} +local function err_func(name, param, err, code) + if code == nil then + return ("%s failed for '%s': %s"):format(tostring(name), tostring(param), tostring(err)) + end + return ("%s failed for '%s': %s (code %s)"):format(tostring(name), tostring(param), tostring(err), tostring(code)) +end + --- Lua iterator over the entries of a given directory. -- Implicit link to [`luafilesystem.dir`](https://keplerproject.github.io/luafilesystem/manual.html#reference) -- @function dir @@ -40,34 +47,70 @@ path.dir = lfs.dir --- Creates a directory. -- Implicit link to [`luafilesystem.mkdir`](https://keplerproject.github.io/luafilesystem/manual.html#reference) -- @function mkdir -path.mkdir = lfs.mkdir +path.mkdir = function(d) + local ok, err, code = lfs.mkdir(d) + if not ok then + return ok, err_func("mkdir", d, err, code), code + end + return ok, err, code +end --- Removes a directory. -- Implicit link to [`luafilesystem.rmdir`](https://keplerproject.github.io/luafilesystem/manual.html#reference) -- @function rmdir -path.rmdir = lfs.rmdir +path.rmdir = function(d) + local ok, err, code = lfs.rmdir(d) + if not ok then + return ok, err_func("rmdir", d, err, code), code + end + return ok, err, code +end --- Gets attributes. -- Implicit link to [`luafilesystem.attributes`](https://keplerproject.github.io/luafilesystem/manual.html#reference) -- @function attrib -path.attrib = attrib +path.attrib = function(d, r) + local ok, err, code = attrib(d, r) + if not ok then + return ok, err_func("attrib", d, err, code), code + end + return ok, err, code +end --- Get the working directory. -- Implicit link to [`luafilesystem.currentdir`](https://keplerproject.github.io/luafilesystem/manual.html#reference) -- @function currentdir -path.currentdir = currentdir +path.currentdir = function(d) + local ok, err, code = currentdir(d) + if not ok then + return ok, err_func("currentdir", d, err, code), code + end + return ok, err, code +end --- Gets symlink attributes. -- Implicit link to [`luafilesystem.symlinkattributes`](https://keplerproject.github.io/luafilesystem/manual.html#reference) -- @function link_attrib -path.link_attrib = link_attrib +path.link_attrib = function(d, r) + local ok, err, code = link_attrib(d, r) + if not ok then + return ok, err_func("link_attrib", d, err, code), code + end + return ok, err, code +end --- Changes the working directory. -- On Windows, if a drive is specified, it also changes the current drive. If -- only specifying the drive, it will only switch drive, but not modify the path. -- Implicit link to [`luafilesystem.chdir`](https://keplerproject.github.io/luafilesystem/manual.html#reference) -- @function chdir -path.chdir = lfs.chdir +path.chdir = function(d) + local ok, err, code = lfs.chdir(d) + if not ok then + return ok, err_func("chdir", d, err, code), code + end + return ok, err, code +end --- is this a directory? -- @string P A file path |