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 accesslist
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 row by ID
@ -22,7 +23,7 @@ func GetByID(id int) (Model, error) {
// Create will create a row from this model
func Create(m *Model) (int, error) {
if m.ID != 0 {
return 0, goerrors.New("Cannot create access list when model already has an ID")
return 0, eris.New("Cannot create access list when model already has an ID")
}
m.Touch(true)
@ -60,7 +61,7 @@ func Create(m *Model) (int, error) {
// Update will Update a row from this model
func Update(m *Model) error {
if m.ID == 0 {
return goerrors.New("Cannot update access list when model doesn't have an ID")
return eris.New("Cannot update access list when model doesn't have an ID")
}
m.Touch(false)

View File

@ -7,6 +7,8 @@ import (
"npm/internal/database"
"npm/internal/entity/user"
"npm/internal/types"
"github.com/rotisserie/eris"
)
const (
@ -52,7 +54,7 @@ func (m *Model) Save() error {
var err error
if m.UserID == 0 {
return fmt.Errorf("User ID must be specified")
return eris.Errorf("User ID must be specified")
}
if m.ID == 0 {

View File

@ -1,10 +1,11 @@
package auth
import (
goerrors "errors"
"fmt"
"npm/internal/database"
"github.com/rotisserie/eris"
)
// GetByID finds a auth by ID
@ -24,7 +25,7 @@ func GetByUserIDType(userID int, authType string) (Model, error) {
// Create will create a Auth from this model
func Create(auth *Model) (int, error) {
if auth.ID != 0 {
return 0, goerrors.New("Cannot create auth when model already has an ID")
return 0, eris.New("Cannot create auth when model already has an ID")
}
auth.Touch(true)
@ -62,7 +63,7 @@ func Create(auth *Model) (int, error) {
// Update will Update a Auth from this model
func Update(auth *Model) error {
if auth.ID == 0 {
return goerrors.New("Cannot update auth when model doesn't have an ID")
return eris.New("Cannot update auth when model doesn't have an ID")
}
auth.Touch(false)

View File

@ -1,13 +1,13 @@
package auth
import (
goerrors "errors"
"fmt"
"time"
"npm/internal/database"
"npm/internal/types"
"github.com/rotisserie/eris"
"golang.org/x/crypto/bcrypt"
)
@ -86,12 +86,12 @@ func (m *Model) SetPassword(password string) error {
// ValidateSecret will check if a given secret matches the encrypted secret
func (m *Model) ValidateSecret(secret string) error {
if m.Type != TypePassword {
return goerrors.New("Could not validate Secret, auth type is not a Password")
return eris.New("Could not validate Secret, auth type is not a Password")
}
err := bcrypt.CompareHashAndPassword([]byte(m.Secret), []byte(secret))
if err != nil {
return goerrors.New("Invalid Password")
return eris.New("Invalid Password")
}
return nil

View File

@ -2,7 +2,6 @@ package certificate
import (
"database/sql"
goerrors "errors"
"fmt"
"npm/internal/database"
@ -11,6 +10,8 @@ import (
"npm/internal/jobqueue"
"npm/internal/logger"
"npm/internal/model"
"github.com/rotisserie/eris"
)
// GetByID finds a row by ID
@ -23,7 +24,7 @@ func GetByID(id int) (Model, error) {
// Create will create a row from this model
func Create(certificate *Model) (int, error) {
if certificate.ID != 0 {
return 0, goerrors.New("Cannot create certificate when model already has an ID")
return 0, eris.New("Cannot create certificate when model already has an ID")
}
certificate.Touch(true)
@ -77,7 +78,7 @@ func Create(certificate *Model) (int, error) {
// Update will Update a Auth from this model
func Update(certificate *Model) error {
if certificate.ID == 0 {
return goerrors.New("Cannot update certificate when model doesn't have an ID")
return eris.New("Cannot update certificate when model doesn't have an ID")
}
certificate.Touch(false)

View File

@ -1,7 +1,6 @@
package certificate
import (
"errors"
"fmt"
"os"
"regexp"
@ -17,6 +16,8 @@ import (
"npm/internal/logger"
"npm/internal/types"
"npm/internal/util"
"github.com/rotisserie/eris"
)
const (
@ -90,15 +91,15 @@ func (m *Model) Save() error {
var err error
if m.UserID == 0 {
return fmt.Errorf("User ID must be specified")
return eris.Errorf("User ID must be specified")
}
if !m.Validate() {
return fmt.Errorf("Certificate data is incorrect or incomplete for this type")
return eris.Errorf("Certificate data is incorrect or incomplete for this type")
}
if !m.ValidateWildcardSupport() {
return fmt.Errorf("Cannot use Wildcard domains with this CA")
return eris.Errorf("Cannot use Wildcard domains with this CA")
}
m.setDefaultStatus()
@ -215,7 +216,7 @@ func (m *Model) Expand(items []string) error {
// Returns: (key, fullchain, certFolder)
func (m *Model) GetCertificateLocations() (string, string, string) {
if m.ID == 0 {
logger.Error("GetCertificateLocationsError", errors.New("GetCertificateLocations called before certificate was saved"))
logger.Error("GetCertificateLocationsError", eris.New("GetCertificateLocations called before certificate was saved"))
return "", "", ""
}

View File

@ -2,7 +2,6 @@ package certificateauthority
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 row by ID
@ -22,7 +23,7 @@ func GetByID(id int) (Model, error) {
// Create will create a row from this model
func Create(ca *Model) (int, error) {
if ca.ID != 0 {
return 0, goerrors.New("Cannot create certificate authority when model already has an ID")
return 0, eris.New("Cannot create certificate authority when model already has an ID")
}
ca.Touch(true)
@ -64,7 +65,7 @@ func Create(ca *Model) (int, error) {
// Update will Update a row from this model
func Update(ca *Model) error {
if ca.ID == 0 {
return goerrors.New("Cannot update certificate authority when model doesn't have an ID")
return eris.New("Cannot update certificate authority when model doesn't have an ID")
}
ca.Touch(false)

View File

@ -1,7 +1,6 @@
package certificateauthority
import (
goerrors "errors"
"fmt"
"os"
"path/filepath"
@ -10,6 +9,8 @@ import (
"npm/internal/database"
"npm/internal/errors"
"npm/internal/types"
"github.com/rotisserie/eris"
)
const (
@ -79,7 +80,7 @@ func (m *Model) Check() error {
var err error
if m.CABundle != "" {
if _, fileerr := os.Stat(filepath.Clean(m.CABundle)); goerrors.Is(fileerr, os.ErrNotExist) {
if _, fileerr := os.Stat(filepath.Clean(m.CABundle)); eris.Is(fileerr, os.ErrNotExist) {
err = errors.ErrCABundleDoesNotExist
}
}

View File

@ -2,7 +2,6 @@ package dnsprovider
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 row by ID
@ -22,7 +23,7 @@ func GetByID(id int) (Model, error) {
// Create will create a row from this model
func Create(provider *Model) (int, error) {
if provider.ID != 0 {
return 0, goerrors.New("Cannot create dns provider when model already has an ID")
return 0, eris.New("Cannot create dns provider when model already has an ID")
}
provider.Touch(true)
@ -64,7 +65,7 @@ func Create(provider *Model) (int, error) {
// Update will Update a row from this model
func Update(provider *Model) error {
if provider.ID == 0 {
return goerrors.New("Cannot update dns provider when model doesn't have an ID")
return eris.New("Cannot update dns provider when model doesn't have an ID")
}
provider.Touch(false)

View File

@ -8,6 +8,8 @@ import (
"npm/internal/dnsproviders"
"npm/internal/logger"
"npm/internal/types"
"github.com/rotisserie/eris"
)
const (
@ -54,7 +56,7 @@ func (m *Model) Save() error {
var err error
if m.UserID == 0 {
return fmt.Errorf("User ID must be specified")
return eris.Errorf("User ID must be specified")
}
if m.ID == 0 {

View File

@ -2,7 +2,6 @@ package host
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 Host by ID
@ -22,7 +23,7 @@ func GetByID(id int) (Model, error) {
// create will create a Host from this model
func create(host *Model) (int, error) {
if host.ID != 0 {
return 0, goerrors.New("Cannot create host when model already has an ID")
return 0, eris.New("Cannot create host when model already has an ID")
}
host.Touch(true)
@ -102,7 +103,7 @@ func create(host *Model) (int, error) {
// update will Update a Host from this model
func update(host *Model) error {
if host.ID == 0 {
return goerrors.New("Cannot update host when model doesn't have an ID")
return eris.New("Cannot update host when model doesn't have an ID")
}
host.Touch(false)

View File

@ -12,6 +12,8 @@ import (
"npm/internal/status"
"npm/internal/types"
"npm/internal/util"
"github.com/rotisserie/eris"
)
const (
@ -87,7 +89,7 @@ func (m *Model) Save(skipConfiguration bool) error {
var err error
if m.UserID == 0 {
return fmt.Errorf("User ID must be specified")
return eris.Errorf("User ID must be specified")
}
if !skipConfiguration {

View File

@ -2,7 +2,6 @@ package nginxtemplate
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 Host by ID
@ -22,7 +23,7 @@ func GetByID(id int) (Model, error) {
// Create will create a Host from this model
func Create(host *Model) (int, error) {
if host.ID != 0 {
return 0, goerrors.New("Cannot create host template when model already has an ID")
return 0, eris.New("Cannot create host template when model already has an ID")
}
host.Touch(true)
@ -62,7 +63,7 @@ func Create(host *Model) (int, error) {
// Update will Update a Host from this model
func Update(host *Model) error {
if host.ID == 0 {
return goerrors.New("Cannot update host template when model doesn't have an ID")
return eris.New("Cannot update host template when model doesn't have an ID")
}
host.Touch(false)

View File

@ -6,6 +6,8 @@ import (
"npm/internal/database"
"npm/internal/types"
"github.com/rotisserie/eris"
)
const (
@ -50,7 +52,7 @@ func (m *Model) Save() error {
var err error
if m.UserID == 0 {
return fmt.Errorf("User ID must be specified")
return eris.Errorf("User ID must be specified")
}
if m.ID == 0 {

View File

@ -2,7 +2,6 @@ package setting
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 setting by ID
@ -29,7 +30,7 @@ func GetByName(name string) (Model, error) {
// Create will Create a Setting from this model
func Create(setting *Model) (int, error) {
if setting.ID != 0 {
return 0, goerrors.New("Cannot create setting when model already has an ID")
return 0, eris.New("Cannot create setting when model already has an ID")
}
setting.Touch(true)
@ -63,7 +64,7 @@ func Create(setting *Model) (int, error) {
// Update will Update a Setting from this model
func Update(setting *Model) error {
if setting.ID == 0 {
return goerrors.New("Cannot update setting when model doesn't have an ID")
return eris.New("Cannot update setting when model doesn't have an ID")
}
setting.Touch(false)

View File

@ -2,7 +2,6 @@ package stream
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 auth by ID
@ -22,7 +23,7 @@ func GetByID(id int) (Model, error) {
// Create will create a Auth from this model
func Create(host *Model) (int, error) {
if host.ID != 0 {
return 0, goerrors.New("Cannot create stream when model already has an ID")
return 0, eris.New("Cannot create stream when model already has an ID")
}
host.Touch(true)
@ -66,7 +67,7 @@ func Create(host *Model) (int, error) {
// Update will Update a Host from this model
func Update(host *Model) error {
if host.ID == 0 {
return goerrors.New("Cannot update stream when model doesn't have an ID")
return eris.New("Cannot update stream when model doesn't have an ID")
}
host.Touch(false)

View File

@ -6,6 +6,8 @@ import (
"npm/internal/database"
"npm/internal/types"
"github.com/rotisserie/eris"
)
const (
@ -52,7 +54,7 @@ func (m *Model) Save() error {
var err error
if m.UserID == 0 {
return fmt.Errorf("User ID must be specified")
return eris.Errorf("User ID must be specified")
}
if m.ID == 0 {

View File

@ -2,7 +2,6 @@ package upstream
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 Upstream by ID
@ -22,7 +23,7 @@ func GetByID(id int) (Model, error) {
// create will create a Upstream from this model
func create(u *Model) (int, error) {
if u.ID != 0 {
return 0, goerrors.New("Cannot create upstream when model already has an ID")
return 0, eris.New("Cannot create upstream when model already has an ID")
}
u.Touch(true)
@ -80,7 +81,7 @@ func create(u *Model) (int, error) {
// update will Update a Upstream from this model
func update(u *Model) error {
if u.ID == 0 {
return goerrors.New("Cannot update upstream when model doesn't have an ID")
return eris.New("Cannot update upstream when model doesn't have an ID")
}
u.Touch(false)

View File

@ -12,6 +12,8 @@ import (
"npm/internal/status"
"npm/internal/types"
"npm/internal/util"
"github.com/rotisserie/eris"
)
const (
@ -73,7 +75,7 @@ func (m *Model) Save(skipConfiguration bool) error {
var err error
if m.UserID == 0 {
return fmt.Errorf("User ID must be specified")
return eris.Errorf("User ID must be specified")
}
// ensure name is trimmed of whitespace

View File

@ -2,7 +2,6 @@ package upstreamserver
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 Upstream Server by ID
@ -34,7 +35,7 @@ func GetByUpstreamID(upstreamID int) ([]Model, error) {
// create will create a Upstream Server from this model
func create(u *Model) (int, error) {
if u.ID != 0 {
return 0, goerrors.New("Cannot create upstream server when model already has an ID")
return 0, eris.New("Cannot create upstream server when model already has an ID")
}
u.Touch(true)
@ -82,7 +83,7 @@ func create(u *Model) (int, error) {
// update will Update a Upstream from this model
func update(u *Model) error {
if u.ID == 0 {
return goerrors.New("Cannot update upstream server when model doesn't have an ID")
return eris.New("Cannot update upstream server when model doesn't have an ID")
}
u.Touch(false)

View File

@ -6,6 +6,8 @@ import (
"npm/internal/database"
"npm/internal/types"
"github.com/rotisserie/eris"
)
const (
@ -53,7 +55,7 @@ func (m *Model) Save() error {
var err error
if m.UpstreamID == 0 {
return fmt.Errorf("Upstream ID must be specified")
return eris.Errorf("Upstream ID must be specified")
}
if m.ID == 0 {

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)
}
}