Fix User UpdateError

This commit is contained in:
Adphi 2019-01-18 13:42:40 +01:00
parent 4925b52a0e
commit 3e33481fb4
4 changed files with 68 additions and 16 deletions

View File

@ -95,13 +95,13 @@ func (s *Shares) Delete(shareID int) error {
// Update update share details // Update update share details
// expireDate expireDate expects a well formatted date string, e.g. YYYY-MM-DD // expireDate expireDate expects a well formatted date string, e.g. YYYY-MM-DD
func (s *Shares) Update(shareUpdate types.ShareUpdate) error { func (s *Shares) Update(shareUpdate types.ShareUpdate) error {
errs := make(chan types.UpdateError) errs := make(chan *types.UpdateError)
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(4) wg.Add(4)
go func() { go func() {
defer wg.Done() defer wg.Done()
if err := s.UpdatePassword(shareUpdate.ShareID, shareUpdate.Password); err != nil { if err := s.UpdatePassword(shareUpdate.ShareID, shareUpdate.Password); err != nil {
errs <- types.UpdateError{ errs <- &types.UpdateError{
Field: "password", Field: "password",
Error: err, Error: err,
} }
@ -110,7 +110,7 @@ func (s *Shares) Update(shareUpdate types.ShareUpdate) error {
go func() { go func() {
defer wg.Done() defer wg.Done()
if err := s.UpdateExpireDate(shareUpdate.ShareID, shareUpdate.ExpireDate); err != nil { if err := s.UpdateExpireDate(shareUpdate.ShareID, shareUpdate.ExpireDate); err != nil {
errs <- types.UpdateError{ errs <- &types.UpdateError{
Field: "expireDate", Field: "expireDate",
Error: err, Error: err,
} }
@ -119,7 +119,7 @@ func (s *Shares) Update(shareUpdate types.ShareUpdate) error {
go func() { go func() {
defer wg.Done() defer wg.Done()
if err := s.UpdatePermissions(shareUpdate.ShareID, shareUpdate.Permissions); err != nil { if err := s.UpdatePermissions(shareUpdate.ShareID, shareUpdate.Permissions); err != nil {
errs <- types.UpdateError{ errs <- &types.UpdateError{
Field: "permissions", Field: "permissions",
Error: err, Error: err,
} }
@ -128,7 +128,7 @@ func (s *Shares) Update(shareUpdate types.ShareUpdate) error {
go func() { go func() {
defer wg.Done() defer wg.Done()
if err := s.UpdatePublicUpload(shareUpdate.ShareID, shareUpdate.PublicUpload); err != nil { if err := s.UpdatePublicUpload(shareUpdate.ShareID, shareUpdate.PublicUpload); err != nil {
errs <- types.UpdateError{ errs <- &types.UpdateError{
Field: "publicUpload", Field: "publicUpload",
Error: err, Error: err,
} }
@ -138,7 +138,10 @@ func (s *Shares) Update(shareUpdate types.ShareUpdate) error {
wg.Wait() wg.Wait()
close(errs) close(errs)
}() }()
return types.NewUpdateError(errs) if err := types.NewUpdateError(errs); err != nil {
return err
}
return nil
} }
//UpdateExpireDate updates the share's expire date //UpdateExpireDate updates the share's expire date

View File

@ -44,14 +44,13 @@ func (e *UserUpdateError) Error() string {
} }
//NewUpdateError returns an UpdateError based on an UpdateError channel //NewUpdateError returns an UpdateError based on an UpdateError channel
func NewUpdateError(errors chan UpdateError) *UserUpdateError { func NewUpdateError(errors chan *UpdateError) *UserUpdateError {
var ue UserUpdateError ue := UserUpdateError{map[string]error{}}
for e := range errors { for e := range errors {
if ue.Errors == nil { if e != nil {
ue.Errors = map[string]error{e.Field: e.Error}
}
ue.Errors[e.Field] = e.Error ue.Errors[e.Field] = e.Error
} }
}
if len(ue.Errors) > 0 { if len(ue.Errors) > 0 {
return &ue return &ue
} }

46
types/errors_test.go Normal file
View File

@ -0,0 +1,46 @@
package types
import (
"errors"
"github.com/stretchr/testify/assert"
"strconv"
"sync"
"testing"
)
func TestUserUpdateErrors(t *testing.T) {
exp := map[string]error{}
errs := make(chan *UpdateError)
go func() {
for i := 0; i < 10; i++ {
f := strconv.Itoa(i)
e := errors.New(f)
err := UpdateError{
Field: f,
Error: e,
}
exp[f] = e
errs <- &err
}
close(errs)
}()
uerrs := NewUpdateError(errs)
assert.Equal(t, exp, uerrs.Errors)
assert.NotEmpty(t, uerrs.Error())
}
func TestUserUpdateErrorsNil(t *testing.T) {
var wg sync.WaitGroup
errs := make(chan *UpdateError)
wg.Add(1)
go func() {
errs <- nil
wg.Done()
}()
go func() {
wg.Wait()
close(errs)
}()
uerrs := NewUpdateError(errs)
assert.Nil(t, uerrs)
}

View File

@ -191,7 +191,7 @@ func (u *Users) SendWelcomeEmail(name string) error {
//Update takes a *types.Users struct to update the user's information //Update takes a *types.Users struct to update the user's information
func (u *Users) Update(user *types.UserDetails) error { func (u *Users) Update(user *types.UserDetails) error {
m := structs.Map(user) m := structs.Map(user)
errs := make(chan types.UpdateError) errs := make(chan *types.UpdateError)
var wg sync.WaitGroup var wg sync.WaitGroup
for k := range m { for k := range m {
// Filter updatable fields // Filter updatable fields
@ -216,7 +216,7 @@ func (u *Users) Update(user *types.UserDetails) error {
go func(key string, value string) { go func(key string, value string) {
defer wg.Done() defer wg.Done()
if err := u.updateAttribute(user.ID, strings.ToLower(key), value); err != nil { if err := u.updateAttribute(user.ID, strings.ToLower(key), value); err != nil {
errs <- types.UpdateError{ errs <- &types.UpdateError{
Field: key, Field: key,
Error: err, Error: err,
} }
@ -228,7 +228,11 @@ func (u *Users) Update(user *types.UserDetails) error {
wg.Wait() wg.Wait()
close(errs) close(errs)
}() }()
return types.NewUpdateError(errs) // Warning : we actually need to check the *err
if err := types.NewUpdateError(errs); err != nil {
return err
}
return nil
} }
//UpdateEmail update the user's email //UpdateEmail update the user's email
@ -355,7 +359,7 @@ func (u *Users) baseRequest(method string, ro *req.RequestOptions, subRoutes ...
} }
func ignoredUserField(key string) bool { func ignoredUserField(key string) bool {
keys := []string{"Email", "DisplayName", "Phone", "Address", "Website", "Twitter", "Quota"} keys := []string{"Email", "Displayname", "Phone", "Address", "Website", "Twitter", "Quota"}
for _, k := range keys { for _, k := range keys {
if key == k { if key == k {
return false return false