Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2ef2f97

Browse files
authored
chore: improve error message on adding existing org_member (#13621)
1 parent889daf2 commit2ef2f97

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

‎coderd/database/dbmem/dbmem.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6197,6 +6197,20 @@ func (q *FakeQuerier) InsertOrganizationMember(_ context.Context, arg database.I
61976197
q.mutex.Lock()
61986198
deferq.mutex.Unlock()
61996199

6200+
ifslices.IndexFunc(q.data.organizationMembers,func(member database.OrganizationMember)bool {
6201+
returnmember.OrganizationID==arg.OrganizationID&&member.UserID==arg.UserID
6202+
})>=0 {
6203+
// Error pulled from a live db error
6204+
return database.OrganizationMember{},&pq.Error{
6205+
Severity:"ERROR",
6206+
Code:"23505",
6207+
Message:"duplicate key value violates unique constraint\"organization_members_pkey\"",
6208+
Detail:"Key (organization_id, user_id)=(f7de1f4e-5833-4410-a28d-0a105f96003f, 36052a80-4a7f-4998-a7ca-44cefa608d3e) already exists.",
6209+
Table:"organization_members",
6210+
Constraint:"organization_members_pkey",
6211+
}
6212+
}
6213+
62006214
//nolint:gosimple
62016215
organizationMember:= database.OrganizationMember{
62026216
OrganizationID:arg.OrganizationID,

‎coderd/members.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ func (api *API) postOrganizationMember(rw http.ResponseWriter, r *http.Request)
4343
httpapi.ResourceNotFound(rw)
4444
return
4545
}
46+
ifdatabase.IsUniqueViolation(err,database.UniqueOrganizationMembersPkey) {
47+
httpapi.Write(ctx,rw,http.StatusBadRequest, codersdk.Response{
48+
Message:"Organization member already exists in this organization",
49+
})
50+
return
51+
}
4652
iferr!=nil {
4753
httpapi.InternalServerError(rw,err)
4854
return

‎coderd/members_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ func TestAddMember(t *testing.T) {
5050
db2sdk.List(members,onlyIDs))
5151
})
5252

53+
t.Run("AlreadyMember",func(t*testing.T) {
54+
t.Parallel()
55+
owner:=coderdtest.New(t,nil)
56+
first:=coderdtest.CreateFirstUser(t,owner)
57+
_,user:=coderdtest.CreateAnotherUser(t,owner,first.OrganizationID)
58+
59+
ctx:=testutil.Context(t,testutil.WaitMedium)
60+
// Add user to org, even though they already exist
61+
// nolint:gocritic // must be an owner to see the user
62+
_,err:=owner.PostOrganizationMember(ctx,first.OrganizationID,user.Username)
63+
require.ErrorContains(t,err,"already exists")
64+
})
65+
5366
t.Run("UserNotExists",func(t*testing.T) {
5467
t.Parallel()
5568
owner:=coderdtest.New(t,nil)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp