Fix fm-rest-api returning incorrect error codes

Some alarm and event exceptions in fm-rest-api are returning
HTTP 500 instead of the correct status code. This issue is related
to the exception class being used.

This commit corrects the returned value and prevents the default
exception from being generated.

Test Plan:
PASS: Apply the changes to the system. Use the 'fm alarm-show' or
      'event-show' commands with a non-existent UUID. Verify that
      the correct code is returned in both logs and console output.
PASS: Force other errors, such as invalid parameters (400) and
      unauthorized access (401), and verify that the returned code
      matches the expected value.

closes-bug: 2080697

Change-Id: Ie91dc3bba8efa0e1e47678f18710c83d97eb2905
Signed-off-by: fperez <fabrizio.perez@windriver.com>
This commit is contained in:
fperez 2024-09-13 19:49:58 -03:00
parent fc97fdbe58
commit 448bb06c3b

@ -31,6 +31,7 @@ class ApiError(Exception):
try:
super(ApiError, self).__init__(self.message % kwargs)
LOG.debug(f"AlarmNotFound initialized with code: {self.kwargs.get('code', self.code)}")
self.message = self.message % kwargs
except Exception:
LOG.exception('Exception in string format operation, '
@ -38,7 +39,7 @@ class ApiError(Exception):
raise
def __str__(self):
return repr(self.value)
return f"[HTTP {self.code}] {self.message % self.kwargs}"
def __unicode__(self):
return self.message