feat(captain): validate custom tools
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user