From 2d4f3da09072b2c6df76f7f720084b1a314c6946 Mon Sep 17 00:00:00 2001 From: Andrey Kurilin Date: Thu, 8 Aug 2019 15:45:10 +0300 Subject: [PATCH] Fix usage of NotFound exception All built-in inheritors of osc_lib.exceptions.ClientException expects 3 positional arguments: first argument is code, the second one is message. osc_lib.api.api.BaseAPI raises NotFound exception with transmitting only message arguments which goes to position of 'code' argument. This patch adds `code=404` argument and passes actual message as 'message'. Also, this patch adds a hack that processes code as message n case when code is not an integer and None was found at 'message' argument. Funny fact: More than 4 years ago, I proposed a similar fix to oslo-incubator... it was so long ago... https://review.opendev.org/#/c/94884 :) Change-Id: I022bbf3ce4bdd48b3bdcb008ee872c4f62a7b12b --- osc_lib/api/api.py | 4 ++-- osc_lib/exceptions.py | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/osc_lib/api/api.py b/osc_lib/api/api.py index 31d223f..aeb36d3 100644 --- a/osc_lib/api/api.py +++ b/osc_lib/api/api.py @@ -361,7 +361,7 @@ class BaseAPI(object): num_bulk = len(bulk_list) if num_bulk == 0: msg = _("none found") - raise exceptions.NotFound(msg) + raise exceptions.NotFound(404, msg) elif num_bulk > 1: msg = _("many found") raise RuntimeError(msg) @@ -388,7 +388,7 @@ class BaseAPI(object): def raise_not_found(): msg = _("%s not found") % value - raise exceptions.NotFound(msg) + raise exceptions.NotFound(404, msg) try: ret = self._request( diff --git a/osc_lib/exceptions.py b/osc_lib/exceptions.py index ca5a0ea..4373cee 100644 --- a/osc_lib/exceptions.py +++ b/osc_lib/exceptions.py @@ -55,6 +55,9 @@ class ClientException(Exception): """The base exception class for all exceptions this library raises.""" def __init__(self, code, message=None, details=None): + if not isinstance(code, int) and message is None: + message = code + code = self.http_status self.code = code self.message = message or self.__class__.message self.details = details