dpgaspar commented on a change in pull request #13960:
URL: https://github.com/apache/superset/pull/13960#discussion_r607640450



##########
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:
       Are we always going to throw HTTP 500 on these? would it make sense that 
the exception itself could identify the HTTP status code?

##########
File path: superset/databases/api.py
##########
@@ -604,13 +609,8 @@ def test_connection(  # pylint: 
disable=too-many-return-statements
         # This validates custom Schema with custom validations
         except ValidationError as error:
             return self.response_400(message=error.messages)
-        try:
-            TestConnectionDatabaseCommand(g.user, item).run()
-            return self.response(200, message="OK")
-        except DatabaseTestConnectionFailedError as ex:
-            return self.response_422(message=str(ex))
-        except SupersetErrorException as ex:
-            return self.response(ex.status, message=ex.error.message)
+        TestConnectionDatabaseCommand(g.user, item).run()
+        return self.response(200, message="OK")

Review comment:
       the OpenAPI error responses are not correct now, on the 
`BaseSupersetModelRestApi` we can declare the new SIP-40 schema 
https://github.com/dpgaspar/Flask-AppBuilder/blob/master/flask_appbuilder/api/__init__.py#L296
   




-- 
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]

Reply via email to