Skip to content

Commit

Permalink
- Adding string to make request function
Browse files Browse the repository at this point in the history
- Fixing signature
- Fixing and updating deps
  • Loading branch information
Maya Sergeeva committed Jan 17, 2022
1 parent b24a7b2 commit 9b4edf4
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 55 deletions.
7 changes: 1 addition & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
deps:
go mod vendor
.PHONY: deps

# ----
## LINTER stuff start

Expand All @@ -27,8 +23,7 @@ tests: tests_include_check

tests_html: tests_include_check
@make -f tests.mk go_tests_html
@open coverage.html
.PHONY: tests
.PHONY: tests_html

## TESTS stuff end
# ----
12 changes: 7 additions & 5 deletions gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
errs "github.com/spacetab-io/errors-go"
"github.com/spacetab-io/errors-go"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand All @@ -19,15 +19,15 @@ const (
// Response makes common error response.
func Response(c *gin.Context, err interface{}) {
errCode, data := MakeResponse(err, getLang(c))
resp := errs.Response{Error: *data}
resp := errors.Response{Error: data}
c.AbortWithStatusJSON(errCode, resp)
}

// MakeResponse makes ErrorObject based on error type.
func MakeResponse(err interface{}, lang langName) (int, *errs.ErrorObject) {
errObj := &errs.ErrorObject{}
func MakeResponse(err interface{}, lang langName) (int, errors.ErrorObject) {
errObj := errors.ErrorObject{}
errCode := http.StatusBadRequest
errType := errs.ErrorTypeError
errType := errors.ErrorTypeError

switch et := err.(type) {
case GRPCValidationError:
Expand Down Expand Up @@ -56,6 +56,8 @@ func MakeResponse(err interface{}, lang langName) (int, *errs.ErrorObject) {
}

errCode, errObj.Message = getErrCode(et)
case string:
errObj.Message = et
case map[string]error:
msgs := make(map[string]string)
for k, e := range et {
Expand Down
24 changes: 12 additions & 12 deletions gin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
ginerrors "github.com/microparts/errors-go-gin"
errs "github.com/spacetab-io/errors-go"
ginerrors "github.com/spacetab-io/errors-go-gin"
"github.com/stretchr/testify/assert"
)

Expand All @@ -20,26 +20,26 @@ func TestMakeResponse(t *testing.T) {
name string
err interface{}
httpCode int
errObject *errs.ErrorObject
errObject errs.ErrorObject
}
errType := errs.ErrorTypeError

// nolint: goerr113
cases := []testCase{
{name: "common error", err: errors.New("common err"), httpCode: http.StatusBadRequest, errObject: &errs.ErrorObject{Message: "common err", Type: &errType}},
{name: "common error", err: errors.New("common err"), httpCode: http.StatusBadRequest, errObject: errs.ErrorObject{Message: "common err", Type: &errType}},

{name: "validation error", err: makeValidationError(), httpCode: http.StatusUnprocessableEntity, errObject: &errs.ErrorObject{Message: "validation error", Validation: map[errs.FieldName][]errs.ValidationError{"String": {"Ошибка валидации для свойства `String` с правилом `%!s(MISSING)`"}}, Type: &errType}},
{name: "validation error", err: makeValidationError(), httpCode: http.StatusUnprocessableEntity, errObject: errs.ErrorObject{Message: "validation error", Validation: map[errs.FieldName][]errs.ValidationError{"String": {"Ошибка валидации для свойства `String` с правилом `%!s(MISSING)`"}}, Type: &errType}},

{name: "mux err no method allowed", err: ginerrors.ErrNoMethod, httpCode: http.StatusMethodNotAllowed, errObject: &errs.ErrorObject{Message: ginerrors.ErrNoMethod.Error(), Type: &errType}},
{name: "mux err route not found", err: ginerrors.ErrNotFound, httpCode: http.StatusNotFound, errObject: &errs.ErrorObject{Message: ginerrors.ErrNotFound.Error(), Type: &errType}},
{name: "mux err no method allowed", err: ginerrors.ErrNoMethod, httpCode: http.StatusMethodNotAllowed, errObject: errs.ErrorObject{Message: ginerrors.ErrNoMethod.Error(), Type: &errType}},
{name: "mux err route not found", err: ginerrors.ErrNotFound, httpCode: http.StatusNotFound, errObject: errs.ErrorObject{Message: ginerrors.ErrNotFound.Error(), Type: &errType}},

{name: "errors slice", err: []error{errors.New("common err 1"), errors.New("common err 2")}, httpCode: http.StatusInternalServerError, errObject: &errs.ErrorObject{Message: "common err 1; common err 2", Type: &errType}},
{name: "map of errors", err: map[string]error{"common_err": errors.New("common err")}, httpCode: http.StatusBadRequest, errObject: &errs.ErrorObject{Message: map[string]string{"common_err": "common err"}, Type: &errType}},
{name: "errors slice", err: []error{errors.New("common err 1"), errors.New("common err 2")}, httpCode: http.StatusInternalServerError, errObject: errs.ErrorObject{Message: "common err 1; common err 2", Type: &errType}},
{name: "map of errors", err: map[string]error{"common_err": errors.New("common err")}, httpCode: http.StatusBadRequest, errObject: errs.ErrorObject{Message: map[string]string{"common_err": "common err"}, Type: &errType}},

{name: "record not found", err: ginerrors.ErrRecordNotFound, httpCode: http.StatusNotFound, errObject: &errs.ErrorObject{Message: ginerrors.ErrRecordNotFound.Error(), Type: &errType}},
{name: "sql error no rows", err: sql.ErrNoRows, httpCode: http.StatusNotFound, errObject: &errs.ErrorObject{Message: ginerrors.ErrRecordNotFound.Error(), Type: &errType}},
{name: "sql error conn done", err: sql.ErrConnDone, httpCode: http.StatusInternalServerError, errObject: &errs.ErrorObject{Message: sql.ErrConnDone.Error(), Type: &errType}},
{name: "sql error tx done", err: sql.ErrTxDone, httpCode: http.StatusInternalServerError, errObject: &errs.ErrorObject{Message: sql.ErrTxDone.Error(), Type: &errType}},
{name: "record not found", err: ginerrors.ErrRecordNotFound, httpCode: http.StatusNotFound, errObject: errs.ErrorObject{Message: ginerrors.ErrRecordNotFound.Error(), Type: &errType}},
{name: "sql error no rows", err: sql.ErrNoRows, httpCode: http.StatusNotFound, errObject: errs.ErrorObject{Message: ginerrors.ErrRecordNotFound.Error(), Type: &errType}},
{name: "sql error conn done", err: sql.ErrConnDone, httpCode: http.StatusInternalServerError, errObject: errs.ErrorObject{Message: sql.ErrConnDone.Error(), Type: &errType}},
{name: "sql error tx done", err: sql.ErrTxDone, httpCode: http.StatusInternalServerError, errObject: errs.ErrorObject{Message: sql.ErrTxDone.Error(), Type: &errType}},
}

t.Parallel()
Expand Down
12 changes: 7 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module github.com/microparts/errors-go-gin
module github.com/spacetab-io/errors-go-gin

go 1.16

require (
github.com/gin-gonic/gin v1.7.2
github.com/go-playground/validator/v10 v10.8.0
github.com/gin-gonic/gin v1.7.7
github.com/go-playground/validator/v10 v10.10.0
github.com/spacetab-io/errors-go v1.3.0
github.com/stretchr/testify v1.7.0
google.golang.org/genproto v0.0.0-20210726200206-e7812ac95cc0
google.golang.org/grpc v1.39.0
google.golang.org/genproto v0.0.0-20220114231437-d2e6a121cae0
google.golang.org/grpc v1.43.0
)

//replace github.com/spacetab-io/errors-go => ../errors-go
Loading

0 comments on commit 9b4edf4

Please sign in to comment.