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
No known key found for this signature in database
GPG Key ID: 367B7C7EBD84A8BD
7 changed files with 121 additions and 1 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
.$*
.idea
.vscode
*.db
automated-release-build
etc/dev.yml

View File

@ -1,8 +1,11 @@
package main
import (
"os"
"github.com/openziti/zrok/rest_client_zrok/admin"
"github.com/openziti/zrok/rest_model_zrok"
"github.com/openziti/zrok/tui"
"github.com/openziti/zrok/zrokdir"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@ -52,7 +55,14 @@ func (cmd *adminCreateFrontendCommand) run(_ *cobra.Command, args []string) {
resp, err := zrok.Admin.CreateFrontend(req, mustGetAdminAuth())
if err != nil {
panic(err)
switch err.(type) {
case *admin.CreateFrontendBadRequest:
tui.Error("create frontend request failed: name already exists", err)
os.Exit(1)
default:
tui.Error("create frontend request failed", err)
os.Exit(1)
}
}
logrus.Infof("created global public frontend '%v'", resp.Payload.Token)

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()
}

View File

@ -29,6 +29,12 @@ func (o *CreateFrontendReader) ReadResponse(response runtime.ClientResponse, con
return nil, err
}
return result, nil
case 400:
result := NewCreateFrontendBadRequest()
if err := result.readResponse(response, consumer, o.formats); err != nil {
return nil, err
}
return nil, result
case 401:
result := NewCreateFrontendUnauthorized()
if err := result.readResponse(response, consumer, o.formats); err != nil {
@ -115,6 +121,57 @@ func (o *CreateFrontendCreated) readResponse(response runtime.ClientResponse, co
return nil
}
// NewCreateFrontendBadRequest creates a CreateFrontendBadRequest with default headers values
func NewCreateFrontendBadRequest() *CreateFrontendBadRequest {
return &CreateFrontendBadRequest{}
}
/*
CreateFrontendBadRequest describes a response with status code 400, with default header values.
bad request
*/
type CreateFrontendBadRequest struct {
}
// IsSuccess returns true when this create frontend bad request response has a 2xx status code
func (o *CreateFrontendBadRequest) IsSuccess() bool {
return false
}
// IsRedirect returns true when this create frontend bad request response has a 3xx status code
func (o *CreateFrontendBadRequest) IsRedirect() bool {
return false
}
// IsClientError returns true when this create frontend bad request response has a 4xx status code
func (o *CreateFrontendBadRequest) IsClientError() bool {
return true
}
// IsServerError returns true when this create frontend bad request response has a 5xx status code
func (o *CreateFrontendBadRequest) IsServerError() bool {
return false
}
// IsCode returns true when this create frontend bad request response a status code equal to that given
func (o *CreateFrontendBadRequest) IsCode(code int) bool {
return code == 400
}
func (o *CreateFrontendBadRequest) Error() string {
return fmt.Sprintf("[POST /frontend][%d] createFrontendBadRequest ", 400)
}
func (o *CreateFrontendBadRequest) String() string {
return fmt.Sprintf("[POST /frontend][%d] createFrontendBadRequest ", 400)
}
func (o *CreateFrontendBadRequest) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {
return nil
}
// NewCreateFrontendUnauthorized creates a CreateFrontendUnauthorized with default headers values
func NewCreateFrontendUnauthorized() *CreateFrontendUnauthorized {
return &CreateFrontendUnauthorized{}

View File

@ -265,6 +265,9 @@ func init() {
"$ref": "#/definitions/createFrontendResponse"
}
},
"400": {
"description": "bad request"
},
"401": {
"description": "unauthorized"
},
@ -1537,6 +1540,9 @@ func init() {
"$ref": "#/definitions/createFrontendResponse"
}
},
"400": {
"description": "bad request"
},
"401": {
"description": "unauthorized"
},

View File

@ -58,6 +58,31 @@ func (o *CreateFrontendCreated) WriteResponse(rw http.ResponseWriter, producer r
}
}
// CreateFrontendBadRequestCode is the HTTP code returned for type CreateFrontendBadRequest
const CreateFrontendBadRequestCode int = 400
/*
CreateFrontendBadRequest bad request
swagger:response createFrontendBadRequest
*/
type CreateFrontendBadRequest struct {
}
// NewCreateFrontendBadRequest creates CreateFrontendBadRequest with default headers values
func NewCreateFrontendBadRequest() *CreateFrontendBadRequest {
return &CreateFrontendBadRequest{}
}
// WriteResponse to the client
func (o *CreateFrontendBadRequest) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) {
rw.Header().Del(runtime.HeaderContentType) //Remove Content-Type on empty responses
rw.WriteHeader(400)
}
// CreateFrontendUnauthorizedCode is the HTTP code returned for type CreateFrontendUnauthorized
const CreateFrontendUnauthorizedCode int = 401

View File

@ -151,6 +151,8 @@ paths:
description: frontend created
schema:
$ref: "#/definitions/createFrontendResponse"
400:
description: bad request
401:
description: unauthorized
404: