Skip to content
This repository was archived by the owner on Dec 7, 2023. It is now read-only.

Commit 49b6081

Browse files
committed
Add conflict errors for duplicate registrations
1 parent b16a906 commit 49b6081

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

common/errors/ConflictError.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package errors
2+
3+
import "net/http"
4+
5+
// An error that occurs when an incoming request tries to create a resource that already exists.
6+
func ConflictError(raw_error string, message string) ApiError {
7+
return ApiError{Status: http.StatusConflict, Type: "CONFLICT_ERROR", Message: message, RawError: raw_error}
8+
}

services/registration/controller/controller.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ func CreateCurrentUserRegistration(w http.ResponseWriter, r *http.Request) {
126126
err = service.CreateUserRegistration(id, user_registration)
127127

128128
if err != nil {
129+
if err.Error() == service.RegistrationAlreadyExists {
130+
errors.WriteError(w, r, errors.ConflictError(err.Error(), "Could not create user registration."))
131+
return
132+
}
129133
errors.WriteError(w, r, errors.InternalError(err.Error(), "Could not create user registration."))
130134
return
131135
}
@@ -324,6 +328,10 @@ func CreateCurrentMentorRegistration(w http.ResponseWriter, r *http.Request) {
324328
err = service.CreateMentorRegistration(id, mentor_registration)
325329

326330
if err != nil {
331+
if err.Error() == service.RegistrationAlreadyExists {
332+
errors.WriteError(w, r, errors.ConflictError(err.Error(), "Could not create mentor registration."))
333+
return
334+
}
327335
errors.WriteError(w, r, errors.InternalError(err.Error(), "Could not create mentor registration."))
328336
return
329337
}

services/registration/service/registration_service.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ var validate *validator.Validate
1414

1515
var db database.Database
1616

17+
const RegistrationAlreadyExists = "Registration already exists."
18+
1719
func Initialize() error {
1820
if db != nil {
1921
db.Close()
@@ -64,7 +66,7 @@ func CreateUserRegistration(id string, user_registration models.UserRegistration
6466
if err != nil {
6567
return err
6668
}
67-
return errors.New("Registration already exists.")
69+
return errors.New(RegistrationAlreadyExists)
6870
}
6971

7072
err = db.Insert("attendees", &user_registration)
@@ -189,7 +191,7 @@ func CreateMentorRegistration(id string, mentor_registration models.MentorRegist
189191
if err != nil {
190192
return err
191193
}
192-
return errors.New("Registration already exists")
194+
return errors.New(RegistrationAlreadyExists)
193195
}
194196

195197
err = db.Insert("mentors", &mentor_registration)

0 commit comments

Comments
 (0)