From fe7f105daeef1bd9c0197c85d92fc19afa67bfe8 Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Fri, 26 Aug 2022 13:06:59 +0430 Subject: [PATCH 01/12] fix unhandled errors --- app_test.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app_test.go b/app_test.go index b20e0c455c..fcb623bdfb 100644 --- a/app_test.go +++ b/app_test.go @@ -499,17 +499,20 @@ func Test_App_Order(t *testing.T) { app := New() app.Get("/test", func(c *Ctx) error { - c.Write([]byte("1")) + _, err := c.Write([]byte("1")) + utils.AssertEqual(t, nil, err) return c.Next() }) app.All("/test", func(c *Ctx) error { - c.Write([]byte("2")) + _, err := c.Write([]byte("2")) + utils.AssertEqual(t, nil, err) return c.Next() }) app.Use(func(c *Ctx) error { - c.Write([]byte("3")) + _, err := c.Write([]byte("3")) + utils.AssertEqual(t, nil, err) return nil }) From 7f1028d7fc4b8f0d4b27ce77cc29523dcdc838ce Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Fri, 26 Aug 2022 15:53:00 +0430 Subject: [PATCH 02/12] fix unhandled error in cache package test --- middleware/cache/cache_test.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/middleware/cache/cache_test.go b/middleware/cache/cache_test.go index 4c7e155a36..f991d24b0a 100644 --- a/middleware/cache/cache_test.go +++ b/middleware/cache/cache_test.go @@ -5,6 +5,7 @@ package cache import ( "bytes" "fmt" + "io" "io/ioutil" "math" "net/http" @@ -126,7 +127,10 @@ func Test_Cache_WithSeveralRequests(t *testing.T) { rsp, err := app.Test(httptest.NewRequest(http.MethodGet, fmt.Sprintf("/%d", id), nil)) utils.AssertEqual(t, nil, err) - defer rsp.Body.Close() + defer func(Body io.ReadCloser) { + err := Body.Close() + utils.AssertEqual(t, nil, err) + }(rsp.Body) idFromServ, err := ioutil.ReadAll(rsp.Body) utils.AssertEqual(t, nil, err) From bf387fae1698ed6536620c109d96eb6c184e2c65 Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Fri, 26 Aug 2022 16:05:19 +0430 Subject: [PATCH 03/12] omit variable type --- internal/go-ole/connect.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/go-ole/connect.go b/internal/go-ole/connect.go index b2ac2ec67a..b910f0e329 100644 --- a/internal/go-ole/connect.go +++ b/internal/go-ole/connect.go @@ -44,8 +44,8 @@ func (c *Connection) Release() { // Load COM object from list of programIDs or strings. func (c *Connection) Load(names ...string) (errors []error) { - var tempErrors []error = make([]error, len(names)) - var numErrors int = 0 + var tempErrors = make([]error, len(names)) + var numErrors = 0 for _, name := range names { err := c.Create(name) if err != nil { From d09213bfa26bbc331619d755d3ffd3c43c560070 Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Fri, 26 Aug 2022 16:07:15 +0430 Subject: [PATCH 04/12] omit variable type --- middleware/session/session_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/middleware/session/session_test.go b/middleware/session/session_test.go index 4afef2458d..19bf121fa0 100644 --- a/middleware/session/session_test.go +++ b/middleware/session/session_test.go @@ -127,9 +127,9 @@ func Test_Session_Types(t *testing.T) { Name: "John", } // set value - var vbool bool = true - var vstring string = "str" - var vint int = 13 + var vbool = true + var vstring = "str" + var vint = 13 var vint8 int8 = 13 var vint16 int16 = 13 var vint32 int32 = 13 From 14b6646bbf66c7d1726975f7d8b5b32cb6440794 Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Fri, 26 Aug 2022 16:10:52 +0430 Subject: [PATCH 05/12] rename variable because collide with the imported package name --- internal/schema/decoder.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/schema/decoder.go b/internal/schema/decoder.go index 9d44822202..b63c45e1dc 100644 --- a/internal/schema/decoder.go +++ b/internal/schema/decoder.go @@ -74,19 +74,19 @@ func (d *Decoder) Decode(dst interface{}, src map[string][]string) error { } v = v.Elem() t := v.Type() - errors := MultiError{} + multiError := MultiError{} for path, values := range src { if parts, err := d.cache.parsePath(path, t); err == nil { if err = d.decode(v, path, parts, values); err != nil { - errors[path] = err + multiError[path] = err } } else if !d.ignoreUnknownKeys { - errors[path] = UnknownKeyError{Key: path} + multiError[path] = UnknownKeyError{Key: path} } } - errors.merge(d.checkRequired(t, src)) - if len(errors) > 0 { - return errors + multiError.merge(d.checkRequired(t, src)) + if len(multiError) > 0 { + return multiError } return nil } From 1c45f7e821298a53858c0725f57594bb0b1c2c08 Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Fri, 26 Aug 2022 16:27:12 +0430 Subject: [PATCH 06/12] handle file error on closing --- internal/gopsutil/common/common.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/internal/gopsutil/common/common.go b/internal/gopsutil/common/common.go index dc7a927b26..5c7adace0e 100644 --- a/internal/gopsutil/common/common.go +++ b/internal/gopsutil/common/common.go @@ -13,6 +13,7 @@ import ( "errors" "fmt" "io/ioutil" + "log" "net/url" "os" "os/exec" @@ -122,7 +123,12 @@ func ReadLinesOffsetN(filename string, offset uint, n int) ([]string, error) { if err != nil { return []string{""}, err } - defer f.Close() + defer func(f *os.File) { + err := f.Close() + if err != nil { + log.Fatalln(err) + } + }(f) var ret []string @@ -204,7 +210,12 @@ func ReadInts(filename string) ([]int64, error) { if err != nil { return []int64{}, err } - defer f.Close() + defer func(f *os.File) { + err := f.Close() + if err != nil { + log.Fatalln(err) + } + }(f) var ret []int64 From 89bf878c534d1f67094ee0aff1952f09a758589c Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Sun, 28 Aug 2022 10:24:54 +0430 Subject: [PATCH 07/12] fix unhandled in common_linux.go --- internal/gopsutil/common/common_linux.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/internal/gopsutil/common/common_linux.go b/internal/gopsutil/common/common_linux.go index 9234f767d7..a89c8e28ac 100644 --- a/internal/gopsutil/common/common_linux.go +++ b/internal/gopsutil/common/common_linux.go @@ -6,6 +6,7 @@ package common import ( "context" "fmt" + "log" "os" "os/exec" "path/filepath" @@ -37,7 +38,12 @@ func NumProcs() (uint64, error) { if err != nil { return 0, err } - defer f.Close() + defer func(f *os.File) { + err := f.Close() + if err != nil { + log.Fatalln(err) + } + }(f) list, err := f.Readdirnames(-1) if err != nil { From 08a925670d778490b24f43ee2d8f2c3085ac42c2 Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Mon, 29 Aug 2022 15:25:25 +0430 Subject: [PATCH 08/12] fix unhandled errors in helpers_test.go --- helpers_test.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/helpers_test.go b/helpers_test.go index 7b45540de7..f1bfbb2ec8 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -22,7 +22,8 @@ func Test_Utils_ETag(t *testing.T) { t.Run("Not Status OK", func(t *testing.T) { c := app.AcquireCtx(&fasthttp.RequestCtx{}) defer app.ReleaseCtx(c) - c.SendString("Hello, World!") + err := c.SendString("Hello, World!") + utils.AssertEqual(t, nil, err) c.Status(201) setETag(c, false) utils.AssertEqual(t, "", string(c.Response().Header.Peek(HeaderETag))) @@ -38,7 +39,8 @@ func Test_Utils_ETag(t *testing.T) { t.Run("Has HeaderIfNoneMatch", func(t *testing.T) { c := app.AcquireCtx(&fasthttp.RequestCtx{}) defer app.ReleaseCtx(c) - c.SendString("Hello, World!") + err := c.SendString("Hello, World!") + utils.AssertEqual(t, nil, err) c.Request().Header.Set(HeaderIfNoneMatch, `"13-1831710635"`) setETag(c, false) utils.AssertEqual(t, 304, c.Response().StatusCode()) @@ -49,7 +51,8 @@ func Test_Utils_ETag(t *testing.T) { t.Run("No HeaderIfNoneMatch", func(t *testing.T) { c := app.AcquireCtx(&fasthttp.RequestCtx{}) defer app.ReleaseCtx(c) - c.SendString("Hello, World!") + err := c.SendString("Hello, World!") + utils.AssertEqual(t, nil, err) setETag(c, false) utils.AssertEqual(t, `"13-1831710635"`, string(c.Response().Header.Peek(HeaderETag))) }) @@ -60,7 +63,8 @@ func Benchmark_Utils_ETag(b *testing.B) { app := New() c := app.AcquireCtx(&fasthttp.RequestCtx{}) defer app.ReleaseCtx(c) - c.SendString("Hello, World!") + err := c.SendString("Hello, World!") + utils.AssertEqual(b, nil, err) for n := 0; n < b.N; n++ { setETag(c, false) } @@ -73,7 +77,8 @@ func Test_Utils_ETag_Weak(t *testing.T) { t.Run("Set Weak", func(t *testing.T) { c := app.AcquireCtx(&fasthttp.RequestCtx{}) defer app.ReleaseCtx(c) - c.SendString("Hello, World!") + err := c.SendString("Hello, World!") + utils.AssertEqual(t, nil, err) setETag(c, true) utils.AssertEqual(t, `W/"13-1831710635"`, string(c.Response().Header.Peek(HeaderETag))) }) From 038315df1e3c9ca17349ebd1670ede564d43efd3 Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Tue, 30 Aug 2022 10:51:02 +0430 Subject: [PATCH 09/12] fix unhandled errors in listen_test.go --- listen_test.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/listen_test.go b/listen_test.go index 11d1908bd4..13f42e19d3 100644 --- a/listen_test.go +++ b/listen_test.go @@ -166,12 +166,18 @@ func captureOutput(f func()) string { go func() { var buf bytes.Buffer wg.Done() - io.Copy(&buf, reader) + _, err := io.Copy(&buf, reader) + if err != nil { + panic(err) + } out <- buf.String() }() wg.Wait() f() - writer.Close() + err = writer.Close() + if err != nil { + panic(err) + } return <-out } From e5b8dad14642a598f161df96a9fb7f31a36eea22 Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Tue, 30 Aug 2022 10:52:08 +0430 Subject: [PATCH 10/12] remove unused parameter in emptyHandler method --- listen_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/listen_test.go b/listen_test.go index 13f42e19d3..978e953a4d 100644 --- a/listen_test.go +++ b/listen_test.go @@ -250,6 +250,6 @@ func Test_App_print_Route_with_group(t *testing.T) { utils.AssertEqual(t, true, strings.Contains(printRoutesMessage, "/v1/test/fiber/*")) } -func emptyHandler(c *Ctx) error { +func emptyHandler(_ *Ctx) error { return nil } From 92e6920fff506a16981cea2ccb702cf6dd1f1924 Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Thu, 1 Sep 2022 16:58:19 +0430 Subject: [PATCH 11/12] refactor path.go --- path.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/path.go b/path.go index db2818dd00..ca41f45969 100644 --- a/path.go +++ b/path.go @@ -61,7 +61,7 @@ const ( paramConstraintDataSeparator byte = ',' // separator of datas of type constraint for a parameter ) -// parameter constraint types +// TypeConstraint parameter constraint types type TypeConstraint int16 type Constraint struct { @@ -260,7 +260,7 @@ func (routeParser *routeParser) analyseParameterPart(pattern string) (string, *r // Check has constraint var constraints []*Constraint - if hasConstraint := (parameterConstraintStart != -1 && parameterConstraintEnd != -1); hasConstraint { + if hasConstraint := parameterConstraintStart != -1 && parameterConstraintEnd != -1; hasConstraint { constraintString := pattern[parameterConstraintStart+1 : parameterConstraintEnd] userconstraints := splitNonEscaped(constraintString, string(parameterConstraintSeparatorChars)) constraints = make([]*Constraint, 0, len(userconstraints)) From 3086fd2f6c53fd1edd4def45d88afc6f7475b52d Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Thu, 1 Sep 2022 17:04:25 +0430 Subject: [PATCH 12/12] unhandled error in hooks test --- hooks_test.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/hooks_test.go b/hooks_test.go index 5a800920d2..5ed9d82145 100644 --- a/hooks_test.go +++ b/hooks_test.go @@ -42,7 +42,8 @@ func Test_Hook_OnName(t *testing.T) { defer bytebufferpool.Put(buf) app.Hooks().OnName(func(r Route) error { - buf.WriteString(r.Name) + _, err := buf.WriteString(r.Name) + utils.AssertEqual(t, nil, err) return nil }) @@ -84,8 +85,8 @@ func Test_Hook_OnGroup(t *testing.T) { defer bytebufferpool.Put(buf) app.Hooks().OnGroup(func(g Group) error { - buf.WriteString(g.Prefix) - + _, err := buf.WriteString(g.Prefix) + utils.AssertEqual(t, nil, err) return nil }) @@ -104,7 +105,8 @@ func Test_Hook_OnGroupName(t *testing.T) { defer bytebufferpool.Put(buf) app.Hooks().OnGroupName(func(g Group) error { - buf.WriteString(g.name) + _, err := buf.WriteString(g.name) + utils.AssertEqual(t, nil, err) return nil }) @@ -143,7 +145,8 @@ func Test_Hook_OnShutdown(t *testing.T) { defer bytebufferpool.Put(buf) app.Hooks().OnShutdown(func() error { - buf.WriteString("shutdowning") + _, err := buf.WriteString("shutdowning") + utils.AssertEqual(t, nil, err) return nil }) @@ -163,7 +166,8 @@ func Test_Hook_OnListen(t *testing.T) { defer bytebufferpool.Put(buf) app.Hooks().OnListen(func() error { - buf.WriteString("ready") + _, err := buf.WriteString("ready") + utils.AssertEqual(t, nil, err) return nil })