From e53e57e9417f1e525595a3045cc7f48c87897d14 Mon Sep 17 00:00:00 2001 From: Arvindh Date: Thu, 4 Jan 2024 16:49:04 +0530 Subject: [PATCH 1/4] add http error type Signed-off-by: Arvindh --- pkg/http/http.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pkg/http/http.go b/pkg/http/http.go index d83a4f6..ee8850b 100644 --- a/pkg/http/http.go +++ b/pkg/http/http.go @@ -85,8 +85,14 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { p.target.ServeHTTP(w, r) } -func encodeError(w http.ResponseWriter, statusCode int, err error) { - w.WriteHeader(statusCode) +func encodeError(w http.ResponseWriter, defStatusCode int, err error) { + hpe, ok := err.(HTTPProxyError) + switch ok { + case true: + w.WriteHeader(hpe.StatusCode()) + default: + w.WriteHeader(defStatusCode) + } w.Header().Set("Content-Type", contentType) if err := json.NewEncoder(w).Encode(err); err != nil { w.WriteHeader(http.StatusInternalServerError) From 84be3d4fdb67902ef003b2f9430af9998e9b2b86 Mon Sep 17 00:00:00 2001 From: Felix Gateru Date: Thu, 3 Oct 2024 11:50:27 +0300 Subject: [PATCH 2/4] refactor(errors.go): reorder code into new file Signed-off-by: Felix Gateru --- go.mod | 1 + go.sum | 10 ++++++++++ pkg/http/errors.go | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 pkg/http/errors.go diff --git a/go.mod b/go.mod index 002570f..d1adcb7 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/absmach/mgate go 1.22.7 require ( + github.com/absmach/magistrala v0.14.0 github.com/caarlos0/env/v11 v11.2.2 github.com/eclipse/paho.mqtt.golang v1.5.0 github.com/google/uuid v1.6.0 diff --git a/go.sum b/go.sum index cd5ebfd..31c24f4 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,9 @@ +github.com/absmach/magistrala v0.14.0 h1:ouIYOFwl0RMumBuXr4lGtfcukLGxFpzGAD4XDgrMcGg= +github.com/absmach/magistrala v0.14.0/go.mod h1:7hyZSXwYbXHRBQTBUZ0lgsPw3HlPw+PBQRe+xsr542M= github.com/caarlos0/env/v11 v11.2.2 h1:95fApNrUyueipoZN/EhA8mMxiNxrBwDa+oAZrMWl3Kg= github.com/caarlos0/env/v11 v11.2.2/go.mod h1:JBfcdeQiBoI3Zh1QRAWfe+tpiNTmDtcCj/hHHHMx0vc= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/eclipse/paho.mqtt.golang v1.5.0 h1:EH+bUVJNgttidWFkLLVKaQPGmkTUfQQqjOsyvMGvD6o= github.com/eclipse/paho.mqtt.golang v1.5.0/go.mod h1:du/2qNQVqJf/Sqs4MEL77kR8QTqANF7XU7Fk0aOTAgk= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= @@ -8,9 +12,15 @@ github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aN github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/http/errors.go b/pkg/http/errors.go new file mode 100644 index 0000000..7dd598a --- /dev/null +++ b/pkg/http/errors.go @@ -0,0 +1,47 @@ +// Copyright (c) Abstract Machines +// SPDX-License-Identifier: Apache-2.0 + +package http + +import mgerrors "github.com/absmach/magistrala/pkg/errors" + +type httpProxyError struct { + statusCode int + err mgerrors.Error +} + +type HTTPProxyError interface { + mgerrors.Error + StatusCode() int +} + +var _ HTTPProxyError = (*httpProxyError)(nil) + +func (hpe *httpProxyError) Error() string { + return hpe.err.Error() +} + +func (hpe *httpProxyError) Err() mgerrors.Error { + return hpe.err +} + +func (hpe *httpProxyError) Msg() string { + return hpe.err.Msg() +} + +func (hpe *httpProxyError) MarshalJSON() ([]byte, error) { + return hpe.err.MarshalJSON() +} + +func (hpe *httpProxyError) StatusCode() int { + return hpe.statusCode +} + +func NewHTTPProxyError(statusCode int, err error) HTTPProxyError { + var merr mgerrors.Error + var ok bool + if merr, ok = err.(mgerrors.Error); !ok { + merr = mgerrors.New(err.Error()) + } + return &httpProxyError{statusCode: statusCode, err: merr} +} From 74f7692d93d0f180b3c49b1cb312e49b72e89d96 Mon Sep 17 00:00:00 2001 From: Arvindh Date: Thu, 17 Oct 2024 15:29:34 +0530 Subject: [PATCH 3/4] remove MG errors Signed-off-by: Arvindh --- go.mod | 1 - go.sum | 10 ---------- pkg/http/errors.go | 28 ++++++++++------------------ 3 files changed, 10 insertions(+), 29 deletions(-) diff --git a/go.mod b/go.mod index d1adcb7..002570f 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/absmach/mgate go 1.22.7 require ( - github.com/absmach/magistrala v0.14.0 github.com/caarlos0/env/v11 v11.2.2 github.com/eclipse/paho.mqtt.golang v1.5.0 github.com/google/uuid v1.6.0 diff --git a/go.sum b/go.sum index 31c24f4..cd5ebfd 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,5 @@ -github.com/absmach/magistrala v0.14.0 h1:ouIYOFwl0RMumBuXr4lGtfcukLGxFpzGAD4XDgrMcGg= -github.com/absmach/magistrala v0.14.0/go.mod h1:7hyZSXwYbXHRBQTBUZ0lgsPw3HlPw+PBQRe+xsr542M= github.com/caarlos0/env/v11 v11.2.2 h1:95fApNrUyueipoZN/EhA8mMxiNxrBwDa+oAZrMWl3Kg= github.com/caarlos0/env/v11 v11.2.2/go.mod h1:JBfcdeQiBoI3Zh1QRAWfe+tpiNTmDtcCj/hHHHMx0vc= -github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= -github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/eclipse/paho.mqtt.golang v1.5.0 h1:EH+bUVJNgttidWFkLLVKaQPGmkTUfQQqjOsyvMGvD6o= github.com/eclipse/paho.mqtt.golang v1.5.0/go.mod h1:du/2qNQVqJf/Sqs4MEL77kR8QTqANF7XU7Fk0aOTAgk= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= @@ -12,15 +8,9 @@ github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aN github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= -github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= -github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/http/errors.go b/pkg/http/errors.go index 7dd598a..ef4e4b4 100644 --- a/pkg/http/errors.go +++ b/pkg/http/errors.go @@ -3,15 +3,16 @@ package http -import mgerrors "github.com/absmach/magistrala/pkg/errors" +import "encoding/json" type httpProxyError struct { statusCode int - err mgerrors.Error + err error } type HTTPProxyError interface { - mgerrors.Error + error + MarshalJSON() ([]byte, error) StatusCode() int } @@ -21,16 +22,12 @@ func (hpe *httpProxyError) Error() string { return hpe.err.Error() } -func (hpe *httpProxyError) Err() mgerrors.Error { - return hpe.err -} - -func (hpe *httpProxyError) Msg() string { - return hpe.err.Msg() -} - func (hpe *httpProxyError) MarshalJSON() ([]byte, error) { - return hpe.err.MarshalJSON() + return json.Marshal(struct { + Error string `json:"error"` + }{ + Error: hpe.err.Error(), + }) } func (hpe *httpProxyError) StatusCode() int { @@ -38,10 +35,5 @@ func (hpe *httpProxyError) StatusCode() int { } func NewHTTPProxyError(statusCode int, err error) HTTPProxyError { - var merr mgerrors.Error - var ok bool - if merr, ok = err.(mgerrors.Error); !ok { - merr = mgerrors.New(err.Error()) - } - return &httpProxyError{statusCode: statusCode, err: merr} + return &httpProxyError{statusCode: statusCode, err: err} } From ea2d7f2c44e8737fe13af4de7e591259effa3ab5 Mon Sep 17 00:00:00 2001 From: Arvindh Date: Thu, 17 Oct 2024 15:33:37 +0530 Subject: [PATCH 4/4] remove MG errors Signed-off-by: Arvindh --- pkg/http/http.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pkg/http/http.go b/pkg/http/http.go index ee8850b..ae75c06 100644 --- a/pkg/http/http.go +++ b/pkg/http/http.go @@ -87,12 +87,10 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { func encodeError(w http.ResponseWriter, defStatusCode int, err error) { hpe, ok := err.(HTTPProxyError) - switch ok { - case true: - w.WriteHeader(hpe.StatusCode()) - default: - w.WriteHeader(defStatusCode) + if !ok { + hpe = NewHTTPProxyError(defStatusCode, err) } + w.WriteHeader(hpe.StatusCode()) w.Header().Set("Content-Type", contentType) if err := json.NewEncoder(w).Encode(err); err != nil { w.WriteHeader(http.StatusInternalServerError)