Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 9415ab7

Browse files
author
Noah Lee
authored
Add the methods to Error for HTTP code (#491)
* Add the methods for HTTP code * Fix the bugs
1 parent ff2feec commit 9415ab7

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

Diff for: internal/server/global/http.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func Response(c *gin.Context, httpCode int, data interface{}) {
1313

1414
func ResponseWithError(c *gin.Context, err error) {
1515
if ge, ok := err.(*e.Error); ok {
16-
c.JSON(e.GetHttpCode(ge.Code), map[string]string{
16+
c.JSON(ge.GetHTTPCode(), map[string]string{
1717
"code": string(ge.Code),
1818
"message": ge.Message,
1919
})
@@ -44,7 +44,7 @@ func ResponseWithStatusAndError(c *gin.Context, status int, err error) {
4444

4545
func AbortWithError(c *gin.Context, err error) {
4646
if ge, ok := err.(*e.Error); ok {
47-
c.AbortWithStatusJSON(e.GetHttpCode(ge.Code), map[string]string{
47+
c.AbortWithStatusJSON(ge.GetHTTPCode(), map[string]string{
4848
"code": string(ge.Code),
4949
"message": ge.Message,
5050
})

Diff for: pkg/e/code.go

+30-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package e
33
import (
44
"errors"
55
"fmt"
6+
"net/http"
67
"strings"
78
)
89

@@ -62,22 +63,26 @@ type (
6263
Code ErrorCode
6364
Message string
6465
Wrap error
66+
67+
httpCode int
6568
}
6669
)
6770

6871
func NewError(code ErrorCode, wrap error) *Error {
6972
return &Error{
70-
Code: code,
71-
Message: GetMessage(code),
72-
Wrap: wrap,
73+
Code: code,
74+
Message: GetMessage(code),
75+
Wrap: wrap,
76+
httpCode: mapHTTPCode(code),
7377
}
7478
}
7579

7680
func NewErrorWithMessage(code ErrorCode, message string, wrap error) *Error {
7781
return &Error{
78-
Code: code,
79-
Message: message,
80-
Wrap: wrap,
82+
Code: code,
83+
Message: message,
84+
Wrap: wrap,
85+
httpCode: mapHTTPCode(code),
8186
}
8287
}
8388

@@ -94,10 +99,29 @@ func (e *Error) Error() string {
9499
return strings.Join(msgs, ", ")
95100
}
96101

102+
// GetHTTPCode returns the HTTP code.
103+
func (e *Error) GetHTTPCode() int {
104+
return e.httpCode
105+
}
106+
107+
// SetHTTPCode sets the HTTP code manually.
108+
func (e *Error) SetHTTPCode(code int) {
109+
e.httpCode = code
110+
}
111+
97112
func (e *Error) Unwrap() error {
98113
return e.Wrap
99114
}
100115

116+
func mapHTTPCode(code ErrorCode) int {
117+
httpCode, ok := httpCodes[code]
118+
if !ok {
119+
return http.StatusInternalServerError
120+
}
121+
122+
return httpCode
123+
}
124+
101125
func IsError(err error) bool {
102126
var ge *Error
103127
return errors.As(err, &ge)

Diff for: pkg/e/code_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,19 @@ package e
22

33
import (
44
"fmt"
5+
"net/http"
56
"testing"
67
)
78

9+
func TestError_GetHTTPError(t *testing.T) {
10+
t.Run("Return the matche HTTP code.", func(t *testing.T) {
11+
err := NewError(ErrorCodeInternalError, nil)
12+
if err.GetHTTPCode() != http.StatusInternalServerError {
13+
t.Fatalf("GetHTTPCode = %v, wanted %v", err.GetHTTPCode(), http.StatusInternalServerError)
14+
}
15+
})
16+
}
17+
818
func Test_IsError(t *testing.T) {
919
t.Run("Return true when the type of error is Error.", func(t *testing.T) {
1020
err := NewError(ErrorCodeInternalError, nil)

Diff for: pkg/e/trans.go

-9
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,3 @@ var httpCodes = map[ErrorCode]int{
5252
ErrorPermissionRequired: http.StatusForbidden,
5353
ErrorRepoUniqueName: http.StatusUnprocessableEntity,
5454
}
55-
56-
func GetHttpCode(code ErrorCode) int {
57-
httpCode, ok := httpCodes[code]
58-
if !ok {
59-
return http.StatusInternalServerError
60-
}
61-
62-
return httpCode
63-
}

0 commit comments

Comments
 (0)