Skip to content

Commit

Permalink
replaced errors.Wrap and other obsolete functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Hempel committed Oct 28, 2024
1 parent 536312f commit 27423dc
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 55 deletions.
5 changes: 2 additions & 3 deletions api_testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"sync/atomic"
"time"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"github.com/programmfabrik/apitest/internal/httpproxy"
Expand Down Expand Up @@ -105,7 +104,7 @@ func NewTestSuite(config TestToolConfig, manifestPath string, manifestDir string
if httpServerReplaceHost != "" {
_, err = url.Parse("//" + httpServerReplaceHost)
if err != nil {
return nil, errors.Wrap(err, "set http_server_host failed (command argument)")
return nil, fmt.Errorf("set http_server_host failed (command argument): %w", err)
}
}
if suitePreload.HttpServer != nil {
Expand All @@ -116,7 +115,7 @@ func NewTestSuite(config TestToolConfig, manifestPath string, manifestDir string
// We need to append it as the golang URL parser is not smart enough to differenciate between hostname and protocol
_, err = url.Parse("//" + preloadHTTPAddrStr)
if err != nil {
return nil, errors.Wrap(err, "set http_server_host failed (manifesr addr)")
return nil, fmt.Errorf("set http_server_host failed (manifesr addr): %w", err)
}
}
suitePreload.HTTPServerHost = httpServerReplaceHost
Expand Down
12 changes: 5 additions & 7 deletions internal/httpproxy/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"net/url"
"strconv"

"github.com/pkg/errors"

"github.com/programmfabrik/apitest/internal/handlerutil"
)

Expand Down Expand Up @@ -65,7 +63,7 @@ func (st *store) write(w http.ResponseWriter, r *http.Request) {
if r.Body != nil {
reqData.Body, err = io.ReadAll(r.Body)
if err != nil {
handlerutil.RespondWithErr(w, http.StatusInternalServerError, errors.Errorf("Could not read request body: %s", err))
handlerutil.RespondWithErr(w, http.StatusInternalServerError, fmt.Errorf("Could not read request body: %w", err))
return
}
}
Expand All @@ -76,7 +74,7 @@ func (st *store) write(w http.ResponseWriter, r *http.Request) {
Offset int `json:"offset"`
}{offset})
if err != nil {
handlerutil.RespondWithErr(w, http.StatusInternalServerError, errors.Errorf("Could not encode response: %s", err))
handlerutil.RespondWithErr(w, http.StatusInternalServerError, fmt.Errorf("Could not encode response: %w", err))
}
}

