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

Add error package to handle generally. #187

Merged
merged 4 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions internal/interactor/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 == "" {
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/server/api/v1/license/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
22 changes: 21 additions & 1 deletion internal/server/global/http.go
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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,
Expand Down
File renamed without changes.
40 changes: 40 additions & 0 deletions pkg/e/code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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)
}

func (e *Error) Unwrap() error {
return e.Wrap
}
33 changes: 33 additions & 0 deletions pkg/e/trans.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 14 additions & 0 deletions pkg/e/trans_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
})
}