From 988a39db41d1da0ac341e96c19ee307c991bb5b6 Mon Sep 17 00:00:00 2001 From: noah Date: Fri, 29 Oct 2021 20:15:53 +0900 Subject: [PATCH 1/4] Move the openapi file --- openapi.yml => openapi/v1.yaml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename openapi.yml => openapi/v1.yaml (100%) diff --git a/openapi.yml b/openapi/v1.yaml similarity index 100% rename from openapi.yml rename to openapi/v1.yaml From 8fe7a280d786fb9791560fe8fcc008f5094a3b5c Mon Sep 17 00:00:00 2001 From: noah Date: Fri, 29 Oct 2021 21:33:53 +0900 Subject: [PATCH 2/4] Add 'e' package --- pkg/e/code.go | 36 ++++++++++++++++++++++++++++++++++++ pkg/e/trans.go | 33 +++++++++++++++++++++++++++++++++ pkg/e/trans_test.go | 14 ++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 pkg/e/code.go create mode 100644 pkg/e/trans.go create mode 100644 pkg/e/trans_test.go diff --git a/pkg/e/code.go b/pkg/e/code.go new file mode 100644 index 00000000..d9b9d357 --- /dev/null +++ b/pkg/e/code.go @@ -0,0 +1,36 @@ +package e + +import ( + "fmt" +) + +const ( + // ErrorCodeMergeConflict is that the ref can't be merged into the main branch. + ErrorCodeMergeConflict ErrorCode = "merge_conflict" + + // ErrorCodeLicenseDecode is that the license. + ErrorCodeLicenseDecode ErrorCode = "license_decode" + + ErrorCodeInternalError ErrorCode = "internal_error" +) + +type ( + ErrorStatus int + ErrorCode string + + Error struct { + Code ErrorCode + Wrap error + } +) + +func NewError(code ErrorCode, wrap error) *Error { + return &Error{ + Code: code, + Wrap: wrap, + } +} + +func (e *Error) Error() string { + return fmt.Sprintf("code: %s, message: %s : %s", e.Code, GetMessage(e.Code), e.Wrap) +} diff --git a/pkg/e/trans.go b/pkg/e/trans.go new file mode 100644 index 00000000..ce96be32 --- /dev/null +++ b/pkg/e/trans.go @@ -0,0 +1,33 @@ +package e + +import "net/http" + +var messages = map[ErrorCode]string{ + ErrorCodeMergeConflict: "There is merge conflict.", + ErrorCodeLicenseDecode: "Decoding the license is failed.", + ErrorCodeInternalError: "Server internal error.", +} + +func GetMessage(code ErrorCode) string { + message, ok := messages[code] + if !ok { + return string(code) + } + + return message +} + +var httpCodes = map[ErrorCode]int{ + ErrorCodeMergeConflict: http.StatusUnprocessableEntity, + ErrorCodeLicenseDecode: http.StatusUnprocessableEntity, + ErrorCodeInternalError: http.StatusInternalServerError, +} + +func GetHttpCode(code ErrorCode) int { + httpCode, ok := httpCodes[code] + if !ok { + return http.StatusInternalServerError + } + + return httpCode +} diff --git a/pkg/e/trans_test.go b/pkg/e/trans_test.go new file mode 100644 index 00000000..181e5661 --- /dev/null +++ b/pkg/e/trans_test.go @@ -0,0 +1,14 @@ +package e + +import "testing" + +func Test_GetMessage(t *testing.T) { + t.Run("Return code when the message is emtpy.", func(t *testing.T) { + const ErrorCodeEmpty ErrorCode = "emtpy" + + message := GetMessage(ErrorCodeEmpty) + if message != string(ErrorCodeEmpty) { + t.Fatalf("GetMessage = %s, wanted %s", message, string(ErrorCodeEmpty)) + } + }) +} From cf61b8e35b3700593f962ffa99d692c7f55f6a42 Mon Sep 17 00:00:00 2001 From: noah Date: Fri, 29 Oct 2021 21:34:47 +0900 Subject: [PATCH 3/4] Add methods to handle errors --- internal/interactor/license.go | 11 +++++++++-- internal/server/api/v1/license/license.go | 2 +- internal/server/global/http.go | 22 +++++++++++++++++++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/internal/interactor/license.go b/internal/interactor/license.go index 1c75fc22..11451aad 100644 --- a/internal/interactor/license.go +++ b/internal/interactor/license.go @@ -3,6 +3,7 @@ package interactor import ( "context" + "github.com/gitploy-io/gitploy/pkg/e" "github.com/gitploy-io/gitploy/pkg/license" "github.com/gitploy-io/gitploy/vo" ) @@ -15,7 +16,10 @@ func (i *Interactor) GetLicense(ctx context.Context) (*vo.License, error) { ) if cnt, err = i.Store.CountUsers(ctx); err != nil { - return nil, err + return nil, e.NewError( + e.ErrorCodeInternalError, + err, + ) } if i.licenseKey == "" { @@ -24,7 +28,10 @@ func (i *Interactor) GetLicense(ctx context.Context) (*vo.License, error) { } if d, err = license.Decode(i.licenseKey); err != nil { - return nil, err + return nil, e.NewError( + e.ErrorCodeLicenseDecode, + err, + ) } lic := vo.NewStandardLicense(cnt, d) diff --git a/internal/server/api/v1/license/license.go b/internal/server/api/v1/license/license.go index 42f51041..0b8d36c1 100644 --- a/internal/server/api/v1/license/license.go +++ b/internal/server/api/v1/license/license.go @@ -24,7 +24,7 @@ func (l *Licenser) GetLicense(c *gin.Context) { lic, err := l.i.GetLicense(ctx) if err != nil { - gb.ErrorResponse(c, http.StatusInternalServerError, "It has failed to get the license.") + gb.ResponseWithError(c, err) return } diff --git a/internal/server/global/http.go b/internal/server/global/http.go index cde6ebf0..1bcf5dbe 100644 --- a/internal/server/global/http.go +++ b/internal/server/global/http.go @@ -1,6 +1,11 @@ package global -import "github.com/gin-gonic/gin" +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/gitploy-io/gitploy/pkg/e" +) func Response(c *gin.Context, httpCode int, data interface{}) { c.JSON(httpCode, data) @@ -12,6 +17,21 @@ func ErrorResponse(c *gin.Context, httpCode int, message string) { }) } +func ResponseWithError(c *gin.Context, err error) { + if ge, ok := err.(*e.Error); ok { + c.JSON(e.GetHttpCode(ge.Code), map[string]string{ + "code": string(ge.Code), + "message": e.GetMessage(ge.Code), + }) + return + } + + c.JSON(http.StatusInternalServerError, map[string]string{ + "code": string(e.ErrorCodeInternalError), + "message": err.Error(), + }) +} + func AbortWithErrorResponse(c *gin.Context, httpCode int, message string) { c.AbortWithStatusJSON(httpCode, map[string]string{ "message": message, From 72a496429bb774bb99db14217111f8efdf6af5dd Mon Sep 17 00:00:00 2001 From: noah Date: Fri, 29 Oct 2021 21:37:46 +0900 Subject: [PATCH 4/4] Add unwrap method --- pkg/e/code.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/e/code.go b/pkg/e/code.go index d9b9d357..317e1289 100644 --- a/pkg/e/code.go +++ b/pkg/e/code.go @@ -34,3 +34,7 @@ func NewError(code ErrorCode, wrap error) *Error { func (e *Error) Error() string { return fmt.Sprintf("code: %s, message: %s : %s", e.Code, GetMessage(e.Code), e.Wrap) } + +func (e *Error) Unwrap() error { + return e.Wrap +}