refatcor: improve error route interceptor

This commit is contained in:
Jonas Kaninda
2024-11-14 09:49:18 +01:00
parent 42abf56473
commit 3c4920ec9a
8 changed files with 25 additions and 54 deletions

View File

@@ -18,7 +18,7 @@ limitations under the License.
import (
"fmt"
"github.com/jkaninda/goma-gateway/internal/middleware"
error_interceptor "github.com/jkaninda/goma-gateway/pkg/error-interceptor"
"github.com/jkaninda/goma-gateway/pkg/errorinterceptor"
"github.com/jkaninda/goma-gateway/pkg/logger"
"github.com/jkaninda/goma-gateway/util"
"golang.org/x/oauth2"
@@ -185,10 +185,9 @@ func initConfig(configFile string) error {
Path: "/",
Name: "Hostname and load balancing example",
Hosts: []string{"example.com", "example.localhost"},
//InterceptErrors: []int{404, 405, 500},
ErrorInterceptor: error_interceptor.ErrorInterceptor{
ContentType: applicationJson,
Errors: []error_interceptor.Error{
//ErrorIntercept: []int{404, 405, 500},
ErrorInterceptor: errorinterceptor.ErrorInterceptor{
Errors: []errorinterceptor.Error{
{
Code: http.StatusUnauthorized,
Message: http.StatusText(http.StatusUnauthorized),

View File

@@ -19,7 +19,7 @@ package middleware
import (
"fmt"
errorinterceptor "github.com/jkaninda/goma-gateway/pkg/error-interceptor"
errorinterceptor "github.com/jkaninda/goma-gateway/pkg/errorinterceptor"
"github.com/jkaninda/goma-gateway/pkg/logger"
"net/http"
"regexp"

View File

@@ -20,8 +20,9 @@ package middleware
import (
"encoding/json"
"fmt"
errorinterceptor "github.com/jkaninda/goma-gateway/pkg/error-interceptor"
"github.com/jkaninda/goma-gateway/pkg/errorinterceptor"
"net/http"
"slices"
)
func getRealIP(r *http.Request) string {
@@ -34,14 +35,7 @@ func getRealIP(r *http.Request) string {
return r.RemoteAddr
}
func allowedOrigin(origins []string, origin string) bool {
for _, o := range origins {
if o == origin {
return true
}
continue
}
return false
return slices.Contains(origins, origin)
}
func canInterceptError(code int, errors []errorinterceptor.Error) bool {
for _, er := range errors {
@@ -71,25 +65,16 @@ func RespondWithError(w http.ResponseWriter, statusCode int, logMessage string,
if err != nil {
message = logMessage
}
if errorIntercept.ContentType == errorinterceptor.ApplicationJson {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
err := json.NewEncoder(w).Encode(ProxyResponseError{
Success: false,
Code: statusCode,
Message: message,
})
if err != nil {
return
}
return
} else {
w.Header().Set("Content-Type", "plain/text;charset=utf-8")
w.WriteHeader(statusCode)
_, err2 := w.Write([]byte(message))
if err2 != nil {
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
err = json.NewEncoder(w).Encode(ProxyResponseError{
Success: false,
Code: statusCode,
Message: message,
})
if err != nil {
return
}
return
}

View File

@@ -17,7 +17,7 @@ package middleware
*
*/
import (
errorinterceptor "github.com/jkaninda/goma-gateway/pkg/error-interceptor"
"github.com/jkaninda/goma-gateway/pkg/errorinterceptor"
"github.com/jkaninda/goma-gateway/pkg/logger"
"io"
"net/http"

View File

@@ -19,7 +19,7 @@ package middleware
import (
"bytes"
errorinterceptor "github.com/jkaninda/goma-gateway/pkg/error-interceptor"
errorinterceptor "github.com/jkaninda/goma-gateway/pkg/errorinterceptor"
"net/http"
"sync"
"time"

View File

@@ -20,7 +20,7 @@ package pkg
import (
"context"
"github.com/gorilla/mux"
errorinterceptor "github.com/jkaninda/goma-gateway/pkg/error-interceptor"
errorinterceptor "github.com/jkaninda/goma-gateway/pkg/errorinterceptor"
"time"
)
@@ -287,16 +287,3 @@ type Health struct {
Interval string
HealthyStatuses []int
}
//type ErrorInterceptor struct {
// // ContentType error response content type, application/json, plain/text
// ContentType string `yaml:"contentType"`
// //Errors contains error status code and custom message
// Errors []ErrorInterceptor `yaml:"errors"`
//}
//type ErrorInterceptor struct {
// // Code HTTP status code
// Code int `yaml:"code"`
// // Message custom message
// Message string `yaml:"message"`
//}

View File

@@ -15,17 +15,17 @@
*
*/
package error_interceptor
package errorinterceptor
type ErrorInterceptor struct {
// ContentType error response content type, application/json, plain/text
ContentType string `yaml:"contentType"`
//ContentType string `yaml:"contentType"`
//Errors contains error status code and custom message
Errors []Error `yaml:"errors"`
}
type Error struct {
// Code HTTP status code
Code int `yaml:"code"`
// Message custom message
// Message Error custom response message
Message string `yaml:"message"`
}

View File

@@ -15,7 +15,7 @@
*
*/
package error_interceptor
package errorinterceptor
const TextPlain = "text/plain"
const ApplicationXml = "application/xml"