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

Commit 27cd8d5

Browse files
committed
Add conflict errors for duplicate registrations
1 parent b16a906 commit 27cd8d5

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
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: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,13 @@ func CreateCurrentUserRegistration(w http.ResponseWriter, r *http.Request) {
126126
err = service.CreateUserRegistration(id, user_registration)
127127

128128
if err != nil {
129-
errors.WriteError(w, r, errors.InternalError(err.Error(), "Could not create user registration."))
129+
var apiError errors.ApiError
130+
if err.Error() == service.RegistrationAlreadyExists {
131+
apiError = errors.ConflictError(err.Error(), "Could not create user registration.")
132+
} else {
133+
apiError = errors.InternalError(err.Error(), "Could not create user registration.")
134+
}
135+
errors.WriteError(w, r, apiError)
130136
return
131137
}
132138

@@ -324,7 +330,13 @@ func CreateCurrentMentorRegistration(w http.ResponseWriter, r *http.Request) {
324330
err = service.CreateMentorRegistration(id, mentor_registration)
325331

326332
if err != nil {
327-
errors.WriteError(w, r, errors.InternalError(err.Error(), "Could not create mentor registration."))
333+
var apiError errors.ApiError
334+
if err.Error() == service.RegistrationAlreadyExists {
335+
apiError = errors.ConflictError(err.Error(), "Could not create mentor registration.")
336+
} else {
337+
apiError = errors.InternalError(err.Error(), "Could not create mentor registration.")
338+
}
339+
errors.WriteError(w, r, apiError)
328340
return
329341
}
330342

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)