Skip to content

Commit

Permalink
added Error() to return string of err interface
Browse files Browse the repository at this point in the history
  • Loading branch information
pacholoamit committed Jun 28, 2022
1 parent 28259ce commit 46b21e1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
10 changes: 5 additions & 5 deletions pkg/controllers/project.controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var Project project
func (project) GetAllProjects(c echo.Context) error {
ap, err := services.Project.GetAllProjects()
if err != nil {
return c.JSON(http.StatusBadRequest, err)
return c.JSON(http.StatusBadRequest, err.Error())
}
return c.JSON(http.StatusOK, ap)
}
Expand All @@ -29,7 +29,7 @@ func (project) CreateProject(c echo.Context) error {

cp, err := services.Project.CreateProject(pr)
if err != nil {
return c.JSON(http.StatusBadRequest, err)
return c.JSON(http.StatusBadRequest, err.Error())
}
return c.JSON(http.StatusCreated, cp)
}
Expand All @@ -39,7 +39,7 @@ func (project) GetProject(c echo.Context) error {
id, _ := strconv.Atoi(p)
gp, err := services.Project.GetProject(id)
if err != nil {
return echo.NewHTTPError(http.StatusNotFound, err)
return echo.NewHTTPError(http.StatusNotFound, err.Error())
}
return c.JSON(http.StatusOK, gp)
}
Expand All @@ -56,7 +56,7 @@ func (project) UpdateProject(c echo.Context) error {
up, err := services.Project.UpdateProject(id, pr)

if err != nil {
return echo.NewHTTPError(http.StatusNotFound, err)
return echo.NewHTTPError(http.StatusNotFound, err.Error())
}
return c.JSON(http.StatusOK, up)
}
Expand All @@ -66,7 +66,7 @@ func (project) DeleteProject(c echo.Context) error {
id, _ := strconv.Atoi(p)
dp, err := services.Project.DeleteProject(id)
if err != nil {
return echo.NewHTTPError(http.StatusNotFound, err)
return echo.NewHTTPError(http.StatusNotFound, err.Error())
}
return c.JSON(http.StatusOK, dp)
}
12 changes: 6 additions & 6 deletions pkg/controllers/task.controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ var Task task
func (task) GetAllTasks(c echo.Context) error {
at, err := services.Task.GetAllTasks()
if err != nil {
return c.JSON(http.StatusBadRequest, err)
return c.JSON(http.StatusBadRequest, err.Error())
}
return c.JSON(http.StatusOK, at)
}

func (task) CreateTask(c echo.Context) error {
t := new(models.Task)
if err := c.Bind(t); err != nil {
return err
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}

ct, err := services.Task.CreateTask(t)
if err != nil {
return c.JSON(http.StatusBadRequest, err)
return c.JSON(http.StatusBadRequest, err.Error())
}
return c.JSON(http.StatusCreated, ct)
}
Expand All @@ -39,7 +39,7 @@ func (task) GetTask(c echo.Context) error {
id, _ := strconv.Atoi(p)
gt, err := services.Task.GetTask(id)
if err != nil {
return echo.NewHTTPError(http.StatusNotFound, err)
return echo.NewHTTPError(http.StatusNotFound, err.Error())
}
return c.JSON(http.StatusOK, gt)
}
Expand All @@ -56,7 +56,7 @@ func (task) UpdateTask(c echo.Context) error {
ut, err := services.Task.UpdateTask(id, t)

if err != nil {
return echo.NewHTTPError(http.StatusNotFound, err)
return echo.NewHTTPError(http.StatusNotFound, err.Error())
}
return c.JSON(http.StatusOK, ut)
}
Expand All @@ -66,7 +66,7 @@ func (task) DeleteTask(c echo.Context) error {
id, _ := strconv.Atoi(p)
dt, err := services.Task.DeleteTask(id)
if err != nil {
return echo.NewHTTPError(http.StatusNotFound, err)
return echo.NewHTTPError(http.StatusNotFound, err.Error())
}
return c.JSON(http.StatusOK, dt)
}
5 changes: 1 addition & 4 deletions pkg/utils/validator.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package utils

import (
"net/http"

"github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
)

type CustomValidator struct {
Expand All @@ -13,7 +10,7 @@ type CustomValidator struct {

func (cv *CustomValidator) Validate(i interface{}) error {
if err := cv.Validator.Struct(i); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
return err
}
return nil
}

0 comments on commit 46b21e1

Please sign in to comment.