Refactoring validation to belong to model
This commit is contained in:
42
internal/models/note_errors.go
Normal file
42
internal/models/note_errors.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ErrEmptyNoteContent struct{}
|
||||
type ErrEmptyNoteTitle struct{}
|
||||
type ErrNoteIncomplete struct {
|
||||
why []error
|
||||
}
|
||||
type ErrNoteTitleOverflow struct {
|
||||
length int
|
||||
}
|
||||
|
||||
func (e *ErrEmptyNoteContent) Error() string {
|
||||
return "content cannot be empty"
|
||||
}
|
||||
|
||||
func (e *ErrEmptyNoteTitle) Error() string {
|
||||
return "title cannot be empty"
|
||||
}
|
||||
|
||||
func (e *ErrNoteIncomplete) Error() string {
|
||||
if len(e.why) == 0 {
|
||||
panic("ok so if we use this we need to know why it is incomplete")
|
||||
}
|
||||
s := make([]string, len(e.why))
|
||||
for i, err := range e.why {
|
||||
s[i] = err.Error()
|
||||
}
|
||||
return "note incomplete: " + strings.Join(s, " & ")
|
||||
}
|
||||
|
||||
func (e *ErrNoteIncomplete) Unwrap() []error {
|
||||
return e.why
|
||||
}
|
||||
|
||||
func (e *ErrNoteTitleOverflow) Error() string {
|
||||
return fmt.Sprintf("title max length %d, %d provided", NoteTitleMaxLength, e.length)
|
||||
}
|
||||
Reference in New Issue
Block a user