lua: Attach __tostring functions exceptions.

This includes a test.
This commit is contained in:
Richard W.M. Jones
2012-11-20 17:32:37 +00:00
parent 4749bff379
commit 1f98450995
4 changed files with 77 additions and 5 deletions

View File

@@ -50,7 +50,7 @@ guestfs.so: libluaguestfs.la
ln -sf .libs/libluaguestfs.so $@
# Tests.
TESTS_ENVIRONMENT = $(top_builddir)/run --test
TESTS_ENVIRONMENT = LC_ALL=C $(top_builddir)/run --test
TESTS = \
run-bindtests \
tests/010-load.lua \
@@ -60,7 +60,8 @@ TESTS = \
tests/027-create-multiple.lua \
tests/030-config.lua \
tests/070-optargs.lua \
tests/400-events.lua
tests/400-events.lua \
tests/900-errors.lua
if ENABLE_APPLIANCE
TESTS += \
@@ -80,7 +81,8 @@ EXTRA_DIST += \
tests/060-readdir.lua \
tests/070-optargs.lua \
tests/400-events.lua \
tests/400-progress.lua
tests/400-progress.lua \
tests/900-errors.lua
# Custom install rule.
install-data-hook:

View File

@@ -90,8 +90,12 @@ The C<errno> (corresponding to L<guestfs(3)/guestfs_last_errno>).
=back
Note that some errors can also be thrown as plain strings. You
need to check the type.
These objects also have C<__tostring> functions attached to them
so you can use C<tostring> (or implicit conversion) to convert them
into printable strings.
Note that the library also throws some errors as plain strings. You
may need to check the type.
=head2 EVENTS

37
lua/tests/900-errors.lua Executable file
View File

@@ -0,0 +1,37 @@
#!/usr/bin/lua
-- libguestfs Lua bindings -*- lua -*-
-- Copyright (C) 2012 Red Hat Inc.
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
local G = require "guestfs"
local g = G.create ()
code, err = pcall (function (fn) g:add_drive (fn) end, "/NOTSUCHFILE")
assert (not code)
-- The default __tostring function will convert the error to either:
--
-- "%s", msg
-- or:
-- "%s: %s", msg, strerror (err)
--
-- We are expecting the second case here, but since the libguestfs
-- code calls perrorf, the string version of ENOENT is already
-- included in 'msg' and so appears twice here.
local str = tostring (err)
assert (str == "/NOTSUCHFILE: No such file or directory: No such file or directory",
string.format ("unexpected error string: %s", str))