feat(agents): align duplicate create errors

This commit is contained in:
2026-06-06 02:31:02 +08:00
parent dd2bdee470
commit 8b30cc78a0
3 changed files with 16 additions and 7 deletions
+4 -1
View File
@@ -111,7 +111,10 @@ func (h *AgentHandler) Create(c *gin.Context) {
agent, svcErr := h.svc.Create(c.Request.Context(), accountID, userID, req)
if svcErr != nil {
if errors.Is(svcErr, repository.ErrAlreadyMember) {
response.AbortWithStatusError(c, http.StatusConflict, response.ErrConflict, svcErr.Error())
c.JSON(http.StatusUnprocessableEntity, gin.H{
"message": "User has already been taken",
"attributes": []string{"user_id"},
})
return
}
applogger.L().Errorf("Create agent for account %d: %v", accountID, svcErr)
@@ -190,10 +190,14 @@ func (s *AgentHandlerTestSuite) TestCreateAgentDuplicate() {
s.handler.Create(c)
assert.Equal(s.T(), http.StatusOK, w.Code)
// Try creating again — should conflict
// Try creating again — Chatwoot renders ActiveRecord::RecordInvalid as 422.
w2, c2 := s.makeRequest("POST", "/api/v1/accounts/1/agents", req, s.account.ID, s.user.ID)
s.handler.Create(c2)
assert.Equal(s.T(), http.StatusConflict, w2.Code)
assert.Equal(s.T(), http.StatusUnprocessableEntity, w2.Code)
var data map[string]interface{}
json.Unmarshal(w2.Body.Bytes(), &data)
assert.Equal(s.T(), "User has already been taken", data["message"])
assert.Equal(s.T(), []interface{}{"user_id"}, data["attributes"])
}
func (s *AgentHandlerTestSuite) TestCreateAgentValidation() {