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

Add the methods to Error for HTTP code #491

Merged
merged 2 commits into from
Nov 8, 2022
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
4 changes: 2 additions & 2 deletions internal/server/global/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func Response(c *gin.Context, httpCode int, data interface{}) {

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

func AbortWithError(c *gin.Context, err error) {
if ge, ok := err.(*e.Error); ok {
c.AbortWithStatusJSON(e.GetHttpCode(ge.Code), map[string]string{
c.AbortWithStatusJSON(ge.GetHTTPCode(), map[string]string{
"code": string(ge.Code),
"message": ge.Message,
})
Expand Down
36 changes: 30 additions & 6 deletions pkg/e/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package e
import (
"errors"
"fmt"
"net/http"
"strings"
)

Expand Down Expand Up @@ -62,22 +63,26 @@ type (
Code ErrorCode
Message string
Wrap error

httpCode int
}
)

func NewError(code ErrorCode, wrap error) *Error {
return &Error{
Code: code,
Message: GetMessage(code),
Wrap: wrap,
Code: code,
Message: GetMessage(code),
Wrap: wrap,
httpCode: mapHTTPCode(code),
}
}

func NewErrorWithMessage(code ErrorCode, message string, wrap error) *Error {
return &Error{
Code: code,
Message: message,
Wrap: wrap,
Code: code,
Message: message,
Wrap: wrap,
httpCode: mapHTTPCode(code),
}
}

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

// GetHTTPCode returns the HTTP code.
func (e *Error) GetHTTPCode() int {
return e.httpCode
}

// SetHTTPCode sets the HTTP code manually.
func (e *Error) SetHTTPCode(code int) {
e.httpCode = code
}

func (e *Error) Unwrap() error {
return e.Wrap
}

func mapHTTPCode(code ErrorCode) int {
httpCode, ok := httpCodes[code]
if !ok {
return http.StatusInternalServerError
}

return httpCode
}

func IsError(err error) bool {
var ge *Error
return errors.As(err, &ge)
Expand Down
10 changes: 10 additions & 0 deletions pkg/e/code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@ package e

import (
"fmt"
"net/http"
"testing"
)

func TestError_GetHTTPError(t *testing.T) {
t.Run("Return the matche HTTP code.", func(t *testing.T) {
err := NewError(ErrorCodeInternalError, nil)
if err.GetHTTPCode() != http.StatusInternalServerError {
t.Fatalf("GetHTTPCode = %v, wanted %v", err.GetHTTPCode(), http.StatusInternalServerError)
}
})
}

func Test_IsError(t *testing.T) {
t.Run("Return true when the type of error is Error.", func(t *testing.T) {
err := NewError(ErrorCodeInternalError, nil)
Expand Down
9 changes: 0 additions & 9 deletions pkg/e/trans.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,3 @@ var httpCodes = map[ErrorCode]int{
ErrorPermissionRequired: http.StatusForbidden,
ErrorRepoUniqueName: http.StatusUnprocessableEntity,
}

func GetHttpCode(code ErrorCode) int {
httpCode, ok := httpCodes[code]
if !ok {
return http.StatusInternalServerError
}

return httpCode
}