zephyring commented on code in PR #24964:
URL: https://github.com/apache/superset/pull/24964#discussion_r1309102067


##########
superset/tags/api.py:
##########
@@ -201,6 +203,70 @@ def post(self) -> Response:
             )
             return self.response_500(message=str(ex))
 
+    @expose("/bulk_create", methods=("POST",))
+    @protect()
+    @safe
+    @statsd_metrics
+    @event_logger.log_this_with_context(
+        action=lambda self, *args, **kwargs: 
f"{self.__class__.__name__}.bulk_create",
+        log_to_statsd=False,
+    )
+    def bulk_create(self) -> Response:
+        """Bulk tag items
+        ---
+        post:
+          description: >-
+            Bulk tag items
+          requestBody:
+            description: Tag schema
+            required: true
+            content:
+              application/json:
+                schema:
+                  $ref: 
'#/components/schemas/{{self.__class__.__name__}}.bulk_create'
+          responses:
+            201:
+              description: Tag added
+              content:
+                application/json:
+                  schema:
+                    type: object
+                    properties:
+                      id:
+                        type: number
+                      result:
+                        $ref: 
'#/components/schemas/{{self.__class__.__name__}}.bulk_create'
+            400:
+              $ref: '#/components/responses/400'
+            401:
+              $ref: '#/components/responses/401'
+            422:
+              $ref: '#/components/responses/422'
+            500:
+              $ref: '#/components/responses/500'
+        """
+        try:
+            item = TagPostBulkSchema().load(request.json)
+        except ValidationError as error:
+            return self.response_400(message=error.messages)
+        try:
+            for tag in item.get("tags"):
+                item = self.add_model_schema.load(
+                    {"name": tag, "objects_to_tag": item.get("objects_to_tag")}
+                )
+                CreateCustomTagWithRelationshipsCommand(item).run()
+            return self.response(201)
+        except TagInvalidError as ex:
+            return self.response_422(message=ex.message)
+        except TagCreateFailedError as ex:
+            logger.error(
+                "Error creating model %s: %s",
+                self.__class__.__name__,
+                str(ex),
+                exc_info=True,
+            )
+            return self.response_500(message=str(ex))

Review Comment:
   catching an exception and returning `response_500` is anti-patterns. We 
already have the `safe` annotation to convert uncaught exception to 500.
   This is an indication that `TagCreateFailedError` is not specific enough to 
describe the unique failure scenario.
   I would suggest to split `TagCreateFailedError` up to match with different 
failure case and catch each individual one.
   And remove all `response_500` from API layer



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

To unsubscribe, e-mail: [email protected]

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