Skip to content

Commit c288886

Browse files
committed
Use eris for error management
1 parent 80315bd commit c288886

File tree

44 files changed

+173
-128
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+173
-128
lines changed

backend/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ require (
1717
github.com/mattn/go-sqlite3 v1.14.16
1818
github.com/patrickmn/go-cache v2.1.0+incompatible
1919
github.com/qri-io/jsonschema v0.2.1
20+
github.com/rotisserie/eris v0.5.4
2021
github.com/stretchr/testify v1.7.0
2122
github.com/vrischmann/envconfig v1.3.0
2223
golang.org/x/crypto v0.5.0

backend/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ github.com/qri-io/jsonpointer v0.1.1/go.mod h1:DnJPaYgiKu56EuDp8TU5wFLdZIcAnb/uH
7676
github.com/qri-io/jsonschema v0.2.1 h1:NNFoKms+kut6ABPf6xiKNM5214jzxAhDBrPHCJ97Wg0=
7777
github.com/qri-io/jsonschema v0.2.1/go.mod h1:g7DPkiOsK1xv6T/Ao5scXRkd+yTFygcANPBaaqW+VrI=
7878
github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc=
79+
github.com/rotisserie/eris v0.5.4 h1:Il6IvLdAapsMhvuOahHWiBnl1G++Q0/L5UIkI5mARSk=
80+
github.com/rotisserie/eris v0.5.4/go.mod h1:Z/kgYTJiJtocxCbFfvRmO+QejApzG6zpyky9G1A4g9s=
7981
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
8082
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
8183
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

backend/internal/acme/acmesh.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ import (
1313
"npm/internal/entity/certificateauthority"
1414
"npm/internal/entity/dnsprovider"
1515
"npm/internal/logger"
16+
17+
"github.com/rotisserie/eris"
1618
)
1719

1820
func getAcmeShFilePath() (string, error) {
1921
path, err := exec.LookPath("acme.sh")
2022
if err != nil {
21-
return path, fmt.Errorf("Cannot find acme.sh execuatable script in PATH")
23+
return path, eris.Wrapf(err, "Cannot find acme.sh execuatable script in PATH")
2224
}
2325
return path, nil
2426
}
@@ -107,7 +109,7 @@ func shExec(args []string, envs []string) (string, error) {
107109
b, e := c.CombinedOutput()
108110

109111
if e != nil {
110-
// logger.Error("AcmeShError", fmt.Errorf("Command error: %s -- %v\n%+v", acmeSh, args, e))
112+
// logger.Error("AcmeShError", eris.Wrapf(e, "Command error: %s -- %v\n%+v", acmeSh, args, e))
111113
logger.Warn(string(b))
112114
}
113115

backend/internal/acme/errors.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package acme
22

3-
import "errors"
3+
import (
4+
"github.com/rotisserie/eris"
5+
)
46

57
// All errors relating to Acme.sh use
68
var (
7-
ErrDNSNeedsDNSProvider = errors.New("RequestCert dns method requires a dns provider")
8-
ErrHTTPHasDNSProvider = errors.New("RequestCert http method does not need a dns provider")
9-
ErrMethodNotSupported = errors.New("RequestCert method not supported")
9+
ErrDNSNeedsDNSProvider = eris.New("RequestCert dns method requires a dns provider")
10+
ErrHTTPHasDNSProvider = eris.New("RequestCert http method does not need a dns provider")
11+
ErrMethodNotSupported = eris.New("RequestCert method not supported")
1012
)

backend/internal/api/handler/helpers.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package handler
22

33
import (
4-
"fmt"
54
"net/http"
65
"strconv"
76
"strings"
@@ -11,6 +10,7 @@ import (
1110
"npm/internal/model"
1211

1312
"github.com/go-chi/chi"
13+
"github.com/rotisserie/eris"
1414
)
1515

1616
const defaultLimit = 10
@@ -45,15 +45,15 @@ func getDateRanges(r *http.Request) (time.Time, time.Time, error) {
4545
var fromErr error
4646
fromDate, fromErr = time.Parse(time.RFC3339, from)
4747
if fromErr != nil {
48-
return fromDate, toDate, fmt.Errorf("From date is not in correct format: %v", strings.ReplaceAll(time.RFC3339, "Z", "+"))
48+
return fromDate, toDate, eris.Errorf("From date is not in correct format: %v", strings.ReplaceAll(time.RFC3339, "Z", "+"))
4949
}
5050
}
5151

5252
if to != "" {
5353
var toErr error
5454
toDate, toErr = time.Parse(time.RFC3339, to)
5555
if toErr != nil {
56-
return fromDate, toDate, fmt.Errorf("To date is not in correct format: %v", strings.ReplaceAll(time.RFC3339, "Z", "+"))
56+
return fromDate, toDate, eris.Errorf("To date is not in correct format: %v", strings.ReplaceAll(time.RFC3339, "Z", "+"))
5757
}
5858
}
5959

@@ -104,14 +104,14 @@ func getQueryVarInt(r *http.Request, varName string, required bool, defaultValue
104104
varValue := queryValues.Get(varName)
105105

106106
if varValue == "" && required {
107-
return 0, fmt.Errorf("%v was not supplied in the request", varName)
107+
return 0, eris.Errorf("%v was not supplied in the request", varName)
108108
} else if varValue == "" {
109109
return defaultValue, nil
110110
}
111111

112112
varInt, intErr := strconv.Atoi(varValue)
113113
if intErr != nil {
114-
return 0, fmt.Errorf("%v is not a valid number", varName)
114+
return 0, eris.Wrapf(intErr, "%v is not a valid number", varName)
115115
}
116116

117117
return varInt, nil
@@ -123,7 +123,7 @@ func getQueryVarBool(r *http.Request, varName string, required bool, defaultValu
123123
varValue := queryValues.Get(varName)
124124
125125
if varValue == "" && required {
126-
return false, fmt.Errorf("%v was not supplied in the request", varName)
126+
return false, eris.Errorf("%v was not supplied in the request", varName)
127127
} else if varValue == "" {
128128
return defaultValue, nil
129129
}
@@ -140,13 +140,13 @@ func getURLParamInt(r *http.Request, varName string) (int, error) {
140140
var paramInt int
141141

142142
if paramStr == "" && required {
143-
return 0, fmt.Errorf("%v was not supplied in the request", varName)
143+
return 0, eris.Errorf("%v was not supplied in the request", varName)
144144
} else if paramStr == "" {
145145
return defaultValue, nil
146146
}
147147

148148
if paramInt, err = strconv.Atoi(paramStr); err != nil {
149-
return 0, fmt.Errorf("%v is not a valid number", varName)
149+
return 0, eris.Wrapf(err, "%v is not a valid number", varName)
150150
}
151151

152152
return paramInt, nil
@@ -158,7 +158,7 @@ func getURLParamString(r *http.Request, varName string) (string, error) {
158158
paramStr := chi.URLParam(r, varName)
159159

160160
if paramStr == "" && required {
161-
return "", fmt.Errorf("%v was not supplied in the request", varName)
161+
return "", eris.Errorf("%v was not supplied in the request", varName)
162162
} else if paramStr == "" {
163163
return defaultValue, nil
164164
}

backend/internal/api/handler/not_found.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package handler
22

33
import (
4-
"errors"
54
"io"
65
"io/fs"
76
"mime"
@@ -11,11 +10,13 @@ import (
1110

1211
"npm/embed"
1312
h "npm/internal/api/http"
13+
14+
"github.com/rotisserie/eris"
1415
)
1516

1617
var (
1718
assetsSub fs.FS
18-
errIsDir = errors.New("path is dir")
19+
errIsDir = eris.New("path is dir")
1920
)
2021

2122
// NotFound is a json error handler for 404's and method not allowed.

backend/internal/api/http/requests.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ package http
33
import (
44
"context"
55
"encoding/json"
6-
"errors"
76

87
"github.com/qri-io/jsonschema"
8+
"github.com/rotisserie/eris"
99
)
1010

1111
var (
1212
// ErrInvalidJSON is an error for invalid json
13-
ErrInvalidJSON = errors.New("JSON is invalid")
13+
ErrInvalidJSON = eris.New("JSON is invalid")
1414
// ErrInvalidPayload is an error for invalid incoming data
15-
ErrInvalidPayload = errors.New("Payload is invalid")
15+
ErrInvalidPayload = eris.New("Payload is invalid")
1616
)
1717

1818
// ValidateRequestSchema takes a Schema and the Content to validate against it

backend/internal/api/middleware/schema.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ package middleware
33
import (
44
"context"
55
"encoding/json"
6-
"fmt"
76
"net/http"
87

98
c "npm/internal/api/context"
109
h "npm/internal/api/http"
1110

1211
"github.com/qri-io/jsonschema"
12+
"github.com/rotisserie/eris"
1313
)
1414

1515
// CheckRequestSchema checks the payload against schema
1616
func CheckRequestSchema(ctx context.Context, schemaData string, payload []byte) ([]jsonschema.KeyError, error) {
1717
// Create root schema
1818
rs := &jsonschema.Schema{}
1919
if err := json.Unmarshal([]byte(schemaData), rs); err != nil {
20-
return nil, fmt.Errorf("Schema Fatal: %v", err)
20+
return nil, eris.Wrapf(err, "Schema Fatal: %v", err)
2121
}
2222

2323
// Validate it

backend/internal/api/schema/create_dns_provider.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"npm/internal/dnsproviders"
88
"npm/internal/logger"
99
"npm/internal/util"
10+
11+
"github.com/rotisserie/eris"
1012
)
1113

1214
// CreateDNSProvider is the schema for incoming data validation
@@ -18,7 +20,7 @@ func CreateDNSProvider() string {
1820
for providerName, provider := range allProviders {
1921
schema, err := provider.GetJsonSchema()
2022
if err != nil {
21-
logger.Error("ProviderSchemaError", fmt.Errorf("Invalid Provider Schema for %s: %v", provider.Title, err))
23+
logger.Error("ProviderSchemaError", eris.Wrapf(err, "Invalid Provider Schema for %s: %v", provider.Title, err))
2224
} else {
2325
allSchemasWrapped = append(allSchemasWrapped, createDNSProviderType(providerName, schema))
2426
}

backend/internal/database/migrator.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"npm/internal/util"
1616

1717
"github.com/jmoiron/sqlx"
18+
"github.com/rotisserie/eris"
1819
)
1920

2021
// MigrationConfiguration options for the migrator.
@@ -34,7 +35,7 @@ func ConfigureMigrator(c *MigrationConfiguration) error {
3435
mConfiguration.mux.Lock()
3536
defer mConfiguration.mux.Unlock()
3637
if c == nil {
37-
return fmt.Errorf("a non nil Configuration is mandatory")
38+
return eris.Errorf("a non nil Configuration is mandatory")
3839
}
3940
if strings.TrimSpace(c.Table) != "" {
4041
mConfiguration.Table = c.Table
@@ -101,7 +102,7 @@ func tableExists(db *sqlx.DB, tableName string) bool {
101102

102103
row := db.QueryRowx(query, tableName)
103104
if row == nil {
104-
logger.Error("MigratorError", fmt.Errorf("Cannot check if table exists, no row returned: %v", tableName))
105+
logger.Error("MigratorError", eris.Errorf("Cannot check if table exists, no row returned: %v", tableName))
105106
return false
106107
}
107108

0 commit comments

Comments
 (0)