follow fail pattern for addUser: throw on bad arg, nil on bad result

closes #2156
This commit is contained in:
payonel 2018-02-20 07:44:41 -08:00
parent e91b8c0971
commit f11d384b51

View File

@ -77,15 +77,18 @@ class ComputerAPI(owner: NativeLuaArchitecture) extends NativeLuaAPI(owner) {
}) })
lua.setField(-2, "users") lua.setField(-2, "users")
lua.pushScalaFunction(lua => try { lua.pushScalaFunction(lua => {
machine.addUser(lua.checkString(1)) val user = lua.checkString(1)
lua.pushBoolean(true) try {
1 machine.addUser(user)
} catch { lua.pushBoolean(true)
case e: Throwable => 1
lua.pushNil() } catch {
lua.pushString(Option(e.getMessage).getOrElse(e.toString)) case e: Throwable =>
2 lua.pushNil()
lua.pushString(Option(e.getMessage).getOrElse(e.toString))
2
}
}) })
lua.setField(-2, "addUser") lua.setField(-2, "addUser")