From a471ad2a6a3a958d7a5ad9716308f5837241468b Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Mon, 20 Jan 2020 12:02:26 +0800 Subject: [PATCH] mgr: better error handling when reading option this also silences the warning of src/mgr/BaseMgrModule.cc:402:9: warning: unused variable 'r' [-Wunused-variable] int r = g_conf().get_val(string(what), &value); ^ Signed-off-by: Kefu Chai --- src/mgr/BaseMgrModule.cc | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/mgr/BaseMgrModule.cc b/src/mgr/BaseMgrModule.cc index baa8426a42a..ebdec8f0503 100644 --- a/src/mgr/BaseMgrModule.cc +++ b/src/mgr/BaseMgrModule.cc @@ -399,13 +399,23 @@ ceph_option_get(BaseMgrModule *self, PyObject *args) const Option *opt = g_conf().find_option(string(what)); if (opt) { std::string value; - int r = g_conf().get_val(string(what), &value); - assert(r >= 0); + switch (int r = g_conf().get_val(string(what), &value); r) { + case -ENOMEM: + PyErr_NoMemory(); + return nullptr; + case -ENAMETOOLONG: + PyErr_SetString(PyExc_ValueError, "value too long"); + return nullptr; + default: + ceph_assert(r == 0); + break; + } dout(10) << "ceph_option_get " << what << " found: " << value << dendl; return get_python_typed_option_value(opt->type, value); } else { dout(4) << "ceph_option_get " << what << " not found " << dendl; - Py_RETURN_NONE; + PyErr_Format(PyExc_KeyError, "option not found: %s", what); + return nullptr; } } -- 2.47.3