From 27423dc3d5fa37fc3712121f16d27ccbc71c65c2 Mon Sep 17 00:00:00 2001 From: Philipp Hempel Date: Mon, 28 Oct 2024 12:40:39 +0100 Subject: [PATCH] replaced errors.Wrap and other obsolete functions --- api_testsuite.go | 5 ++--- internal/httpproxy/store.go | 12 +++++------- pkg/lib/api/request.go | 4 +--- pkg/lib/api/response.go | 16 +++++++--------- pkg/lib/compare/comparison_functions.go | 5 ++--- pkg/lib/csv/csv.go | 4 +--- pkg/lib/template/template_funcs.go | 10 +++++----- pkg/lib/template/template_loader.go | 23 +++++++++++------------ pkg/lib/util/oauth.go | 4 ++-- pkg/lib/util/util.go | 15 +++++++-------- 10 files changed, 43 insertions(+), 55 deletions(-) diff --git a/api_testsuite.go b/api_testsuite.go index b05bfbe9..8c6ebd3c 100644 --- a/api_testsuite.go +++ b/api_testsuite.go @@ -14,7 +14,6 @@ import ( "sync/atomic" "time" - "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/programmfabrik/apitest/internal/httpproxy" @@ -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 { @@ -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 diff --git a/internal/httpproxy/store.go b/internal/httpproxy/store.go index af50c676..828cc061 100644 --- a/internal/httpproxy/store.go +++ b/internal/httpproxy/store.go @@ -8,8 +8,6 @@ import ( "net/url" "strconv" - "github.com/pkg/errors" - "github.com/programmfabrik/apitest/internal/handlerutil" ) @@ -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 } } @@ -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)) } } @@ -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 } @@ -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)) } } diff --git a/pkg/lib/api/request.go b/pkg/lib/api/request.go index 2e1bd56c..0ef79383 100755 --- a/pkg/lib/api/request.go +++ b/pkg/lib/api/request.go @@ -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" @@ -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 diff --git a/pkg/lib/api/response.go b/pkg/lib/api/response.go index 92f75504..1aa79f12 100755 --- a/pkg/lib/api/response.go +++ b/pkg/lib/api/response.go @@ -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" @@ -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 @@ -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 := ',' @@ -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 @@ -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 diff --git a/pkg/lib/compare/comparison_functions.go b/pkg/lib/compare/comparison_functions.go index 45c5a215..509a3a5a 100755 --- a/pkg/lib/compare/comparison_functions.go +++ b/pkg/lib/compare/comparison_functions.go @@ -8,7 +8,6 @@ import ( "strconv" "strings" - "github.com/pkg/errors" "github.com/programmfabrik/apitest/pkg/lib/util" "github.com/programmfabrik/golib" ) @@ -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))}) diff --git a/pkg/lib/csv/csv.go b/pkg/lib/csv/csv.go index a3a723a8..663b2ba4 100644 --- a/pkg/lib/csv/csv.go +++ b/pkg/lib/csv/csv.go @@ -8,8 +8,6 @@ import ( "io" "strconv" "strings" - - "github.com/pkg/errors" ) // Get information @@ -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) diff --git a/pkg/lib/template/template_funcs.go b/pkg/lib/template/template_funcs.go index 80b676f7..62f4f4b6 100644 --- a/pkg/lib/template/template_funcs.go +++ b/pkg/lib/template/template_funcs.go @@ -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 { @@ -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 } @@ -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 } @@ -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 } diff --git a/pkg/lib/template/template_loader.go b/pkg/lib/template/template_loader.go index 160a07a6..4075314d 100644 --- a/pkg/lib/template/template_loader.go +++ b/pkg/lib/template/template_loader.go @@ -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" @@ -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 @@ -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 @@ -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 @@ -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) @@ -382,7 +381,7 @@ 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() @@ -390,7 +389,7 @@ func (loader *Loader) Render( "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...) @@ -398,7 +397,7 @@ func (loader *Loader) Render( "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...) @@ -406,7 +405,7 @@ func (loader *Loader) Render( "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 @@ -414,7 +413,7 @@ func (loader *Loader) Render( "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 @@ -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 }, diff --git a/pkg/lib/util/oauth.go b/pkg/lib/util/oauth.go index b38896d4..e3c3aec0 100644 --- a/pkg/lib/util/oauth.go +++ b/pkg/lib/util/oauth.go @@ -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" ) @@ -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 } diff --git a/pkg/lib/util/util.go b/pkg/lib/util/util.go index df0fc4f3..d5c87f69 100644 --- a/pkg/lib/util/util.go +++ b/pkg/lib/util/util.go @@ -10,7 +10,6 @@ import ( "github.com/PuerkitoBio/goquery" "github.com/clbanning/mxj" - "github.com/pkg/errors" "github.com/programmfabrik/golib" "golang.org/x/net/html" ) @@ -67,16 +66,16 @@ func Xml2Json(rawXml []byte, format string) ([]byte, error) { case "xml2": mv, err = mxj.NewMapXml(replacedXML) default: - return []byte{}, errors.Errorf("Unknown format %s", format) + return []byte{}, fmt.Errorf("Unknown format %s", format) } if err != nil { - return []byte{}, errors.Wrap(err, "Could not parse xml") + return []byte{}, fmt.Errorf("Could not parse xml: %w", err) } jsonStr, err := mv.JsonIndent("", " ") if err != nil { - return []byte{}, errors.Wrap(err, "Could not convert to json") + return []byte{}, fmt.Errorf("Could not convert to json: %w", err) } return jsonStr, nil } @@ -90,12 +89,12 @@ func Xhtml2Json(rawXhtml []byte) ([]byte, error) { mv, err = mxj.NewMapXml(rawXhtml) if err != nil { - return []byte{}, errors.Wrap(err, "Could not parse xhtml") + return []byte{}, fmt.Errorf("Could not parse xhtml: %w", err) } jsonStr, err := mv.JsonIndent("", " ") if err != nil { - return []byte{}, errors.Wrap(err, "Could not convert to json") + return []byte{}, fmt.Errorf("Could not convert to json: %w", err) } return jsonStr, nil } @@ -109,7 +108,7 @@ func Html2Json(rawHtml []byte) ([]byte, error) { htmlDoc, err = goquery.NewDocumentFromReader(bytes.NewReader(rawHtml)) if err != nil { - return []byte{}, errors.Wrap(err, "Could not parse html") + return []byte{}, fmt.Errorf("Could not parse html: %w", err) } htmlData := map[string]any{} @@ -125,7 +124,7 @@ func Html2Json(rawHtml []byte) ([]byte, error) { jsonStr, err := golib.JsonBytesIndent(htmlData, "", " ") if err != nil { - return []byte{}, errors.Wrap(err, "Could not convert html to json") + return []byte{}, fmt.Errorf("Could not convert html to json: %w", err) } return jsonStr, nil