Use eris for error management

This commit is contained in:
Jamie Curnow
2023-02-24 17:19:07 +10:00
parent 80315bd50e
commit c288886fd4
44 changed files with 173 additions and 128 deletions

View File

@ -2,7 +2,6 @@ package user
import (
"database/sql"
goerrors "errors"
"fmt"
"npm/internal/database"
@ -10,6 +9,8 @@ import (
"npm/internal/errors"
"npm/internal/logger"
"npm/internal/model"
"github.com/rotisserie/eris"
)
// GetByID finds a user by ID
@ -33,7 +34,7 @@ func Create(user *Model) (int, error) {
// database schema, but it's a bit more complex because of the is_deleted field.
if user.ID != 0 {
return 0, goerrors.New("Cannot create user when model already has an ID")
return 0, eris.New("Cannot create user when model already has an ID")
}
// Check if an existing user with this email exists
@ -77,7 +78,7 @@ func Create(user *Model) (int, error) {
// Update will Update a User from this model
func Update(user *Model) error {
if user.ID == 0 {
return goerrors.New("Cannot update user when model doesn't have an ID")
return eris.New("Cannot update user when model doesn't have an ID")
}
// Check that the email address isn't associated with another user

View File

@ -1,7 +1,6 @@
package user
import (
goerrors "errors"
"fmt"
"strings"
"time"
@ -14,6 +13,7 @@ import (
"npm/internal/util"
"github.com/drexedam/gravatar"
"github.com/rotisserie/eris"
)
const (
@ -100,7 +100,7 @@ func (m *Model) Delete() bool {
// SetPermissions will wipe out any existing permissions and add new ones for this user
func (m *Model) SetPermissions(permissions []string) error {
if m.ID == 0 {
return fmt.Errorf("Cannot set permissions without first saving the User")
return eris.Errorf("Cannot set permissions without first saving the User")
}
db := database.GetInstance()
@ -156,12 +156,12 @@ func (m *Model) generateGravatar() {
func (m *Model) SaveCapabilities() error {
// m.Capabilities
if m.ID == 0 {
return fmt.Errorf("Cannot save capabilities on unsaved user")
return eris.Errorf("Cannot save capabilities on unsaved user")
}
// there must be at least 1 capability
if len(m.Capabilities) == 0 {
return goerrors.New("At least 1 capability required for a user")
return eris.New("At least 1 capability required for a user")
}
db := database.GetInstance()
@ -183,7 +183,7 @@ func (m *Model) SaveCapabilities() error {
}
}
if !found {
return fmt.Errorf("Capability `%s` is not valid", cap)
return eris.Errorf("Capability `%s` is not valid", cap)
}
}