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

@ -1,7 +1,6 @@
package handler
import (
"fmt"
"net/http"
"strconv"
"strings"
@ -11,6 +10,7 @@ import (
"npm/internal/model"
"github.com/go-chi/chi"
"github.com/rotisserie/eris"
)
const defaultLimit = 10
@ -45,7 +45,7 @@ func getDateRanges(r *http.Request) (time.Time, time.Time, error) {
var fromErr error
fromDate, fromErr = time.Parse(time.RFC3339, from)
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
toDate, toErr = time.Parse(time.RFC3339, to)
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)
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 == "" {
return defaultValue, nil
}
varInt, intErr := strconv.Atoi(varValue)
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
@ -123,7 +123,7 @@ func getQueryVarBool(r *http.Request, varName string, required bool, defaultValu
varValue := queryValues.Get(varName)
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 == "" {
return defaultValue, nil
}
@ -140,13 +140,13 @@ func getURLParamInt(r *http.Request, varName string) (int, error) {
var paramInt int
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 == "" {
return defaultValue, 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
@ -158,7 +158,7 @@ func getURLParamString(r *http.Request, varName string) (string, error) {
paramStr := chi.URLParam(r, varName)
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 == "" {
return defaultValue, nil
}

View File

@ -1,7 +1,6 @@
package handler
import (
"errors"
"io"
"io/fs"
"mime"
@ -11,11 +10,13 @@ import (
"npm/embed"
h "npm/internal/api/http"
"github.com/rotisserie/eris"
)
var (
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.

View File

@ -3,16 +3,16 @@ package http
import (
"context"
"encoding/json"
"errors"
"github.com/qri-io/jsonschema"
"github.com/rotisserie/eris"
)
var (
// 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 = errors.New("Payload is invalid")
ErrInvalidPayload = eris.New("Payload is invalid")
)
// ValidateRequestSchema takes a Schema and the Content to validate against it

View File

@ -3,13 +3,13 @@ package middleware
import (
"context"
"encoding/json"
"fmt"
"net/http"
c "npm/internal/api/context"
h "npm/internal/api/http"
"github.com/qri-io/jsonschema"
"github.com/rotisserie/eris"
)
// CheckRequestSchema checks the payload against schema
@ -17,7 +17,7 @@ func CheckRequestSchema(ctx context.Context, schemaData string, payload []byte)
// Create root schema
rs := &jsonschema.Schema{}
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

View File

@ -7,6 +7,8 @@ import (
"npm/internal/dnsproviders"
"npm/internal/logger"
"npm/internal/util"
"github.com/rotisserie/eris"
)
// CreateDNSProvider is the schema for incoming data validation
@ -18,7 +20,7 @@ func CreateDNSProvider() string {
for providerName, provider := range allProviders {
schema, err := provider.GetJsonSchema()
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 {
allSchemasWrapped = append(allSchemasWrapped, createDNSProviderType(providerName, schema))
}