mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-21 22:53:37 +00:00
lua: Add some missing features.
- Remove default error handler. - User cancel. - Add the errno to the object which is thrown on error. Still no events.
This commit is contained in:
@@ -114,6 +114,8 @@ lua_guestfs_create (lua_State *L)
|
||||
if (!g)
|
||||
return luaL_error (L, \"Guestfs.create: cannot create handle: %%m\");
|
||||
|
||||
guestfs_set_error_handler (g, NULL, NULL);
|
||||
|
||||
u = lua_newuserdata (L, sizeof (struct userdata));
|
||||
luaL_getmetatable (L, LUA_GUESTFS_HANDLE);
|
||||
lua_setmetatable (L, -2);
|
||||
@@ -151,6 +153,18 @@ lua_guestfs_close (lua_State *L)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* User cancel. */
|
||||
static int
|
||||
lua_guestfs_user_cancel (lua_State *L)
|
||||
{
|
||||
struct userdata *u = get_handle (L, 1);
|
||||
|
||||
if (u->g)
|
||||
guestfs_user_cancel (u->g);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
";
|
||||
|
||||
List.iter (
|
||||
@@ -297,18 +311,29 @@ lua_guestfs_close (lua_State *L)
|
||||
) optargs;
|
||||
|
||||
(* Handle errors. *)
|
||||
let raise_error () =
|
||||
pr " lua_newtable (L);\n";
|
||||
pr " lua_pushliteral (L, \"msg\");\n";
|
||||
pr " lua_pushstring (L, guestfs_last_error (g));\n";
|
||||
pr " lua_settable (L, -3);\n";
|
||||
pr " lua_pushliteral (L, \"code\");\n";
|
||||
pr " lua_pushinteger (L, guestfs_last_errno (g));\n";
|
||||
pr " lua_settable (L, -3);\n";
|
||||
pr " return lua_error (L);\n"
|
||||
in
|
||||
|
||||
(match errcode_of_ret ret with
|
||||
| `CannotReturnError -> ()
|
||||
| `ErrorIsMinusOne ->
|
||||
pr " if (r == -1)\n";
|
||||
pr " return luaL_error (L, \"Guestfs.%%s: %%s\",\n";
|
||||
pr " \"%s\", guestfs_last_error (g));\n" name;
|
||||
pr " if (r == -1) {\n";
|
||||
raise_error ();
|
||||
pr " }\n";
|
||||
pr "\n"
|
||||
| `ErrorIsNULL ->
|
||||
pr " if (r == NULL)\n";
|
||||
pr " return luaL_error (L, \"Guestfs.%%s: %%s\",\n";
|
||||
pr " \"%s\", guestfs_last_error (g));\n" name;
|
||||
pr "\n";
|
||||
pr " if (r == NULL) {\n";
|
||||
raise_error ();
|
||||
pr " }\n";
|
||||
pr "\n"
|
||||
);
|
||||
|
||||
(* Push return value on the stack. *)
|
||||
@@ -496,6 +521,7 @@ static luaL_Reg handle_methods[] = {
|
||||
{ \"__gc\", lua_guestfs_finalizer },
|
||||
{ \"create\", lua_guestfs_create },
|
||||
{ \"close\", lua_guestfs_close },
|
||||
{ \"user_cancel\", lua_guestfs_user_cancel },
|
||||
|
||||
";
|
||||
|
||||
|
||||
@@ -65,7 +65,23 @@ properly. We hope to come up with a better solution later.
|
||||
|
||||
=head2 ERRORS
|
||||
|
||||
Errors are converted into exceptions. Use C<pcall> to catch these.
|
||||
Most (but not all) errors are converted into objects (ie. tables)
|
||||
containing the following fields:
|
||||
|
||||
=over 4
|
||||
|
||||
=item msg
|
||||
|
||||
The error message (corresponding to L<guestfs(3)/guestfs_last_error>).
|
||||
|
||||
=item code
|
||||
|
||||
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.
|
||||
|
||||
=head1 EXAMPLE 1: CREATE A DISK IMAGE
|
||||
|
||||
|
||||
Reference in New Issue
Block a user