Skip to content

Commit 3ffe037

Browse files
committed
refactor: added RequestError interface and its implementations
* Added RequestError interface and its implementations (FieldError, HeaderError and ParameterError) * Re-structured folders
1 parent aec6c19 commit 3ffe037

File tree

18 files changed

+146
-70
lines changed

18 files changed

+146
-70
lines changed

http/client_ip.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package http
22

33
import (
4+
"github.com/ralvarezdev/go-net/http/status"
45
"net/http"
56
"strings"
67
)
78

89
// GetClientIP returns the client's IP address from the request
910
func GetClientIP(r *http.Request) string {
1011
// Check if the request has a forwarded IP from a proxy or load balancer
11-
forwarded := r.Header.Get(XForwardedFor)
12+
forwarded := r.Header.Get(status.XForwardedFor)
1213
if forwarded != "" {
1314
// X-Forwarded-For can contain multiple IP addresses, the client's IP is the first one
1415
ip := strings.Split(forwarded, ",")[0]

http/errors.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ package http
22

33
import (
44
"errors"
5+
gojwtnethttp "github.com/ralvarezdev/go-jwt/net/http"
6+
gonethttpresponse "github.com/ralvarezdev/go-net/http/response"
57
)
68

79
var (
8-
ErrInvalidRequestBody = "invalid request body: %v"
910
ErrNilRequestBody = errors.New("request body cannot be nil")
1011
ErrInDevelopment = errors.New("in development")
11-
ErrInvalidAuthorizationHeader = errors.New("invalid authorization header")
12+
ErrInvalidAuthorizationHeader = gonethttpresponse.NewHeaderError(
13+
gojwtnethttp.AuthorizationHeaderKey,
14+
"invalid authorization header",
15+
)
1216
)

http/handler/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package handler
22

33
import (
44
goflagsmode "github.com/ralvarezdev/go-flags/mode"
5-
gonethttperrors "github.com/ralvarezdev/go-net/http/errors"
65
gonethttpjson "github.com/ralvarezdev/go-net/http/json"
76
gonethttpresponse "github.com/ralvarezdev/go-net/http/response"
7+
gonethttperrors "github.com/ralvarezdev/go-net/http/status/errors"
88
"net/http"
99
)
1010

http/json/body.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ func bodyDecodeErrorHandler(
3232
return encoder.Encode(
3333
w,
3434
gonethttpresponse.NewDebugFailResponse(
35-
gonethttpresponse.NewFieldErrorsBodyData(
36-
fieldName,
37-
fmt.Errorf(
38-
ErrFieldInvalidValue,
39-
fieldTypeName,
40-
fieldValue,
35+
gonethttpresponse.NewRequestErrorsBodyData(
36+
gonethttpresponse.NewFieldError(
37+
fieldName,
38+
fmt.Sprintf(
39+
ErrFieldInvalidValue,
40+
fieldTypeName,
41+
fieldValue,
42+
),
4143
),
4244
),
4345
err,

http/json/decoder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package json
33
import (
44
"encoding/json"
55
goflagsmode "github.com/ralvarezdev/go-flags/mode"
6-
gonethttperrors "github.com/ralvarezdev/go-net/http/errors"
76
gonethttpresponse "github.com/ralvarezdev/go-net/http/response"
7+
gonethttpstatuserrors "github.com/ralvarezdev/go-net/http/status/errors"
88
"io"
99
"net/http"
1010
)
@@ -52,7 +52,7 @@ func (d *DefaultDecoder) Decode(
5252
if dest == nil {
5353
_ = d.encoder.Encode(
5454
w, gonethttpresponse.NewDebugErrorResponse(
55-
gonethttperrors.InternalServerError,
55+
gonethttpstatuserrors.InternalServerError,
5656
err,
5757
nil,
5858
nil,

http/json/encoder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package json
33
import (
44
"encoding/json"
55
"github.com/ralvarezdev/go-flags/mode"
6-
gonethttperrors "github.com/ralvarezdev/go-net/http/errors"
76
gonethttpresponse "github.com/ralvarezdev/go-net/http/response"
7+
gonethttpstatuserrors "github.com/ralvarezdev/go-net/http/status/errors"
88
"net/http"
99
)
1010

@@ -43,7 +43,7 @@ func (d *DefaultEncoder) Encode(
4343
return d.Encode(
4444
w,
4545
gonethttpresponse.NewDebugErrorResponse(
46-
gonethttperrors.InternalServerError,
46+
gonethttpstatuserrors.InternalServerError,
4747
err,
4848
nil,
4949
nil,

http/json/errors.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
)
66

77
var (
8-
ErrNilData = errors.New("json data is nil")
98
ErrNilEncoder = errors.New("json encoder is nil")
109
ErrNilDecoder = errors.New("json decoder is nil")
1110
ErrUnmarshalBodyDataFailed = errors.New("failed to unmarshal json body data")

http/json/stream_decoder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package json
33
import (
44
"encoding/json"
55
goflagsmode "github.com/ralvarezdev/go-flags/mode"
6-
gonethttperrors "github.com/ralvarezdev/go-net/http/errors"
76
gonethttpresponse "github.com/ralvarezdev/go-net/http/response"
7+
gonethttpstatuserrors "github.com/ralvarezdev/go-net/http/status/errors"
88
"net/http"
99
)
1010

@@ -42,7 +42,7 @@ func (d *DefaultStreamDecoder) Decode(
4242
if dest == nil {
4343
_ = d.encoder.Encode(
4444
w, gonethttpresponse.NewDebugErrorResponse(
45-
gonethttperrors.InternalServerError,
45+
gonethttpstatuserrors.InternalServerError,
4646
err,
4747
nil,
4848
nil,

http/json/stream_encoder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package json
33
import (
44
"encoding/json"
55
goflagsmode "github.com/ralvarezdev/go-flags/mode"
6-
gonethttperrors "github.com/ralvarezdev/go-net/http/errors"
76
gonethttpresponse "github.com/ralvarezdev/go-net/http/response"
7+
gonethttpstatuserrors "github.com/ralvarezdev/go-net/http/status/errors"
88
"net/http"
99
)
1010

@@ -36,7 +36,7 @@ func (d *DefaultStreamEncoder) Encode(
3636
_ = d.Encode(
3737
w,
3838
gonethttpresponse.NewDebugErrorResponse(
39-
gonethttperrors.InternalServerError,
39+
gonethttpstatuserrors.InternalServerError,
4040
err,
4141
nil,
4242
nil,

http/jwt/validator/handler.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package validator
22

33
import (
4+
gojwtnethttp "github.com/ralvarezdev/go-jwt/net/http"
45
gonethttpjson "github.com/ralvarezdev/go-net/http/json"
56
gonethttpresponse "github.com/ralvarezdev/go-net/http/response"
67
"net/http"
78
)
89

910
// FailHandler handles the possible JWT validation errors
10-
type FailHandler func(w http.ResponseWriter, err ...error)
11+
type FailHandler func(
12+
w http.ResponseWriter,
13+
error error,
14+
)
1115

1216
// NewDefaultFailHandler function
1317
func NewDefaultFailHandler(
@@ -18,13 +22,18 @@ func NewDefaultFailHandler(
1822
return nil, gonethttpjson.ErrNilEncoder
1923
}
2024

21-
return func(w http.ResponseWriter, err ...error) {
25+
return func(
26+
w http.ResponseWriter,
27+
error error,
28+
) {
2229
// Encode the response
2330
_ = jsonEncoder.Encode(
2431
w, gonethttpresponse.NewFailResponse(
25-
gonethttpresponse.NewFieldErrorsBodyData(
26-
"authorization",
27-
err...,
32+
gonethttpresponse.NewRequestErrorsBodyData(
33+
gonethttpresponse.NewHeaderError(
34+
gojwtnethttp.AuthorizationHeaderKey,
35+
error.Error(),
36+
),
2837
),
2938
nil,
3039
http.StatusUnauthorized,

0 commit comments

Comments
 (0)