Expand All @@ -94,14 +92,14 @@ func (st *store) read(w http.ResponseWriter, r *http.Request) {
if offsetStr != "" {
offset, err = strconv.Atoi(offsetStr)
if err != nil {
handlerutil.RespondWithErr(w, http.StatusBadRequest, errors.Errorf("Invalid offset %s", offsetStr))
handlerutil.RespondWithErr(w, http.StatusBadRequest, fmt.Errorf("Invalid offset %s", offsetStr))
return
}
}

count := len(st.Data)
if offset >= count {
handlerutil.RespondWithErr(w, http.StatusBadRequest, errors.Errorf("Offset (%d) is higher than count (%d)", offset, count))
handlerutil.RespondWithErr(w, http.StatusBadRequest, fmt.Errorf("Offset (%d) is higher than count (%d)", offset, count))
return
}

Expand All @@ -126,6 +124,6 @@ func (st *store) read(w http.ResponseWriter, r *http.Request) {

_, err = w.Write(req.Body)
if err != nil {
handlerutil.RespondWithErr(w, http.StatusInternalServerError, errors.Errorf("Could not encode response: %s", err))
handlerutil.RespondWithErr(w, http.StatusInternalServerError, fmt.Errorf("Could not encode response: %w", err))
}
}
4 changes: 1 addition & 3 deletions pkg/lib/api/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"strings"
"time"

"github.com/pkg/errors"

"github.com/moul/http2curl"
"github.com/programmfabrik/apitest/pkg/lib/datastore"
"github.com/programmfabrik/apitest/pkg/lib/util"
Expand Down Expand Up @@ -84,7 +82,7 @@ func (request Request) buildHttpRequest() (req *http.Request, err error) {

reqUrl, err := url.Parse(requestUrl)
if err != nil {
return nil, errors.Wrapf(err, "Unable to buildHttpRequest with URL %q", requestUrl)
return nil, fmt.Errorf("Unable to buildHttpRequest with URL %q: %w", requestUrl, err)
}

// Note that buildPolicy may return a file handle that needs to be
Expand Down
16 changes: 7 additions & 9 deletions pkg/lib/api/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"time"
"unicode/utf8"

"github.com/pkg/errors"

"github.com/programmfabrik/apitest/pkg/lib/csv"
"github.com/programmfabrik/apitest/pkg/lib/util"
"github.com/programmfabrik/golib"
Expand Down Expand Up @@ -158,7 +156,7 @@ func (response Response) ServerResponseToGenericJSON(responseFormat ResponseForm
if responseFormat.PreProcess != nil {
resp, err = responseFormat.PreProcess.RunPreProcess(response)
if err != nil {
return res, errors.Wrap(err, "Could not pre process response")
return res, fmt.Errorf("Could not pre process response: %w", err)
}
} else {
resp = response
Expand All @@ -168,17 +166,17 @@ func (response Response) ServerResponseToGenericJSON(responseFormat ResponseForm
case "xml", "xml2":
bodyData, err = util.Xml2Json(resp.Body, responseFormat.Type)
if err != nil {
return res, errors.Wrap(err, "Could not marshal xml to json")
return res, fmt.Errorf("Could not marshal xml to json: %w", err)
}
case "html":
bodyData, err = util.Html2Json(resp.Body)
if err != nil {
return res, errors.Wrap(err, "Could not marshal html to json")
return res, fmt.Errorf("Could not marshal html to json: %w", err)
}
case "xhtml":
bodyData, err = util.Xhtml2Json(resp.Body)
if err != nil {
return res, errors.Wrap(err, "Could not marshal xhtml to json")
return res, fmt.Errorf("Could not marshal xhtml to json: %w", err)
}
case "csv":
runeComma := ','
Expand All @@ -188,12 +186,12 @@ func (response Response) ServerResponseToGenericJSON(responseFormat ResponseForm

csvData, err := csv.GenericCSVToMap(resp.Body, runeComma)
if err != nil {
return res, errors.Wrap(err, "Could not parse csv")
return res, fmt.Errorf("Could not parse csv: %w", err)
}

bodyData, err = json.Marshal(csvData)
if err != nil {
return res, errors.Wrap(err, "Could not marshal csv to json")
return res, fmt.Errorf("Could not marshal csv to json: %w", err)
}
case "binary":
// We have another file format (binary). We thereby take the md5 Hash of the body and compare that one
Expand All @@ -204,7 +202,7 @@ func (response Response) ServerResponseToGenericJSON(responseFormat ResponseForm
}
bodyData, err = json.Marshal(JsonObject)
if err != nil {
return res, errors.Wrap(err, "Could not marshal body with md5sum to json")
return res, fmt.Errorf("Could not marshal body with md5sum to json: %w", err)
}
case "":
// no specific format, we assume a json, and thereby try to unmarshal it into our body
Expand Down
5 changes: 2 additions & 3 deletions pkg/lib/compare/comparison_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strconv"
"strings"

"github.com/pkg/errors"
"github.com/programmfabrik/apitest/pkg/lib/util"
"github.com/programmfabrik/golib"
)
Expand Down Expand Up @@ -361,11 +360,11 @@ func arrayComparison(left, right util.JsonArray, currControl ComparisonContext,

leftJson, err := golib.JsonBytesIndent(left, "", " ")
if err != nil {
return CompareResult{}, errors.Wrap(err, "Could not marshal expected array")
return CompareResult{}, fmt.Errorf("Could not marshal expected array: %w", err)
}
rightJson, err := golib.JsonBytesIndent(right, "", " ")
if err != nil {
return CompareResult{}, errors.Wrap(err, "Could not marshal actual array")
return CompareResult{}, fmt.Errorf("Could not marshal actual array: %w", err)
}

res.Failures = append(res.Failures, CompareFailure{"", fmt.Sprintf("[arrayComparison] length of expected response (%d) > length of actual response (%d)\nExpected response:\n%s\nActual response:\n%s\n", len(left), len(right), string(leftJson), string(rightJson))})
Expand Down
4 changes: 1 addition & 3 deletions pkg/lib/csv/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"io"
"strconv"
"strings"

"github.com/pkg/errors"
)

// Get information
Expand All @@ -25,7 +23,7 @@ func CSVToMap(inputCSV []byte, comma rune) ([]map[string]any, error) {

records, err := renderCSV(bytes.NewReader(inputCSV), comma)
if err != nil {
return nil, errors.Wrap(err, "CSVToMap.renderCSV")
return nil, fmt.Errorf("CSVToMap.renderCSV: %w", err)
}

records = removeEmptyRowsAndComments(records)
Expand Down
10 changes: 5 additions & 5 deletions pkg/lib/template/template_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func pivotRows(key, typ string, rows []map[string]any) (sheet []map[string]any,
case "string", "int64", "float64", "number", "json":
// supported
default:
return nil, errors.Errorf("type %q not supported", sheetType)
return nil, fmt.Errorf("type %q not supported", sheetType)
}

for kI, vI := range row {
Expand Down Expand Up @@ -325,13 +325,13 @@ func divide(b, a any) (any, error) {
func fileReadInternal(pathOrURL, rootDir string) ([]byte, error) {
file, err := util.OpenFileOrUrl(pathOrURL, rootDir)
if err != nil {
return nil, errors.Wrapf(err, "fileReadInternal: %q", pathOrURL)
return nil, fmt.Errorf("fileReadInternal: %q: %w", pathOrURL, err)
}
defer file.Close()

data, err := io.ReadAll(file)
if err != nil {
return nil, errors.Wrapf(err, "fileReadInternal: %q", pathOrURL)
return nil, fmt.Errorf("fileReadInternal: %q: %w", pathOrURL, err)
}
return data, nil
}
Expand All @@ -350,7 +350,7 @@ func loadFileAndRender(rootDir string, loader *Loader) any {
}
data, err = loader.Render(data, filepath.Dir(filepath.Join(rootDir, path)), tmplParams)
if err != nil {
return "", errors.Wrapf(err, "Render error in file %q", path)
return "", fmt.Errorf("Render error in file %q: %w", path, err)
}
return string(data), nil
}
Expand Down Expand Up @@ -387,7 +387,7 @@ func loadFileCSV(rootDir string) any {
}
data, err := csv.CSVToMap(fileBytes, delimiter)
if err != nil {
return data, errors.Wrapf(err, "CSV map error in file %q", path)
return data, fmt.Errorf("CSV map error in file %q: %w", path, err)
}
return data, err
}
Expand Down
23 changes: 11 additions & 12 deletions pkg/lib/template/template_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"text/template"

"github.com/Masterminds/sprig/v3"
"github.com/pkg/errors"
"github.com/programmfabrik/apitest/pkg/lib/datastore"
"github.com/programmfabrik/golib"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -192,7 +191,7 @@ func (loader *Loader) Render(

bytes, err := util.Xml2Json(fileBytes, "xml2")
if err != nil {
return "", errors.Wrap(err, "Could not marshal xml to json")
return "", fmt.Errorf("Could not marshal xml to json: %w", err)
}

return string(bytes), nil
Expand All @@ -205,7 +204,7 @@ func (loader *Loader) Render(

bytes, err := util.Xhtml2Json(fileBytes)
if err != nil {
return "", errors.Wrap(err, "Could not marshal xhtml to json")
return "", fmt.Errorf("Could not marshal xhtml to json: %w", err)
}

return string(bytes), nil
Expand All @@ -218,7 +217,7 @@ func (loader *Loader) Render(

bytes, err := util.Html2Json(fileBytes)
if err != nil {
return "", errors.Wrap(err, "Could not marshal html to json")
return "", fmt.Errorf("Could not marshal html to json: %w", err)
}

return string(bytes), nil
Expand Down Expand Up @@ -373,7 +372,7 @@ func (loader *Loader) Render(
// println("client", client, login, password)
oAuthClient, ok := loader.OAuthClient[client]
if !ok {
return nil, errors.Errorf("OAuth client %q not configured", client)
return nil, fmt.Errorf("OAuth client %q not configured", client)
}

return oAuthClient.GetPasswordCredentialsAuthToken(login, password)
Expand All @@ -382,39 +381,39 @@ func (loader *Loader) Render(
"oauth2_client_token": func(client string) (tok *oauth2.Token, err error) {
oAuthClient, ok := loader.OAuthClient[client]
if !ok {
return nil, errors.Errorf("OAuth client %q not configured", client)
return nil, fmt.Errorf("OAuth client %q not configured", client)
}

return oAuthClient.GetClientCredentialsAuthToken()
},
"oauth2_code_token": func(client string, params ...string) (tok *oauth2.Token, err error) {
oAuthClient, ok := loader.OAuthClient[client]
if !ok {
return nil, errors.Errorf("OAuth client %q not configured", client)
return nil, fmt.Errorf("OAuth client %q not configured", client)
}

return oAuthClient.GetCodeAuthToken(params...)
},
"oauth2_implicit_token": func(client string, params ...string) (tok *oauth2.Token, err error) {
oAuthClient, ok := loader.OAuthClient[client]
if !ok {
return nil, errors.Errorf("OAuth client %q not configured", client)
return nil, fmt.Errorf("OAuth client %q not configured", client)
}

return oAuthClient.GetAuthToken(params...)
},
"oauth2_client": func(client string) (c *util.OAuthClientConfig, err error) {
oAuthClient, ok := loader.OAuthClient[client]
if !ok {
return nil, errors.Errorf("OAuth client %s not configured", client)
return nil, fmt.Errorf("OAuth client %s not configured", client)
}

return &oAuthClient, nil
},
"oauth2_basic_auth": func(client string) (string, error) {
oAuthClient, ok := loader.OAuthClient[client]
if !ok {
return "", errors.Errorf("OAuth client %s not configured", client)
return "", fmt.Errorf("OAuth client %s not configured", client)
}

return "Basic " + base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", oAuthClient.Client, oAuthClient.Secret))), nil
Expand Down Expand Up @@ -449,10 +448,10 @@ func (loader *Loader) Render(
w = "v0.0.0"
}
if !semver.IsValid(v) {
return 0, errors.Errorf("version string %s is invalid", v)
return 0, fmt.Errorf("version string %s is invalid", v)
}
if !semver.IsValid(w) {
return 0, errors.Errorf("version string %s is invalid", w)
return 0, fmt.Errorf("version string %s is invalid", w)
}
return semver.Compare(v, w), nil
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/lib/util/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package util

import (
"context"
"fmt"
"net/http"
"net/url"
"time"

"log"

"github.com/pkg/errors"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)
Expand Down Expand Up @@ -122,7 +122,7 @@ func (c OAuthClientConfig) getRedirectURL(params ...string) (*url.URL, error) {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, errors.Errorf("No proper status after redirect returned: %s (%d)", res.Status, res.StatusCode)
return nil, fmt.Errorf("No proper status after redirect returned: %s (%d)", res.Status, res.StatusCode)
}
return res.Request.URL, nil
}
Expand Down
Loading

0 comments on commit 27423dc

Please sign in to comment.