diff --git a/common/errors/ConflictError.go b/common/errors/ConflictError.go new file mode 100644 index 00000000..9cae7c13 --- /dev/null +++ b/common/errors/ConflictError.go @@ -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} +} diff --git a/services/registration/controller/controller.go b/services/registration/controller/controller.go index 74d2b0e9..7d6fa64d 100644 --- a/services/registration/controller/controller.go +++ b/services/registration/controller/controller.go @@ -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 } @@ -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 } diff --git a/services/registration/service/registration_service.go b/services/registration/service/registration_service.go index 0509f13b..3bf8c705 100644 --- a/services/registration/service/registration_service.go +++ b/services/registration/service/registration_service.go @@ -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() @@ -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) @@ -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)