added error checking for name collision on frontend and send corresponding error

This commit is contained in:
Cam Otts
2023-02-01 13:13:15 -06:00
parent cd28970f13
commit 9aba5f3524
7 changed files with 121 additions and 1 deletions

View File

@ -1,7 +1,11 @@
package controller
import (
"errors"
"github.com/go-openapi/runtime/middleware"
"github.com/lib/pq"
"github.com/mattn/go-sqlite3"
"github.com/openziti/zrok/controller/store"
"github.com/openziti/zrok/controller/zrokEdgeSdk"
"github.com/openziti/zrok/rest_model_zrok"
@ -60,6 +64,21 @@ func (h *createFrontendHandler) Handle(params admin.CreateFrontendParams, princi
Reserved: true,
}
if _, err := str.CreateGlobalFrontend(fe, tx); err != nil {
perr := &pq.Error{}
sqliteErr := &sqlite3.Error{}
switch {
case errors.As(err, &perr):
if perr.Code == pq.ErrorCode("23505") {
return admin.NewCreateFrontendBadRequest()
}
case errors.As(err, sqliteErr):
if errors.Is(sqliteErr.Code, sqlite3.ErrConstraint) {
return admin.NewCreateFrontendBadRequest()
}
default:
logrus.Error("None?", err)
}
logrus.Errorf("error creating frontend record: %v", err)
return admin.NewCreateFrontendInternalServerError()
}