Skip to content

Commit a94a2e4

Browse files
Merge pull request #1 from laironacosta/feature/handler-errors
adding custom errors in responses
2 parents 7c1cff5 + 6cd38a6 commit a94a2e4

12 files changed

+187
-92
lines changed
+14-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
package dto
22

3+
import (
4+
"github.com/go-playground/validator/v10"
5+
)
6+
37
type CreateUserRequest struct {
4-
Name string `json:"name" binding:"required"`
5-
Email string `json:"email" binding:"required,email"`
8+
Name string `json:"name" validate:"required"`
9+
Email string `json:"email" validate:"required,email"`
10+
}
11+
12+
func (cu *CreateUserRequest) Validate() error {
13+
if err := validator.New().Struct(cu); err != nil {
14+
return err
15+
}
16+
17+
return nil
618
}
+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
package dto
22

3+
import (
4+
"github.com/go-playground/validator/v10"
5+
)
6+
37
type UpdateUserRequest struct {
4-
Name string `json:"name" binding:"required"`
8+
Name string `json:"name" validate:"required"`
9+
}
10+
11+
func (uu *UpdateUserRequest) Validate() error {
12+
if err := validator.New().Struct(uu); err != nil {
13+
return err
14+
}
15+
16+
return nil
517
}

controllers/dto/user_dto.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dto
22

33
type User struct {
4-
Name string `json:"name" binding:"required"`
5-
Email string `json:"email" binding:"required,email"`
4+
Name string `json:"name"`
5+
Email string `json:"email"`
66
}

controllers/health_controller.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package controllers
22

33
import (
4-
"github.com/labstack/echo"
4+
"github.com/labstack/echo/v4"
55
"github.com/laironacosta/ms-echo-go/controllers/dto"
66
"net/http"
77
)

controllers/users_controller.go

+24-8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package controllers
33
import (
44
"context"
55
"fmt"
6-
"github.com/labstack/echo"
6+
"github.com/labstack/echo/v4"
7+
"github.com/labstack/gommon/log"
8+
respKit "github.com/laironacosta/kit-go/middleware/responses"
79
"github.com/laironacosta/ms-echo-go/controllers/dto"
10+
"github.com/laironacosta/ms-echo-go/enums"
811
"github.com/laironacosta/ms-echo-go/services"
912
"net/http"
1013
)
@@ -29,14 +32,18 @@ func NewUserController(userService services.UserServiceInterface) UserController
2932
func (ctr *UserController) Create(c echo.Context) error {
3033
u := dto.CreateUserRequest{}
3134
if err := c.Bind(&u); err != nil {
32-
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
35+
return respKit.GenericBadRequestError(enums.ErrorRequestBodyCode, err.Error())
36+
}
37+
38+
if err := u.Validate(); err != nil {
39+
return respKit.GenericBadRequestError(enums.ErrorRequestBodyCode, err.Error())
3340
}
3441

3542
if err := ctr.userService.Create(context.Background(), u); err != nil {
36-
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
43+
return err
3744
}
3845

39-
fmt.Printf("Request received: %+v \n", u)
46+
log.Infof("Request received: %+v \n", u)
4047
return c.JSON(http.StatusOK, dto.Response{
4148
Message: "created",
4249
})
@@ -47,16 +54,23 @@ func (ctr *UserController) GetByEmail(c echo.Context) error {
4754
fmt.Printf("Path param received: %+v \n", e)
4855

4956
u, err := ctr.userService.GetByEmail(context.Background(), e)
57+
fmt.Printf("Service finished, controller\n")
5058
if err != nil {
51-
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
59+
fmt.Printf("err %v\n", err)
60+
return err
5261
}
62+
5363
return c.JSON(http.StatusOK, u)
5464
}
5565

5666
func (ctr *UserController) UpdateByEmail(c echo.Context) error {
5767
u := dto.UpdateUserRequest{}
5868
if err := c.Bind(&u); err != nil {
59-
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
69+
return respKit.GenericBadRequestError(enums.ErrorRequestBodyCode, err.Error())
70+
}
71+
72+
if err := u.Validate(); err != nil {
73+
return respKit.GenericBadRequestError(enums.ErrorRequestBodyCode, err.Error())
6074
}
6175

6276
e := c.Param("email")
@@ -65,8 +79,9 @@ func (ctr *UserController) UpdateByEmail(c echo.Context) error {
6579
fmt.Printf("Path param received: %+v \n", e)
6680

6781
if err := ctr.userService.UpdateByEmail(context.Background(), u, e); err != nil {
68-
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
82+
return err
6983
}
84+
7085
return c.JSON(http.StatusOK, dto.Response{
7186
Message: "updated",
7287
})
@@ -78,8 +93,9 @@ func (ctr *UserController) DeleteByEmail(c echo.Context) error {
7893
fmt.Printf("Path param received: %+v \n", e)
7994

8095
if err := ctr.userService.DeleteByEmail(context.Background(), e); err != nil {
81-
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
96+
return err
8297
}
98+
8399
return c.JSON(http.StatusOK, dto.Response{
84100
Message: "deleted",
85101
})

enums/errors_enum.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package enums
2+
3+
const (
4+
ErrorDBDuplicatedKeyMsg = "duplicate key"
5+
ErrorRequestBodyCode = "ms_echo_go_error_request_body"
6+
ErrorEmailNotEmptyMsg = "email could not be empty"
7+
ErrorEmailNotEmptyCode = "ms_echo_go_error_email_not_empty"
8+
ErrorEmailExistsMsg = "error email %s already exists"
9+
ErrorEmailExistsCode = "ms_echo_go_error_email_exists"
10+
ErrorEmailNotFoundMsg = "error email %s not found"
11+
ErrorEmailNotFoundCode = "ms_echo_go_error_email_not_found"
12+
ErrorInsertCode = "ms_echo_go_error_inserting_user"
13+
ErrorGetByEmailCode = "ms_echo_go_error_getting_user_by_email"
14+
ErrorUpdateCode = "ms_echo_go_error_updating_user"
15+
ErrorDeleteCode = "ms_echo_go_error_deleting_user"
16+
)

go.mod

+5-9
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@ module github.com/laironacosta/ms-echo-go
33
go 1.16
44

55
require (
6-
github.com/dropbox/godropbox v0.0.0-20200228041828-52ad444d3502 // indirect
7-
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 // indirect
8-
github.com/facebookgo/stackerr v0.0.0-20150612192056-c2fcf88613f4 // indirect
96
github.com/go-pg/migrations/v8 v8.1.0
107
github.com/go-pg/pg/v10 v10.9.0
11-
github.com/juju/errors v0.0.0-20200330140219-3fe23663418f // indirect
8+
github.com/go-playground/validator/v10 v10.5.0
129
github.com/kelseyhightower/envconfig v1.4.0
13-
github.com/labstack/echo v3.3.10+incompatible
14-
github.com/labstack/gommon v0.3.0 // indirect
15-
github.com/laironacosta/kit-go v1.0.0
16-
github.com/signalfx/golib v2.5.1+incompatible
17-
github.com/smartystreets/goconvey v1.6.4 // indirect
10+
github.com/labstack/echo/v4 v4.2.2
11+
github.com/labstack/gommon v0.3.0
12+
github.com/laironacosta/kit-go v1.1.3
13+
github.com/pkg/errors v0.9.1
1814
)

0 commit comments

Comments
 (0)