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

Commit 403cb42

Browse files
author
Noah Hanjun Lee
authored
Add error package to handle generally. (#187)
* Move the openapi file * Add 'e' package to handle errors
1 parent 1b01105 commit 403cb42

File tree

7 files changed

+118
-4
lines changed

7 files changed

+118
-4
lines changed

internal/interactor/license.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package interactor
33
import (
44
"context"
55

6+
"github.com/gitploy-io/gitploy/pkg/e"
67
"github.com/gitploy-io/gitploy/pkg/license"
78
"github.com/gitploy-io/gitploy/vo"
89
)
@@ -15,7 +16,10 @@ func (i *Interactor) GetLicense(ctx context.Context) (*vo.License, error) {
1516
)
1617

1718
if cnt, err = i.Store.CountUsers(ctx); err != nil {
18-
return nil, err
19+
return nil, e.NewError(
20+
e.ErrorCodeInternalError,
21+
err,
22+
)
1923
}
2024

2125
if i.licenseKey == "" {
@@ -24,7 +28,10 @@ func (i *Interactor) GetLicense(ctx context.Context) (*vo.License, error) {
2428
}
2529

2630
if d, err = license.Decode(i.licenseKey); err != nil {
27-
return nil, err
31+
return nil, e.NewError(
32+
e.ErrorCodeLicenseDecode,
33+
err,
34+
)
2835
}
2936

3037
lic := vo.NewStandardLicense(cnt, d)

internal/server/api/v1/license/license.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (l *Licenser) GetLicense(c *gin.Context) {
2424

2525
lic, err := l.i.GetLicense(ctx)
2626
if err != nil {
27-
gb.ErrorResponse(c, http.StatusInternalServerError, "It has failed to get the license.")
27+
gb.ResponseWithError(c, err)
2828
return
2929
}
3030

internal/server/global/http.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package global
22

3-
import "github.com/gin-gonic/gin"
3+
import (
4+
"net/http"
5+
6+
"github.com/gin-gonic/gin"
7+
"github.com/gitploy-io/gitploy/pkg/e"
8+
)
49

510
func Response(c *gin.Context, httpCode int, data interface{}) {
611
c.JSON(httpCode, data)
@@ -12,6 +17,21 @@ func ErrorResponse(c *gin.Context, httpCode int, message string) {
1217
})
1318
}
1419

20+
func ResponseWithError(c *gin.Context, err error) {
21+
if ge, ok := err.(*e.Error); ok {
22+
c.JSON(e.GetHttpCode(ge.Code), map[string]string{
23+
"code": string(ge.Code),
24+
"message": e.GetMessage(ge.Code),
25+
})
26+
return
27+
}
28+
29+
c.JSON(http.StatusInternalServerError, map[string]string{
30+
"code": string(e.ErrorCodeInternalError),
31+
"message": err.Error(),
32+
})
33+
}
34+
1535
func AbortWithErrorResponse(c *gin.Context, httpCode int, message string) {
1636
c.AbortWithStatusJSON(httpCode, map[string]string{
1737
"message": message,
File renamed without changes.

pkg/e/code.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package e
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
const (
8+
// ErrorCodeMergeConflict is that the ref can't be merged into the main branch.
9+
ErrorCodeMergeConflict ErrorCode = "merge_conflict"
10+
11+
// ErrorCodeLicenseDecode is that the license.
12+
ErrorCodeLicenseDecode ErrorCode = "license_decode"
13+
14+
ErrorCodeInternalError ErrorCode = "internal_error"
15+
)
16+
17+
type (
18+
ErrorStatus int
19+
ErrorCode string
20+
21+
Error struct {
22+
Code ErrorCode
23+
Wrap error
24+
}
25+
)
26+
27+
func NewError(code ErrorCode, wrap error) *Error {
28+
return &Error{
29+
Code: code,
30+
Wrap: wrap,
31+
}
32+
}
33+
34+
func (e *Error) Error() string {
35+
return fmt.Sprintf("code: %s, message: %s : %s", e.Code, GetMessage(e.Code), e.Wrap)
36+
}
37+
38+
func (e *Error) Unwrap() error {
39+
return e.Wrap
40+
}

pkg/e/trans.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package e
2+
3+
import "net/http"
4+
5+
var messages = map[ErrorCode]string{
6+
ErrorCodeMergeConflict: "There is merge conflict.",
7+
ErrorCodeLicenseDecode: "Decoding the license is failed.",
8+
ErrorCodeInternalError: "Server internal error.",
9+
}
10+
11+
func GetMessage(code ErrorCode) string {
12+
message, ok := messages[code]
13+
if !ok {
14+
return string(code)
15+
}
16+
17+
return message
18+
}
19+
20+
var httpCodes = map[ErrorCode]int{
21+
ErrorCodeMergeConflict: http.StatusUnprocessableEntity,
22+
ErrorCodeLicenseDecode: http.StatusUnprocessableEntity,
23+
ErrorCodeInternalError: http.StatusInternalServerError,
24+
}
25+
26+
func GetHttpCode(code ErrorCode) int {
27+
httpCode, ok := httpCodes[code]
28+
if !ok {
29+
return http.StatusInternalServerError
30+
}
31+
32+
return httpCode
33+
}

pkg/e/trans_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package e
2+
3+
import "testing"
4+
5+
func Test_GetMessage(t *testing.T) {
6+
t.Run("Return code when the message is emtpy.", func(t *testing.T) {
7+
const ErrorCodeEmpty ErrorCode = "emtpy"
8+
9+
message := GetMessage(ErrorCodeEmpty)
10+
if message != string(ErrorCodeEmpty) {
11+
t.Fatalf("GetMessage = %s, wanted %s", message, string(ErrorCodeEmpty))
12+
}
13+
})
14+
}

0 commit comments

Comments
 (0)