diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2012-11-17 21:17:44 +0000 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2012-11-17 21:28:43 +0000 |
commit | 4ba8b5a00d7336e87e15a854b0534c630f456f7c (patch) | |
tree | 3cab9dc103d887dd00f314435521969703af5be9 | |
parent | a9c4efdcfd213202c9c9b478e92fe908a700b957 (diff) | |
download | libguestfs-4ba8b5a00d7336e87e15a854b0534c630f456f7c.tar.gz libguestfs-4ba8b5a00d7336e87e15a854b0534c630f456f7c.tar.xz libguestfs-4ba8b5a00d7336e87e15a854b0534c630f456f7c.zip |
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.
-rw-r--r-- | generator/lua.ml | 40 | ||||
-rw-r--r-- | lua/examples/guestfs-lua.pod | 18 |
2 files changed, 50 insertions, 8 deletions
diff --git a/generator/lua.ml b/generator/lua.ml index 4f652e96..b561b3c1 100644 --- a/generator/lua.ml +++ b/generator/lua.ml @@ -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 }, "; diff --git a/lua/examples/guestfs-lua.pod b/lua/examples/guestfs-lua.pod index 33c9b811..b7d167c9 100644 --- a/lua/examples/guestfs-lua.pod +++ b/lua/examples/guestfs-lua.pod @@ -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 |