pandoc.lua: throw better error when pipe command fails

A table containing the error code, command, and command output is thrown
instead of just a string error message.
This commit is contained in:
Albert Krewinkel 2017-10-05 11:41:59 +02:00
parent 2262f005ce
commit 12f8efe012
No known key found for this signature in database
GPG key ID: 388DC0B21F631124
3 changed files with 11 additions and 6 deletions

View file

@ -792,6 +792,8 @@ end
--- Runs command with arguments, passing it some input, and returns the output.
-- @treturn string Output of command.
-- @raise A table containing the keys `command`, `error_code`, and `output` is
-- thrown if the command exits with a non-zero error code.
-- @usage
-- local ec, output = pandoc.pipe("sed", {"-e","s/a/b/"}, "abc")
function M.pipe (command, args, input)
@ -806,9 +808,7 @@ function M.pipe (command, args, input)
end
}
)
-- TODO: drop the wrapping call to `tostring` as soon as hslua supports
-- non-string error objects.
error(tostring(err))
error(err)
end
return output
end

View file

@ -1132,6 +1132,12 @@ Lua functions for pandoc scripts.
- Output of command.
Raises:
- A table containing the keys `command`, `error_code`, and
`output` is thrown if the command exits with a non-zero
error code.
Usage:
local output = pandoc.pipe("sed", {"-e","s/a/b/"}, "abc")

View file

@ -196,9 +196,8 @@ runFilterFunction lf x = do
push x
z <- Lua.pcall 1 1 Nothing
when (z /= OK) $ do
msg <- Lua.peek (-1) <* Lua.pop 1
let prefix = "Error while running filter function: "
Lua.throwLuaError $ prefix ++ msg
let addPrefix = ("Error while running filter function: " ++)
Lua.throwTopMessageAsError' addPrefix
elementOrList :: FromLuaStack a => a -> Lua [a]
elementOrList x = do