gonextcloud/types/errors.go

59 lines
1.2 KiB
Go
Raw Permalink Normal View History

package types
import (
"fmt"
"strings"
)
2018-07-30 08:18:54 +00:00
//APIError contains the returned error code and message from the Nextcloud's API
type APIError struct {
Code int
Message string
}
2018-07-30 08:18:54 +00:00
//ErrorFromMeta return a types.APIError from the Response's types.Meta
func ErrorFromMeta(meta Meta) *APIError {
return &APIError{
meta.Statuscode,
meta.Message,
}
}
2018-07-30 08:18:54 +00:00
//Error return the types.APIError string
func (e *APIError) Error() string {
return fmt.Sprintf("%d : %s", e.Code, e.Message)
}
2018-07-30 08:18:54 +00:00
//UpdateError contains the user's field and corresponding error
type UpdateError struct {
Field string
Error error
}
2018-07-30 08:18:54 +00:00
//UpdateError contains the errors resulting from a UserUpdate or a UserCreateFull call
type UserUpdateError struct {
Errors map[string]error
}
func (e *UserUpdateError) Error() string {
var errors []string
for k, e := range e.Errors {
2018-08-08 14:04:06 +00:00
errors = append(errors, fmt.Sprintf("%s: %v", k, e))
}
2019-01-18 12:42:40 +00:00
return strings.Join(errors, ", ")
}
2018-07-30 08:18:54 +00:00
//NewUpdateError returns an UpdateError based on an UpdateError channel
2019-01-18 12:42:40 +00:00
func NewUpdateError(errors chan *UpdateError) *UserUpdateError {
ue := UserUpdateError{map[string]error{}}
for e := range errors {
2019-01-18 12:42:40 +00:00
if e != nil {
ue.Errors[e.Field] = e.Error
}
}
if len(ue.Errors) > 0 {
return &ue
}
return nil
}