mirror of
https://github.com/portainer/portainer.git
synced 2025-07-19 05:19:39 +02:00
chore(code): clean up the code EE-7251 (#11948)
Some checks are pending
ci / build_images (map[arch:ppc64le platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:s390x platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:amd64 platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:amd64 platform:windows version:1809]) (push) Waiting to run
ci / build_images (map[arch:amd64 platform:windows version:ltsc2022]) (push) Waiting to run
ci / build_images (map[arch:arm platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:arm64 platform:linux version:]) (push) Waiting to run
ci / build_manifests (push) Blocked by required conditions
/ triage (push) Waiting to run
Lint / Run linters (push) Waiting to run
Test / test-server (map[arch:amd64 platform:windows version:ltsc2022]) (push) Waiting to run
Test / test-server (map[arch:arm64 platform:linux]) (push) Waiting to run
Test / test-client (push) Waiting to run
Test / test-server (map[arch:amd64 platform:linux]) (push) Waiting to run
Test / test-server (map[arch:amd64 platform:windows version:1809]) (push) Waiting to run
Some checks are pending
ci / build_images (map[arch:ppc64le platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:s390x platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:amd64 platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:amd64 platform:windows version:1809]) (push) Waiting to run
ci / build_images (map[arch:amd64 platform:windows version:ltsc2022]) (push) Waiting to run
ci / build_images (map[arch:arm platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:arm64 platform:linux version:]) (push) Waiting to run
ci / build_manifests (push) Blocked by required conditions
/ triage (push) Waiting to run
Lint / Run linters (push) Waiting to run
Test / test-server (map[arch:amd64 platform:windows version:ltsc2022]) (push) Waiting to run
Test / test-server (map[arch:arm64 platform:linux]) (push) Waiting to run
Test / test-client (push) Waiting to run
Test / test-server (map[arch:amd64 platform:linux]) (push) Waiting to run
Test / test-server (map[arch:amd64 platform:windows version:1809]) (push) Waiting to run
This commit is contained in:
parent
be9d3285e1
commit
bfa27d9103
49 changed files with 338 additions and 368 deletions
|
@ -1,4 +1,4 @@
|
|||
// Package error provides error/logging functions that can be used in conjuction with http.Handler.
|
||||
// Package error provides error/logging functions that can be used in conjunction with http.Handler.
|
||||
package error
|
||||
|
||||
import (
|
||||
|
@ -21,8 +21,7 @@ type (
|
|||
)
|
||||
|
||||
func (handler LoggerHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||
err := handler(rw, r)
|
||||
if err != nil {
|
||||
if err := handler(rw, r); err != nil {
|
||||
writeErrorResponse(rw, err)
|
||||
}
|
||||
}
|
||||
|
@ -34,6 +33,7 @@ func capitalize(s string) string {
|
|||
|
||||
// Capitalize the first letter of the word or sentence
|
||||
firstLetter := unicode.ToUpper(rune(s[0]))
|
||||
|
||||
return string(firstLetter) + s[1:]
|
||||
}
|
||||
|
||||
|
@ -42,7 +42,12 @@ func writeErrorResponse(rw http.ResponseWriter, err *HandlerError) {
|
|||
err.Err = errors.New(capitalize(err.Message))
|
||||
}
|
||||
|
||||
log.Debug().CallerSkipFrame(2).Err(err.Err).Int("status_code", err.StatusCode).Str("msg", err.Message).Msg("HTTP error")
|
||||
log.Debug().
|
||||
CallerSkipFrame(2).
|
||||
Err(err.Err).
|
||||
Int("status_code", err.StatusCode).
|
||||
Str("msg", err.Message).
|
||||
Msg("HTTP error")
|
||||
|
||||
rw.Header().Set("Content-Type", "application/json")
|
||||
rw.WriteHeader(err.StatusCode)
|
||||
|
|
|
@ -22,7 +22,6 @@ func (p *requestPayload) Validate(r *http.Request) error {
|
|||
}
|
||||
|
||||
func Test_GetPayload(t *testing.T) {
|
||||
|
||||
payload := requestPayload{
|
||||
FirstName: "John",
|
||||
LastName: "Doe",
|
||||
|
@ -34,6 +33,7 @@ func Test_GetPayload(t *testing.T) {
|
|||
}
|
||||
|
||||
r := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(payloadJSON))
|
||||
|
||||
newPayload, err := request.GetPayload[requestPayload](r)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
|
@ -5,7 +5,7 @@ package request
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
|
@ -33,10 +33,11 @@ func RetrieveMultiPartFormFile(request *http.Request, requestParameter string) (
|
|||
}
|
||||
defer file.Close()
|
||||
|
||||
fileContent, err := ioutil.ReadAll(file)
|
||||
fileContent, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
return fileContent, headers.Filename, nil
|
||||
}
|
||||
|
||||
|
@ -46,10 +47,10 @@ func RetrieveMultiPartFormJSONValue(request *http.Request, name string, target i
|
|||
value, err := RetrieveMultiPartFormValue(request, name, optional)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if value == "" {
|
||||
} else if value == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
return json.Unmarshal([]byte(value), target)
|
||||
}
|
||||
|
||||
|
@ -60,6 +61,7 @@ func RetrieveMultiPartFormValue(request *http.Request, name string, optional boo
|
|||
if value == "" && !optional {
|
||||
return "", errors.New(ErrMissingFormDataValue)
|
||||
}
|
||||
|
||||
return value, nil
|
||||
}
|
||||
|
||||
|
@ -70,6 +72,7 @@ func RetrieveNumericMultiPartFormValue(request *http.Request, name string, optio
|
|||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return strconv.Atoi(value)
|
||||
}
|
||||
|
||||
|
@ -80,6 +83,7 @@ func RetrieveBooleanMultiPartFormValue(request *http.Request, name string, optio
|
|||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return value == "true", nil
|
||||
}
|
||||
|
||||
|
@ -89,10 +93,12 @@ func RetrieveRouteVariableValue(request *http.Request, name string) (string, err
|
|||
if routeVariables == nil {
|
||||
return "", errors.New(ErrInvalidRequestURL)
|
||||
}
|
||||
|
||||
routeVar := routeVariables[name]
|
||||
if routeVar == "" {
|
||||
return "", errors.New(ErrInvalidRequestURL)
|
||||
}
|
||||
|
||||
return routeVar, nil
|
||||
}
|
||||
|
||||
|
@ -102,6 +108,7 @@ func RetrieveNumericRouteVariableValue(request *http.Request, name string) (int,
|
|||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return strconv.Atoi(routeVar)
|
||||
}
|
||||
|
||||
|
@ -112,6 +119,7 @@ func RetrieveQueryParameter(request *http.Request, name string, optional bool) (
|
|||
if queryParameter == "" && !optional {
|
||||
return "", errors.New(ErrMissingQueryParameter)
|
||||
}
|
||||
|
||||
return queryParameter, nil
|
||||
}
|
||||
|
||||
|
@ -121,10 +129,10 @@ func RetrieveNumericQueryParameter(request *http.Request, name string, optional
|
|||
queryParameter, err := RetrieveQueryParameter(request, name, optional)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if queryParameter == "" && optional {
|
||||
} else if queryParameter == "" && optional {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
return strconv.Atoi(queryParameter)
|
||||
}
|
||||
|
||||
|
@ -135,6 +143,7 @@ func RetrieveBooleanQueryParameter(request *http.Request, name string, optional
|
|||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return queryParameter == "true", nil
|
||||
}
|
||||
|
||||
|
@ -144,9 +153,9 @@ func RetrieveJSONQueryParameter(request *http.Request, name string, target inter
|
|||
queryParameter, err := RetrieveQueryParameter(request, name, optional)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if queryParameter == "" {
|
||||
} else if queryParameter == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
return json.Unmarshal([]byte(queryParameter), target)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue