From 7d7766e4cbc1b6c76c26df9f9fb93821dc235a83 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 11 Apr 2020 17:55:00 -0700 Subject: [PATCH] Correctly handle validation errors vs. errors from the panel during installation --- installer/errors.go | 19 +++++++++++++++++++ installer/installer.go | 9 ++------- router/router_system.go | 11 ++++++++--- 3 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 installer/errors.go diff --git a/installer/errors.go b/installer/errors.go new file mode 100644 index 0000000..19451a9 --- /dev/null +++ b/installer/errors.go @@ -0,0 +1,19 @@ +package installer + +type validationError struct { + msg string +} + +func (e *validationError) Error() string { + return e.msg +} + +func IsValidationError(err error) bool { + _, ok := err.(*validationError) + + return ok +} + +func NewValidationError(msg string) error { + return &validationError{msg: msg} +} diff --git a/installer/installer.go b/installer/installer.go index f29c7aa..9f7f868 100644 --- a/installer/installer.go +++ b/installer/installer.go @@ -22,11 +22,11 @@ type Installer struct { // calling Execute(). func New(data []byte) (*Installer, error) { if !govalidator.IsUUIDv4(getString(data, "uuid")) { - return nil, errors.New("uuid provided was not in a valid format") + return nil, NewValidationError("uuid provided was not in a valid format") } if !govalidator.IsUUIDv4(getString(data, "service", "egg")) { - return nil, errors.New("service egg provided was not in a valid format") + return nil, NewValidationError("service egg provided was not in a valid format") } s := &server.Server{ @@ -82,11 +82,6 @@ func New(data []byte) (*Installer, error) { return nil, errors.New(rerr.String()) } - /*b, err := s.WriteConfigurationToDisk() - if err != nil { - return nil, err - }*/ - // Destroy the temporary server instance. s = nil diff --git a/router/router_system.go b/router/router_system.go index 3b3ddcd..085f383 100644 --- a/router/router_system.go +++ b/router/router_system.go @@ -37,9 +37,14 @@ func postCreateServer(c *gin.Context) { install, err := installer.New(buf.Bytes()) if err != nil { - TrackedError(err). - SetMessage("Failed to validate the data provided in the request."). - AbortWithStatus(http.StatusUnprocessableEntity, c) + if installer.IsValidationError(err) { + c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{ + "error": "The data provided in the request could not be validated.", + }) + return + } + + TrackedError(err).AbortWithServerError(c) return }