Commit 0b5acdb9 authored by ale's avatar ale

Move AddEmailAlias/DeleteEmailAlias validation code to the right place

Previously we were returning error 500 when we should have returned a 400.
parent b3e84191
Pipeline #1538 passed with stages
in 1 minute and 37 seconds
......@@ -134,6 +134,15 @@ type AddEmailAliasRequest struct {
// Validate the request.
func (r *AddEmailAliasRequest) Validate(rctx *RequestContext) error {
if rctx.Resource.ID.Type() != ResourceTypeEmail {
return errors.New("this operation only works on email resources")
}
// Allow at most 5 aliases.
if len(rctx.Resource.Email.Aliases) >= maxEmailAliases {
return errors.New("too many aliases")
}
if err := rctx.fieldValidators.email(rctx.Context, r.Addr); err != nil {
return newValidationError(nil, "addr", err.Error())
}
......@@ -144,15 +153,6 @@ const maxEmailAliases = 5
// Serve the request.
func (r *AddEmailAliasRequest) Serve(rctx *RequestContext) (interface{}, error) {
if rctx.Resource.ID.Type() != ResourceTypeEmail {
return nil, errors.New("this operation only works on email resources")
}
// Allow at most 5 aliases.
if len(rctx.Resource.Email.Aliases) >= maxEmailAliases {
return nil, errors.New("too many aliases")
}
rctx.Resource.Email.Aliases = append(rctx.Resource.Email.Aliases, r.Addr)
if err := rctx.TX.UpdateResource(rctx.Context, rctx.Resource); err != nil {
return nil, err
......@@ -168,12 +168,16 @@ type DeleteEmailAliasRequest struct {
Addr string `json:"addr"`
}
// Serve the request.
func (r *DeleteEmailAliasRequest) Serve(rctx *RequestContext) (interface{}, error) {
// Validate the request.
func (r *DeleteEmailAliasRequest) Validate(rctx *RequestContext) error {
if rctx.Resource.ID.Type() != ResourceTypeEmail {
return nil, errors.New("this operation only works on email resources")
return errors.New("this operation only works on email resources")
}
return nil
}
// Serve the request.
func (r *DeleteEmailAliasRequest) Serve(rctx *RequestContext) (interface{}, error) {
var aliases []string
for _, a := range rctx.Resource.Email.Aliases {
if a != r.Addr {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment