betodealmeida commented on a change in pull request #13960:
URL: https://github.com/apache/superset/pull/13960#discussion_r607990331
##########
File path: superset/views/base_api.py
##########
@@ -84,6 +88,72 @@ def wraps(self: "BaseSupersetModelRestApi", *args: Any,
**kwargs: Any) -> Respon
return functools.update_wrapper(wraps, f)
+def get_level(status: int) -> ErrorLevel:
+ if status < 400:
+ return ErrorLevel.INFO
+ if status < 500:
+ return ErrorLevel.WARNING
+ return ErrorLevel.ERROR
+
+
+def handle_exception(f: Callable[..., FlaskResponse]) -> Callable[...,
FlaskResponse]:
+ """
+ A decorator that formats exceptions.
+
+ SIP-40 (https://github.com/apache/superset/issues/9194) introduced
+ a standard error payload for all API errors returned by Superset.
+ This decorator formats exceptions to conform to it.
+ """
+
+ def wraps(self: Any, *args: Any, **kwargs: Any) -> FlaskResponse:
+ try:
+ return f(self, *args, **kwargs)
+ except SupersetErrorException as ex:
+ logger.warning(ex)
+ return json_errors_response(errors=[ex.error], status=ex.status)
+ except SupersetErrorsException as ex:
+ logger.warning(ex)
+ return json_errors_response(errors=ex.errors, status=ex.status)
+ except CommandInvalidError as ex:
+ logger.warning(ex)
+ return json_errors_response(
+ errors=[
+ SupersetError(
Review comment:
I don't think that command exceptions should have HTTP status codes,
they should care only about business logic and be decoupled from the API.
Ideally the API layer should do the mapping between command exceptions to
`SupersetError(s)Exception`. The reason why I added `CommandInvalidError` and
`CommandException` is so we can gradually implement that mapping, while in the
meantime any non-covered command exceptions return a 500.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]