Use eris for error management

This commit is contained in:
Jamie Curnow 2023-02-24 17:19:07 +10:00
parent 80315bd50e
commit c288886fd4
No known key found for this signature in database
GPG Key ID: FFBB624C43388E9E
44 changed files with 173 additions and 128 deletions

View File

@ -17,6 +17,7 @@ require (
github.com/mattn/go-sqlite3 v1.14.16 github.com/mattn/go-sqlite3 v1.14.16
github.com/patrickmn/go-cache v2.1.0+incompatible github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/qri-io/jsonschema v0.2.1 github.com/qri-io/jsonschema v0.2.1
github.com/rotisserie/eris v0.5.4
github.com/stretchr/testify v1.7.0 github.com/stretchr/testify v1.7.0
github.com/vrischmann/envconfig v1.3.0 github.com/vrischmann/envconfig v1.3.0
golang.org/x/crypto v0.5.0 golang.org/x/crypto v0.5.0

View File

@ -76,6 +76,8 @@ github.com/qri-io/jsonpointer v0.1.1/go.mod h1:DnJPaYgiKu56EuDp8TU5wFLdZIcAnb/uH
github.com/qri-io/jsonschema v0.2.1 h1:NNFoKms+kut6ABPf6xiKNM5214jzxAhDBrPHCJ97Wg0= github.com/qri-io/jsonschema v0.2.1 h1:NNFoKms+kut6ABPf6xiKNM5214jzxAhDBrPHCJ97Wg0=
github.com/qri-io/jsonschema v0.2.1/go.mod h1:g7DPkiOsK1xv6T/Ao5scXRkd+yTFygcANPBaaqW+VrI= github.com/qri-io/jsonschema v0.2.1/go.mod h1:g7DPkiOsK1xv6T/Ao5scXRkd+yTFygcANPBaaqW+VrI=
github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc= github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc=
github.com/rotisserie/eris v0.5.4 h1:Il6IvLdAapsMhvuOahHWiBnl1G++Q0/L5UIkI5mARSk=
github.com/rotisserie/eris v0.5.4/go.mod h1:Z/kgYTJiJtocxCbFfvRmO+QejApzG6zpyky9G1A4g9s=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ= github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

View File

@ -13,12 +13,14 @@ import (
"npm/internal/entity/certificateauthority" "npm/internal/entity/certificateauthority"
"npm/internal/entity/dnsprovider" "npm/internal/entity/dnsprovider"
"npm/internal/logger" "npm/internal/logger"
"github.com/rotisserie/eris"
) )
func getAcmeShFilePath() (string, error) { func getAcmeShFilePath() (string, error) {
path, err := exec.LookPath("acme.sh") path, err := exec.LookPath("acme.sh")
if err != nil { if err != nil {
return path, fmt.Errorf("Cannot find acme.sh execuatable script in PATH") return path, eris.Wrapf(err, "Cannot find acme.sh execuatable script in PATH")
} }
return path, nil return path, nil
} }
@ -107,7 +109,7 @@ func shExec(args []string, envs []string) (string, error) {
b, e := c.CombinedOutput() b, e := c.CombinedOutput()
if e != nil { if e != nil {
// logger.Error("AcmeShError", fmt.Errorf("Command error: %s -- %v\n%+v", acmeSh, args, e)) // logger.Error("AcmeShError", eris.Wrapf(e, "Command error: %s -- %v\n%+v", acmeSh, args, e))
logger.Warn(string(b)) logger.Warn(string(b))
} }

View File

@ -1,10 +1,12 @@
package acme package acme
import "errors" import (
"github.com/rotisserie/eris"
)
// All errors relating to Acme.sh use // All errors relating to Acme.sh use
var ( var (
ErrDNSNeedsDNSProvider = errors.New("RequestCert dns method requires a dns provider") ErrDNSNeedsDNSProvider = eris.New("RequestCert dns method requires a dns provider")
ErrHTTPHasDNSProvider = errors.New("RequestCert http method does not need a dns provider") ErrHTTPHasDNSProvider = eris.New("RequestCert http method does not need a dns provider")
ErrMethodNotSupported = errors.New("RequestCert method not supported") ErrMethodNotSupported = eris.New("RequestCert method not supported")
) )

View File

@ -1,7 +1,6 @@
package handler package handler
import ( import (
"fmt"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
@ -11,6 +10,7 @@ import (
"npm/internal/model" "npm/internal/model"
"github.com/go-chi/chi" "github.com/go-chi/chi"
"github.com/rotisserie/eris"
) )
const defaultLimit = 10 const defaultLimit = 10
@ -45,7 +45,7 @@ func getDateRanges(r *http.Request) (time.Time, time.Time, error) {
var fromErr error var fromErr error
fromDate, fromErr = time.Parse(time.RFC3339, from) fromDate, fromErr = time.Parse(time.RFC3339, from)
if fromErr != nil { if fromErr != nil {
return fromDate, toDate, fmt.Errorf("From date is not in correct format: %v", strings.ReplaceAll(time.RFC3339, "Z", "+")) return fromDate, toDate, eris.Errorf("From date is not in correct format: %v", strings.ReplaceAll(time.RFC3339, "Z", "+"))
} }
} }
@ -53,7 +53,7 @@ func getDateRanges(r *http.Request) (time.Time, time.Time, error) {
var toErr error var toErr error
toDate, toErr = time.Parse(time.RFC3339, to) toDate, toErr = time.Parse(time.RFC3339, to)
if toErr != nil { if toErr != nil {
return fromDate, toDate, fmt.Errorf("To date is not in correct format: %v", strings.ReplaceAll(time.RFC3339, "Z", "+")) return fromDate, toDate, eris.Errorf("To date is not in correct format: %v", strings.ReplaceAll(time.RFC3339, "Z", "+"))
} }
} }
@ -104,14 +104,14 @@ func getQueryVarInt(r *http.Request, varName string, required bool, defaultValue
varValue := queryValues.Get(varName) varValue := queryValues.Get(varName)
if varValue == "" && required { if varValue == "" && required {
return 0, fmt.Errorf("%v was not supplied in the request", varName) return 0, eris.Errorf("%v was not supplied in the request", varName)
} else if varValue == "" { } else if varValue == "" {
return defaultValue, nil return defaultValue, nil
} }
varInt, intErr := strconv.Atoi(varValue) varInt, intErr := strconv.Atoi(varValue)
if intErr != nil { if intErr != nil {
return 0, fmt.Errorf("%v is not a valid number", varName) return 0, eris.Wrapf(intErr, "%v is not a valid number", varName)
} }
return varInt, nil return varInt, nil
@ -123,7 +123,7 @@ func getQueryVarBool(r *http.Request, varName string, required bool, defaultValu
varValue := queryValues.Get(varName) varValue := queryValues.Get(varName)
if varValue == "" && required { if varValue == "" && required {
return false, fmt.Errorf("%v was not supplied in the request", varName) return false, eris.Errorf("%v was not supplied in the request", varName)
} else if varValue == "" { } else if varValue == "" {
return defaultValue, nil return defaultValue, nil
} }
@ -140,13 +140,13 @@ func getURLParamInt(r *http.Request, varName string) (int, error) {
var paramInt int var paramInt int
if paramStr == "" && required { if paramStr == "" && required {
return 0, fmt.Errorf("%v was not supplied in the request", varName) return 0, eris.Errorf("%v was not supplied in the request", varName)
} else if paramStr == "" { } else if paramStr == "" {
return defaultValue, nil return defaultValue, nil
} }
if paramInt, err = strconv.Atoi(paramStr); err != nil { if paramInt, err = strconv.Atoi(paramStr); err != nil {
return 0, fmt.Errorf("%v is not a valid number", varName) return 0, eris.Wrapf(err, "%v is not a valid number", varName)
} }
return paramInt, nil return paramInt, nil
@ -158,7 +158,7 @@ func getURLParamString(r *http.Request, varName string) (string, error) {
paramStr := chi.URLParam(r, varName) paramStr := chi.URLParam(r, varName)
if paramStr == "" && required { if paramStr == "" && required {
return "", fmt.Errorf("%v was not supplied in the request", varName) return "", eris.Errorf("%v was not supplied in the request", varName)
} else if paramStr == "" { } else if paramStr == "" {
return defaultValue, nil return defaultValue, nil
} }

View File

@ -1,7 +1,6 @@
package handler package handler
import ( import (
"errors"
"io" "io"
"io/fs" "io/fs"
"mime" "mime"
@ -11,11 +10,13 @@ import (
"npm/embed" "npm/embed"
h "npm/internal/api/http" h "npm/internal/api/http"
"github.com/rotisserie/eris"
) )
var ( var (
assetsSub fs.FS assetsSub fs.FS
errIsDir = errors.New("path is dir") errIsDir = eris.New("path is dir")
) )
// NotFound is a json error handler for 404's and method not allowed. // NotFound is a json error handler for 404's and method not allowed.

View File

@ -3,16 +3,16 @@ package http
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"github.com/qri-io/jsonschema" "github.com/qri-io/jsonschema"
"github.com/rotisserie/eris"
) )
var ( var (
// ErrInvalidJSON is an error for invalid json // ErrInvalidJSON is an error for invalid json
ErrInvalidJSON = errors.New("JSON is invalid") ErrInvalidJSON = eris.New("JSON is invalid")
// ErrInvalidPayload is an error for invalid incoming data // ErrInvalidPayload is an error for invalid incoming data
ErrInvalidPayload = errors.New("Payload is invalid") ErrInvalidPayload = eris.New("Payload is invalid")
) )
// ValidateRequestSchema takes a Schema and the Content to validate against it // ValidateRequestSchema takes a Schema and the Content to validate against it

View File

@ -3,13 +3,13 @@ package middleware
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
c "npm/internal/api/context" c "npm/internal/api/context"
h "npm/internal/api/http" h "npm/internal/api/http"
"github.com/qri-io/jsonschema" "github.com/qri-io/jsonschema"
"github.com/rotisserie/eris"
) )
// CheckRequestSchema checks the payload against schema // CheckRequestSchema checks the payload against schema
@ -17,7 +17,7 @@ func CheckRequestSchema(ctx context.Context, schemaData string, payload []byte)
// Create root schema // Create root schema
rs := &jsonschema.Schema{} rs := &jsonschema.Schema{}
if err := json.Unmarshal([]byte(schemaData), rs); err != nil { if err := json.Unmarshal([]byte(schemaData), rs); err != nil {
return nil, fmt.Errorf("Schema Fatal: %v", err) return nil, eris.Wrapf(err, "Schema Fatal: %v", err)
} }
// Validate it // Validate it

View File

@ -7,6 +7,8 @@ import (
"npm/internal/dnsproviders" "npm/internal/dnsproviders"
"npm/internal/logger" "npm/internal/logger"
"npm/internal/util" "npm/internal/util"
"github.com/rotisserie/eris"
) )
// CreateDNSProvider is the schema for incoming data validation // CreateDNSProvider is the schema for incoming data validation
@ -18,7 +20,7 @@ func CreateDNSProvider() string {
for providerName, provider := range allProviders { for providerName, provider := range allProviders {
schema, err := provider.GetJsonSchema() schema, err := provider.GetJsonSchema()
if err != nil { if err != nil {
logger.Error("ProviderSchemaError", fmt.Errorf("Invalid Provider Schema for %s: %v", provider.Title, err)) logger.Error("ProviderSchemaError", eris.Wrapf(err, "Invalid Provider Schema for %s: %v", provider.Title, err))
} else { } else {
allSchemasWrapped = append(allSchemasWrapped, createDNSProviderType(providerName, schema)) allSchemasWrapped = append(allSchemasWrapped, createDNSProviderType(providerName, schema))
} }

View File

@ -15,6 +15,7 @@ import (
"npm/internal/util" "npm/internal/util"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/rotisserie/eris"
) )
// MigrationConfiguration options for the migrator. // MigrationConfiguration options for the migrator.
@ -34,7 +35,7 @@ func ConfigureMigrator(c *MigrationConfiguration) error {
mConfiguration.mux.Lock() mConfiguration.mux.Lock()
defer mConfiguration.mux.Unlock() defer mConfiguration.mux.Unlock()
if c == nil { if c == nil {
return fmt.Errorf("a non nil Configuration is mandatory") return eris.Errorf("a non nil Configuration is mandatory")
} }
if strings.TrimSpace(c.Table) != "" { if strings.TrimSpace(c.Table) != "" {
mConfiguration.Table = c.Table mConfiguration.Table = c.Table
@ -101,7 +102,7 @@ func tableExists(db *sqlx.DB, tableName string) bool {
row := db.QueryRowx(query, tableName) row := db.QueryRowx(query, tableName)
if row == nil { if row == nil {
logger.Error("MigratorError", fmt.Errorf("Cannot check if table exists, no row returned: %v", tableName)) logger.Error("MigratorError", eris.Errorf("Cannot check if table exists, no row returned: %v", tableName))
return false return false
} }

View File

@ -2,7 +2,8 @@ package dnsproviders
import ( import (
"encoding/json" "encoding/json"
"errors"
"github.com/rotisserie/eris"
) )
// providerField should mimick jsonschema, so that // providerField should mimick jsonschema, so that
@ -111,5 +112,5 @@ func Get(provider string) (Provider, error) {
if item, found := all[provider]; found { if item, found := all[provider]; found {
return item, nil return item, nil
} }
return Provider{}, errors.New("provider_not_found") return Provider{}, eris.New("provider_not_found")
} }

View File

@ -2,7 +2,6 @@ package accesslist
import ( import (
"database/sql" "database/sql"
goerrors "errors"
"fmt" "fmt"
"npm/internal/database" "npm/internal/database"
@ -10,6 +9,8 @@ import (
"npm/internal/errors" "npm/internal/errors"
"npm/internal/logger" "npm/internal/logger"
"npm/internal/model" "npm/internal/model"
"github.com/rotisserie/eris"
) )
// GetByID finds a row by ID // GetByID finds a row by ID
@ -22,7 +23,7 @@ func GetByID(id int) (Model, error) {
// Create will create a row from this model // Create will create a row from this model
func Create(m *Model) (int, error) { func Create(m *Model) (int, error) {
if m.ID != 0 { 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) m.Touch(true)
@ -60,7 +61,7 @@ func Create(m *Model) (int, error) {
// Update will Update a row from this model // Update will Update a row from this model
func Update(m *Model) error { func Update(m *Model) error {
if m.ID == 0 { 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) m.Touch(false)

View File

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

View File

@ -1,10 +1,11 @@
package auth package auth
import ( import (
goerrors "errors"
"fmt" "fmt"
"npm/internal/database" "npm/internal/database"
"github.com/rotisserie/eris"
) )
// GetByID finds a auth by ID // 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 // Create will create a Auth from this model
func Create(auth *Model) (int, error) { func Create(auth *Model) (int, error) {
if auth.ID != 0 { 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) auth.Touch(true)
@ -62,7 +63,7 @@ func Create(auth *Model) (int, error) {
// Update will Update a Auth from this model // Update will Update a Auth from this model
func Update(auth *Model) error { func Update(auth *Model) error {
if auth.ID == 0 { 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) auth.Touch(false)

View File

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

View File

@ -2,7 +2,6 @@ package certificate
import ( import (
"database/sql" "database/sql"
goerrors "errors"
"fmt" "fmt"
"npm/internal/database" "npm/internal/database"
@ -11,6 +10,8 @@ import (
"npm/internal/jobqueue" "npm/internal/jobqueue"
"npm/internal/logger" "npm/internal/logger"
"npm/internal/model" "npm/internal/model"
"github.com/rotisserie/eris"
) )
// GetByID finds a row by ID // GetByID finds a row by ID
@ -23,7 +24,7 @@ func GetByID(id int) (Model, error) {
// Create will create a row from this model // Create will create a row from this model
func Create(certificate *Model) (int, error) { func Create(certificate *Model) (int, error) {
if certificate.ID != 0 { 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) certificate.Touch(true)
@ -77,7 +78,7 @@ func Create(certificate *Model) (int, error) {
// Update will Update a Auth from this model // Update will Update a Auth from this model
func Update(certificate *Model) error { func Update(certificate *Model) error {
if certificate.ID == 0 { 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) certificate.Touch(false)

View File

@ -1,7 +1,6 @@
package certificate package certificate
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"regexp" "regexp"
@ -17,6 +16,8 @@ import (
"npm/internal/logger" "npm/internal/logger"
"npm/internal/types" "npm/internal/types"
"npm/internal/util" "npm/internal/util"
"github.com/rotisserie/eris"
) )
const ( const (
@ -90,15 +91,15 @@ func (m *Model) Save() error {
var err error var err error
if m.UserID == 0 { if m.UserID == 0 {
return fmt.Errorf("User ID must be specified") return eris.Errorf("User ID must be specified")
} }
if !m.Validate() { 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() { 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() m.setDefaultStatus()
@ -215,7 +216,7 @@ func (m *Model) Expand(items []string) error {
// Returns: (key, fullchain, certFolder) // Returns: (key, fullchain, certFolder)
func (m *Model) GetCertificateLocations() (string, string, string) { func (m *Model) GetCertificateLocations() (string, string, string) {
if m.ID == 0 { 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 "", "", "" return "", "", ""
} }

View File

@ -2,7 +2,6 @@ package certificateauthority
import ( import (
"database/sql" "database/sql"
goerrors "errors"
"fmt" "fmt"
"npm/internal/database" "npm/internal/database"
@ -10,6 +9,8 @@ import (
"npm/internal/errors" "npm/internal/errors"
"npm/internal/logger" "npm/internal/logger"
"npm/internal/model" "npm/internal/model"
"github.com/rotisserie/eris"
) )
// GetByID finds a row by ID // GetByID finds a row by ID
@ -22,7 +23,7 @@ func GetByID(id int) (Model, error) {
// Create will create a row from this model // Create will create a row from this model
func Create(ca *Model) (int, error) { func Create(ca *Model) (int, error) {
if ca.ID != 0 { 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) ca.Touch(true)
@ -64,7 +65,7 @@ func Create(ca *Model) (int, error) {
// Update will Update a row from this model // Update will Update a row from this model
func Update(ca *Model) error { func Update(ca *Model) error {
if ca.ID == 0 { 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) ca.Touch(false)

View File

@ -1,7 +1,6 @@
package certificateauthority package certificateauthority
import ( import (
goerrors "errors"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -10,6 +9,8 @@ import (
"npm/internal/database" "npm/internal/database"
"npm/internal/errors" "npm/internal/errors"
"npm/internal/types" "npm/internal/types"
"github.com/rotisserie/eris"
) )
const ( const (
@ -79,7 +80,7 @@ func (m *Model) Check() error {
var err error var err error
if m.CABundle != "" { 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 err = errors.ErrCABundleDoesNotExist
} }
} }

View File

@ -2,7 +2,6 @@ package dnsprovider
import ( import (
"database/sql" "database/sql"
goerrors "errors"
"fmt" "fmt"
"npm/internal/database" "npm/internal/database"
@ -10,6 +9,8 @@ import (
"npm/internal/errors" "npm/internal/errors"
"npm/internal/logger" "npm/internal/logger"
"npm/internal/model" "npm/internal/model"
"github.com/rotisserie/eris"
) )
// GetByID finds a row by ID // GetByID finds a row by ID
@ -22,7 +23,7 @@ func GetByID(id int) (Model, error) {
// Create will create a row from this model // Create will create a row from this model
func Create(provider *Model) (int, error) { func Create(provider *Model) (int, error) {
if provider.ID != 0 { 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) provider.Touch(true)
@ -64,7 +65,7 @@ func Create(provider *Model) (int, error) {
// Update will Update a row from this model // Update will Update a row from this model
func Update(provider *Model) error { func Update(provider *Model) error {
if provider.ID == 0 { 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) provider.Touch(false)

View File

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

View File

@ -2,7 +2,6 @@ package host
import ( import (
"database/sql" "database/sql"
goerrors "errors"
"fmt" "fmt"
"npm/internal/database" "npm/internal/database"
@ -10,6 +9,8 @@ import (
"npm/internal/errors" "npm/internal/errors"
"npm/internal/logger" "npm/internal/logger"
"npm/internal/model" "npm/internal/model"
"github.com/rotisserie/eris"
) )
// GetByID finds a Host by ID // GetByID finds a Host by ID
@ -22,7 +23,7 @@ func GetByID(id int) (Model, error) {
// create will create a Host from this model // create will create a Host from this model
func create(host *Model) (int, error) { func create(host *Model) (int, error) {
if host.ID != 0 { 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) host.Touch(true)
@ -102,7 +103,7 @@ func create(host *Model) (int, error) {
// update will Update a Host from this model // update will Update a Host from this model
func update(host *Model) error { func update(host *Model) error {
if host.ID == 0 { 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) host.Touch(false)

View File

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

View File

@ -2,7 +2,6 @@ package nginxtemplate
import ( import (
"database/sql" "database/sql"
goerrors "errors"
"fmt" "fmt"
"npm/internal/database" "npm/internal/database"
@ -10,6 +9,8 @@ import (
"npm/internal/errors" "npm/internal/errors"
"npm/internal/logger" "npm/internal/logger"
"npm/internal/model" "npm/internal/model"
"github.com/rotisserie/eris"
) )
// GetByID finds a Host by ID // GetByID finds a Host by ID
@ -22,7 +23,7 @@ func GetByID(id int) (Model, error) {
// Create will create a Host from this model // Create will create a Host from this model
func Create(host *Model) (int, error) { func Create(host *Model) (int, error) {
if host.ID != 0 { 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) host.Touch(true)
@ -62,7 +63,7 @@ func Create(host *Model) (int, error) {
// Update will Update a Host from this model // Update will Update a Host from this model
func Update(host *Model) error { func Update(host *Model) error {
if host.ID == 0 { 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) host.Touch(false)

View File

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

View File

@ -2,7 +2,6 @@ package setting
import ( import (
"database/sql" "database/sql"
goerrors "errors"
"fmt" "fmt"
"npm/internal/database" "npm/internal/database"
@ -10,6 +9,8 @@ import (
"npm/internal/errors" "npm/internal/errors"
"npm/internal/logger" "npm/internal/logger"
"npm/internal/model" "npm/internal/model"
"github.com/rotisserie/eris"
) )
// GetByID finds a setting by ID // GetByID finds a setting by ID
@ -29,7 +30,7 @@ func GetByName(name string) (Model, error) {
// Create will Create a Setting from this model // Create will Create a Setting from this model
func Create(setting *Model) (int, error) { func Create(setting *Model) (int, error) {
if setting.ID != 0 { 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) setting.Touch(true)
@ -63,7 +64,7 @@ func Create(setting *Model) (int, error) {
// Update will Update a Setting from this model // Update will Update a Setting from this model
func Update(setting *Model) error { func Update(setting *Model) error {
if setting.ID == 0 { 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) setting.Touch(false)

View File

@ -2,7 +2,6 @@ package stream
import ( import (
"database/sql" "database/sql"
goerrors "errors"
"fmt" "fmt"
"npm/internal/database" "npm/internal/database"
@ -10,6 +9,8 @@ import (
"npm/internal/errors" "npm/internal/errors"
"npm/internal/logger" "npm/internal/logger"
"npm/internal/model" "npm/internal/model"
"github.com/rotisserie/eris"
) )
// GetByID finds a auth by ID // GetByID finds a auth by ID
@ -22,7 +23,7 @@ func GetByID(id int) (Model, error) {
// Create will create a Auth from this model // Create will create a Auth from this model
func Create(host *Model) (int, error) { func Create(host *Model) (int, error) {
if host.ID != 0 { 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) host.Touch(true)
@ -66,7 +67,7 @@ func Create(host *Model) (int, error) {
// Update will Update a Host from this model // Update will Update a Host from this model
func Update(host *Model) error { func Update(host *Model) error {
if host.ID == 0 { 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) host.Touch(false)

View File

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

View File

@ -2,7 +2,6 @@ package upstream
import ( import (
"database/sql" "database/sql"
goerrors "errors"
"fmt" "fmt"
"npm/internal/database" "npm/internal/database"
@ -10,6 +9,8 @@ import (
"npm/internal/errors" "npm/internal/errors"
"npm/internal/logger" "npm/internal/logger"
"npm/internal/model" "npm/internal/model"
"github.com/rotisserie/eris"
) )
// GetByID finds a Upstream by ID // GetByID finds a Upstream by ID
@ -22,7 +23,7 @@ func GetByID(id int) (Model, error) {
// create will create a Upstream from this model // create will create a Upstream from this model
func create(u *Model) (int, error) { func create(u *Model) (int, error) {
if u.ID != 0 { 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) u.Touch(true)
@ -80,7 +81,7 @@ func create(u *Model) (int, error) {
// update will Update a Upstream from this model // update will Update a Upstream from this model
func update(u *Model) error { func update(u *Model) error {
if u.ID == 0 { 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) u.Touch(false)

View File

@ -12,6 +12,8 @@ import (
"npm/internal/status" "npm/internal/status"
"npm/internal/types" "npm/internal/types"
"npm/internal/util" "npm/internal/util"
"github.com/rotisserie/eris"
) )
const ( const (
@ -73,7 +75,7 @@ func (m *Model) Save(skipConfiguration bool) error {
var err error var err error
if m.UserID == 0 { 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 // ensure name is trimmed of whitespace

View File

@ -2,7 +2,6 @@ package upstreamserver
import ( import (
"database/sql" "database/sql"
goerrors "errors"
"fmt" "fmt"
"npm/internal/database" "npm/internal/database"
@ -10,6 +9,8 @@ import (
"npm/internal/errors" "npm/internal/errors"
"npm/internal/logger" "npm/internal/logger"
"npm/internal/model" "npm/internal/model"
"github.com/rotisserie/eris"
) )
// GetByID finds a Upstream Server by ID // 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 // create will create a Upstream Server from this model
func create(u *Model) (int, error) { func create(u *Model) (int, error) {
if u.ID != 0 { 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) u.Touch(true)
@ -82,7 +83,7 @@ func create(u *Model) (int, error) {
// update will Update a Upstream from this model // update will Update a Upstream from this model
func update(u *Model) error { func update(u *Model) error {
if u.ID == 0 { 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) u.Touch(false)

View File

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

View File

@ -2,7 +2,6 @@ package user
import ( import (
"database/sql" "database/sql"
goerrors "errors"
"fmt" "fmt"
"npm/internal/database" "npm/internal/database"
@ -10,6 +9,8 @@ import (
"npm/internal/errors" "npm/internal/errors"
"npm/internal/logger" "npm/internal/logger"
"npm/internal/model" "npm/internal/model"
"github.com/rotisserie/eris"
) )
// GetByID finds a user by ID // 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. // database schema, but it's a bit more complex because of the is_deleted field.
if user.ID != 0 { 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 // 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 // Update will Update a User from this model
func Update(user *Model) error { func Update(user *Model) error {
if user.ID == 0 { 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 // Check that the email address isn't associated with another user

View File

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

View File

@ -1,16 +1,18 @@
package errors package errors
import "errors" import (
"github.com/rotisserie/eris"
)
// All error messages used by the service package to report // All error messages used by the service package to report
// problems back to calling clients // problems back to calling clients
var ( var (
ErrDatabaseUnavailable = errors.New("database-unavailable") ErrDatabaseUnavailable = eris.New("database-unavailable")
ErrDuplicateEmailUser = errors.New("email-already-exists") ErrDuplicateEmailUser = eris.New("email-already-exists")
ErrInvalidLogin = errors.New("invalid-login-credentials") ErrInvalidLogin = eris.New("invalid-login-credentials")
ErrUserDisabled = errors.New("user-disabled") ErrUserDisabled = eris.New("user-disabled")
ErrSystemUserReadonly = errors.New("cannot-save-system-users") ErrSystemUserReadonly = eris.New("cannot-save-system-users")
ErrValidationFailed = errors.New("request-failed-validation") ErrValidationFailed = eris.New("request-failed-validation")
ErrCurrentPasswordInvalid = errors.New("current-password-invalid") ErrCurrentPasswordInvalid = eris.New("current-password-invalid")
ErrCABundleDoesNotExist = errors.New("ca-bundle-does-not-exist") ErrCABundleDoesNotExist = eris.New("ca-bundle-does-not-exist")
) )

View File

@ -2,7 +2,8 @@ package jobqueue
import ( import (
"context" "context"
"errors"
"github.com/rotisserie/eris"
) )
var ( var (
@ -30,7 +31,7 @@ func Start() {
// Shutdown will gracefully stop the queue // Shutdown will gracefully stop the queue
func Shutdown() error { func Shutdown() error {
if cancel == nil { if cancel == nil {
return errors.New("Unable to shutdown, jobqueue has not been started") return eris.New("Unable to shutdown, jobqueue has not been started")
} }
cancel() cancel()
return nil return nil
@ -39,7 +40,7 @@ func Shutdown() error {
// AddJob adds a job to the queue for processing // AddJob adds a job to the queue for processing
func AddJob(j Job) error { func AddJob(j Job) error {
if worker == nil { if worker == nil {
return errors.New("Unable to add job, jobqueue has not been started") return eris.New("Unable to add job, jobqueue has not been started")
} }
worker.Queue.AddJob(j) worker.Queue.AddJob(j)
return nil return nil

View File

@ -1,13 +1,13 @@
package jwt package jwt
import ( import (
"fmt"
"time" "time"
"npm/internal/entity/user" "npm/internal/entity/user"
"npm/internal/logger" "npm/internal/logger"
"github.com/dgrijalva/jwt-go" "github.com/dgrijalva/jwt-go"
"github.com/rotisserie/eris"
) )
// UserJWTClaims is the structure of a JWT for a User // UserJWTClaims is the structure of a JWT for a User
@ -47,7 +47,7 @@ func Generate(userObj *user.Model) (GeneratedResponse, error) {
var err error var err error
token.Signature, err = token.SignedString(key) token.Signature, err = token.SignedString(key)
if err != nil { if err != nil {
logger.Error("JWTError", fmt.Errorf("Error signing token: %v", err)) logger.Error("JWTError", eris.Wrapf(err, "Error signing token: %v", err))
return response, err return response, err
} }

View File

@ -4,9 +4,10 @@ import (
"crypto/rsa" "crypto/rsa"
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"errors"
"npm/internal/config" "npm/internal/config"
"github.com/rotisserie/eris"
) )
var ( var (
@ -21,7 +22,7 @@ func GetPrivateKey() (*rsa.PrivateKey, error) {
var blankKey *rsa.PrivateKey var blankKey *rsa.PrivateKey
if config.PrivateKey == "" { if config.PrivateKey == "" {
return blankKey, errors.New("Could not get Private Key from configuration") return blankKey, eris.New("Could not get Private Key from configuration")
} }
var err error var err error
@ -48,7 +49,7 @@ func GetPublicKey() (*rsa.PublicKey, error) {
var blankKey *rsa.PublicKey var blankKey *rsa.PublicKey
if config.PublicKey == "" { if config.PublicKey == "" {
return blankKey, errors.New("Could not get Public Key filename, check environment variables") return blankKey, eris.New("Could not get Public Key filename, check environment variables")
} }
var err error var err error

View File

@ -11,6 +11,7 @@ import (
"github.com/fatih/color" "github.com/fatih/color"
"github.com/getsentry/sentry-go" "github.com/getsentry/sentry-go"
"github.com/rotisserie/eris"
) )
var colorReset, colorGray, colorYellow, colorBlue, colorRed, colorMagenta, colorBlack, colorWhite *color.Color var colorReset, colorGray, colorYellow, colorBlue, colorRed, colorMagenta, colorBlack, colorWhite *color.Color
@ -98,7 +99,7 @@ func (l *Logger) Configure(c *Config) error {
defer l.mux.Unlock() defer l.mux.Unlock()
if c == nil { if c == nil {
return fmt.Errorf("a non nil Config is mandatory") return eris.Errorf("a non nil Config is mandatory")
} }
if err := c.LogThreshold.validate(); err != nil { if err := c.LogThreshold.validate(); err != nil {
@ -125,7 +126,7 @@ func (l Level) validate() error {
case DebugLevel, InfoLevel, WarnLevel, ErrorLevel: case DebugLevel, InfoLevel, WarnLevel, ErrorLevel:
return nil return nil
default: default:
return fmt.Errorf("invalid \"Level\" %d", l) return eris.Errorf("invalid \"Level\" %d", l)
} }
} }

View File

@ -2,14 +2,13 @@ package logger
import ( import (
"bytes" "bytes"
"errors"
"fmt"
"io" "io"
"log" "log"
"os" "os"
"testing" "testing"
"github.com/getsentry/sentry-go" "github.com/getsentry/sentry-go"
"github.com/rotisserie/eris"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -34,7 +33,7 @@ func TestThreshold(t *testing.T) {
Info("this should display") Info("this should display")
assert.NotEmpty(t, buf.String()) assert.NotEmpty(t, buf.String())
Error("ErrorClass", errors.New("this should display")) Error("ErrorClass", eris.New("this should display"))
assert.NotEmpty(t, buf.String()) assert.NotEmpty(t, buf.String())
} }
@ -97,7 +96,7 @@ func TestError(t *testing.T) {
LogThreshold: ErrorLevel, LogThreshold: ErrorLevel,
})) }))
Error("TestErrorClass", fmt.Errorf("this is a %s error", "test")) Error("TestErrorClass", eris.Errorf("this is a %s error", "test"))
assert.Contains(t, buf.String(), "ERROR") assert.Contains(t, buf.String(), "ERROR")
assert.Contains(t, buf.String(), "this is a test error") assert.Contains(t, buf.String(), "this is a test error")
} }

View File

@ -1,10 +1,11 @@
package nginx package nginx
import ( import (
"fmt"
"os/exec" "os/exec"
"npm/internal/logger" "npm/internal/logger"
"github.com/rotisserie/eris"
) )
func reloadNginx() (string, error) { func reloadNginx() (string, error) {
@ -14,7 +15,7 @@ func reloadNginx() (string, error) {
func getNginxFilePath() (string, error) { func getNginxFilePath() (string, error) {
path, err := exec.LookPath("nginx") path, err := exec.LookPath("nginx")
if err != nil { if err != nil {
return path, fmt.Errorf("Cannot find nginx execuatable script in PATH") return path, eris.Wrapf(err, "Cannot find nginx execuatable script in PATH")
} }
return path, nil return path, nil
} }
@ -34,7 +35,7 @@ func shExec(args []string) (string, error) {
b, e := c.CombinedOutput() b, e := c.CombinedOutput()
if e != nil { if e != nil {
logger.Error("NginxError", fmt.Errorf("Command error: %s -- %v\n%+v", ng, args, e)) logger.Error("NginxError", eris.Wrapf(err, "Command error: %s -- %v\n%+v", ng, args, e))
logger.Warn(string(b)) logger.Warn(string(b))
} }

View File

@ -3,7 +3,8 @@ package types
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/json" "encoding/json"
"fmt"
"github.com/rotisserie/eris"
) )
// JSONB can be anything // JSONB can be anything
@ -28,7 +29,7 @@ func (j *JSONB) Scan(src interface{}) error {
case []uint8: case []uint8:
srcString = string(src.([]uint8)) srcString = string(src.([]uint8))
default: default:
return fmt.Errorf("Incompatible type for JSONB: %v", v) return eris.Errorf("Incompatible type for JSONB: %v", v)
} }
jsonb.Encoded = srcString jsonb.Encoded = srcString

View File

@ -1,12 +1,12 @@
package validator package validator
import ( import (
"fmt"
"npm/internal/entity/certificate" "npm/internal/entity/certificate"
"npm/internal/entity/host" "npm/internal/entity/host"
"npm/internal/entity/nginxtemplate" "npm/internal/entity/nginxtemplate"
"npm/internal/entity/upstream" "npm/internal/entity/upstream"
"github.com/rotisserie/eris"
) )
// ValidateHost will check if associated objects exist and other checks // ValidateHost will check if associated objects exist and other checks
@ -17,32 +17,32 @@ func ValidateHost(h host.Model) error {
// This will not determine if the certificate is Ready to use, // This will not determine if the certificate is Ready to use,
// as this validation only cares that the row exists. // as this validation only cares that the row exists.
if _, cErr := certificate.GetByID(h.CertificateID); cErr != nil { if _, cErr := certificate.GetByID(h.CertificateID); cErr != nil {
return fmt.Errorf("Certificate #%d does not exist", h.CertificateID) return eris.Wrapf(cErr, "Certificate #%d does not exist", h.CertificateID)
} }
} }
if h.UpstreamID > 0 { if h.UpstreamID > 0 {
// Check upstream exists // Check upstream exists
if _, uErr := upstream.GetByID(h.UpstreamID); uErr != nil { if _, uErr := upstream.GetByID(h.UpstreamID); uErr != nil {
return fmt.Errorf("Upstream #%d does not exist", h.UpstreamID) return eris.Wrapf(uErr, "Upstream #%d does not exist", h.UpstreamID)
} }
} }
// Ensure either UpstreamID is set or appropriate proxy host params are set // Ensure either UpstreamID is set or appropriate proxy host params are set
if h.UpstreamID > 0 && (h.ProxyHost != "" || h.ProxyPort > 0) { if h.UpstreamID > 0 && (h.ProxyHost != "" || h.ProxyPort > 0) {
return fmt.Errorf("Proxy Host or Port cannot be set when using an Upstream") return eris.Errorf("Proxy Host or Port cannot be set when using an Upstream")
} }
if h.UpstreamID == 0 && (h.ProxyHost == "" || h.ProxyPort < 1) { if h.UpstreamID == 0 && (h.ProxyHost == "" || h.ProxyPort < 1) {
return fmt.Errorf("Proxy Host and Port must be specified, unless using an Upstream") return eris.Errorf("Proxy Host and Port must be specified, unless using an Upstream")
} }
// Check the nginx template exists and has the same type. // Check the nginx template exists and has the same type.
nginxTemplate, tErr := nginxtemplate.GetByID(h.NginxTemplateID) nginxTemplate, tErr := nginxtemplate.GetByID(h.NginxTemplateID)
if tErr != nil { if tErr != nil {
return fmt.Errorf("Host Template #%d does not exist", h.NginxTemplateID) return eris.Wrapf(tErr, "Host Template #%d does not exist", h.NginxTemplateID)
} }
if nginxTemplate.Type != h.Type { if nginxTemplate.Type != h.Type {
return fmt.Errorf("Host Template #%d is not valid for this host type", h.NginxTemplateID) return eris.Errorf("Host Template #%d is not valid for this host type", h.NginxTemplateID)
} }
return nil return nil

View File

@ -1,11 +1,10 @@
package validator package validator
import ( import (
"errors"
"fmt"
"npm/internal/entity/nginxtemplate" "npm/internal/entity/nginxtemplate"
"npm/internal/entity/upstream" "npm/internal/entity/upstream"
"github.com/rotisserie/eris"
) )
// ValidateUpstream will check if associated objects exist and other checks // ValidateUpstream will check if associated objects exist and other checks
@ -13,7 +12,7 @@ import (
func ValidateUpstream(u upstream.Model) error { func ValidateUpstream(u upstream.Model) error {
// Needs to have more than 1 server // Needs to have more than 1 server
if len(u.Servers) < 2 { if len(u.Servers) < 2 {
return errors.New("Upstreams require at least 2 servers") return eris.New("Upstreams require at least 2 servers")
} }
// Backup servers aren't permitted with hash balancing // Backup servers aren't permitted with hash balancing
@ -21,7 +20,7 @@ func ValidateUpstream(u upstream.Model) error {
// check all servers for a backup param // check all servers for a backup param
for _, server := range u.Servers { for _, server := range u.Servers {
if server.Backup { if server.Backup {
return errors.New("Backup servers cannot be used with hash balancing") return eris.New("Backup servers cannot be used with hash balancing")
} }
} }
} }
@ -29,10 +28,10 @@ func ValidateUpstream(u upstream.Model) error {
// Check the nginx template exists and has the same type. // Check the nginx template exists and has the same type.
nginxTemplate, err := nginxtemplate.GetByID(u.NginxTemplateID) nginxTemplate, err := nginxtemplate.GetByID(u.NginxTemplateID)
if err != nil { if err != nil {
return fmt.Errorf("Nginx Template #%d does not exist", u.NginxTemplateID) return eris.Errorf("Nginx Template #%d does not exist", u.NginxTemplateID)
} }
if nginxTemplate.Type != "upstream" { if nginxTemplate.Type != "upstream" {
return fmt.Errorf("Host Template #%d is not valid for this upstream", u.NginxTemplateID) return eris.Errorf("Host Template #%d is not valid for this upstream", u.NginxTemplateID)
} }
return nil return nil