Skip to content
This repository was archived by the owner on Dec 7, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions common/errors/ConflictError.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package errors

import "net/http"

// An error that occurs when an incoming request tries to create a resource that already exists.
func ConflictError(raw_error string, message string) ApiError {
return ApiError{Status: http.StatusConflict, Type: "CONFLICT_ERROR", Message: message, RawError: raw_error}
}
16 changes: 14 additions & 2 deletions services/registration/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,13 @@ func CreateCurrentUserRegistration(w http.ResponseWriter, r *http.Request) {
err = service.CreateUserRegistration(id, user_registration)

if err != nil {
errors.WriteError(w, r, errors.InternalError(err.Error(), "Could not create user registration."))
var apiError errors.ApiError
if err.Error() == service.RegistrationAlreadyExists {
apiError = errors.ConflictError(err.Error(), "Could not create user registration.")
} else {
apiError = errors.InternalError(err.Error(), "Could not create user registration.")
}
errors.WriteError(w, r, apiError)
return
}

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

if err != nil {
errors.WriteError(w, r, errors.InternalError(err.Error(), "Could not create mentor registration."))
var apiError errors.ApiError
if err.Error() == service.RegistrationAlreadyExists {
apiError = errors.ConflictError(err.Error(), "Could not create mentor registration.")
} else {
apiError = errors.InternalError(err.Error(), "Could not create mentor registration.")
}
errors.WriteError(w, r, apiError)
return
}

Expand Down
6 changes: 4 additions & 2 deletions services/registration/service/registration_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ var validate *validator.Validate

var db database.Database

const RegistrationAlreadyExists = "Registration already exists."

func Initialize() error {
if db != nil {
db.Close()
Expand Down Expand Up @@ -64,7 +66,7 @@ func CreateUserRegistration(id string, user_registration models.UserRegistration
if err != nil {
return err
}
return errors.New("Registration already exists.")
return errors.New(RegistrationAlreadyExists)
}

err = db.Insert("attendees", &user_registration)
Expand Down Expand Up @@ -189,7 +191,7 @@ func CreateMentorRegistration(id string, mentor_registration models.MentorRegist
if err != nil {
return err
}
return errors.New("Registration already exists")
return errors.New(RegistrationAlreadyExists)
}

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