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

Commit

Permalink
Add conflict errors for duplicate registrations
Browse files Browse the repository at this point in the history
  • Loading branch information
TakshPSingh committed Nov 10, 2021
1 parent b16a906 commit 27cd8d5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
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

0 comments on commit 27cd8d5

Please sign in to comment.