feat(captain): validate custom tools

This commit is contained in:
2026-06-07 16:20:39 +08:00
parent dd7bcf4118
commit 8f9adecd04
5 changed files with 213 additions and 12 deletions
@@ -47,6 +47,9 @@ func (h *CaptainCustomToolHandler) Create(c *gin.Context) {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": service.ErrCaptainCustomToolLimitExceeded.Error()})
return
}
if renderCaptainCustomToolValidationError(c, err) {
return
}
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to create custom tool")
return
}
@@ -107,6 +110,9 @@ func (h *CaptainCustomToolHandler) Update(c *gin.Context) {
tool, err := h.svc.UpdateByAccount(c.Request.Context(), accountID, id, &req)
if err != nil {
applogger.L().Errorf("Update captain custom tool: %v", err)
if renderCaptainCustomToolValidationError(c, err) {
return
}
handleServiceError(c, err)
return
}
@@ -227,6 +233,18 @@ func (h *CaptainCustomToolHandler) ensureCustomToolsEnabled(c *gin.Context, acco
return false
}
func renderCaptainCustomToolValidationError(c *gin.Context, err error) bool {
var validationErr *service.CaptainCustomToolValidationError
if !errors.As(err, &validationErr) {
return false
}
c.JSON(http.StatusUnprocessableEntity, gin.H{
"message": validationErr.Message,
"attributes": validationErr.Attributes,
})
return true
}
func captainCustomToolPayload(tool *model.CaptainCustomTool) gin.H {
return gin.H{
"id": tool.ID,