* @brief Pushes the CommandResponse onto the Lua stack as a result table. */
| 565 | * @brief Pushes the CommandResponse onto the Lua stack as a result table. |
| 566 | */ |
| 567 | static void pushApiResult(lua_State* L, const API::CommandResponse& r) |
| 568 | { |
| 569 | lua_createtable(L, 0, r.success ? 2 : 4); |
| 570 | |
| 571 | lua_pushboolean(L, r.success ? 1 : 0); |
| 572 | lua_setfield(L, -2, "ok"); |
| 573 | |
| 574 | if (r.success) { |
| 575 | if (!r.result.isEmpty()) { |
| 576 | jsonObjectToLua(L, r.result, 0); |
| 577 | lua_setfield(L, -2, "result"); |
| 578 | } |
| 579 | } else { |
| 580 | const QByteArray msg = r.errorMessage.toUtf8(); |
| 581 | lua_pushlstring(L, msg.constData(), static_cast<size_t>(msg.size())); |
| 582 | lua_setfield(L, -2, "error"); |
| 583 | |
| 584 | const QByteArray code = r.errorCode.toUtf8(); |
| 585 | lua_pushlstring(L, code.constData(), static_cast<size_t>(code.size())); |
| 586 | lua_setfield(L, -2, "errorCode"); |
| 587 | |
| 588 | if (!r.errorData.isEmpty()) { |
| 589 | jsonObjectToLua(L, r.errorData, 0); |
| 590 | lua_setfield(L, -2, "errorData"); |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * @brief Pushes an early-failure {ok = false, error = msg[, errorCode]} table onto the Lua stack. |
no test coverage detected