change error format
This commit is contained in:
@@ -4,7 +4,8 @@ import (
|
||||
"astra-events/model"
|
||||
"astra-events/pkg/utils"
|
||||
"astra-events/services"
|
||||
"fmt"
|
||||
"errors"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/google/uuid"
|
||||
"github.com/hashicorp/go-set"
|
||||
"github.com/labstack/echo/v4"
|
||||
@@ -24,20 +25,39 @@ func (h *Handler) CreateEvent(c echo.Context) (err error) {
|
||||
authorId, err := uuid.Parse(c.Request().Header.Get("X-User-Id"))
|
||||
orgId, err := uuid.Parse(c.Request().Header.Get("X-User-Organization-Id"))
|
||||
if err != nil {
|
||||
return c.NoContent(http.StatusUnauthorized)
|
||||
return c.JSON(http.StatusUnauthorized, &utils.Error{
|
||||
Type: "internal_error",
|
||||
Message: "User in not authorized",
|
||||
Code: "0002",
|
||||
})
|
||||
}
|
||||
|
||||
err = c.Bind(&event)
|
||||
err = h.Validator.Struct(event)
|
||||
|
||||
if err != nil {
|
||||
return c.HTML(http.StatusBadRequest, fmt.Sprintf("Err(s):\n%+v\n", err))
|
||||
var validationErrors validator.ValidationErrors
|
||||
errors.As(err, &validationErrors)
|
||||
Fields := make(map[string]string)
|
||||
for _, validationError := range validationErrors {
|
||||
Fields[validationError.Field()] = validationError.Error()
|
||||
}
|
||||
return c.JSON(http.StatusBadRequest, &utils.ValidationError{
|
||||
Type: "validation_error",
|
||||
Message: err.Error(),
|
||||
Code: "0001",
|
||||
Fields: Fields,
|
||||
})
|
||||
}
|
||||
|
||||
if event.Person.ID.ID() != 0 {
|
||||
person, err = services.GetPerson(event.Person.ID, c.Request().Header, c)
|
||||
if err != nil || person.ID != event.Person.ID {
|
||||
return c.NoContent(http.StatusBadRequest)
|
||||
return c.JSON(http.StatusBadGateway, &utils.Error{
|
||||
Type: "internal_error",
|
||||
Message: "Can't sen request to persons service",
|
||||
Code: "0003",
|
||||
})
|
||||
}
|
||||
} else {
|
||||
newPerson := model.PersonalInformationApi{
|
||||
@@ -54,14 +74,22 @@ func (h *Handler) CreateEvent(c echo.Context) (err error) {
|
||||
|
||||
person, err = services.CreatePerson(newPerson, c.Request().Header, c)
|
||||
if err != nil || person.ID.ID() == 0 {
|
||||
return c.NoContent(http.StatusBadRequest)
|
||||
return c.JSON(http.StatusBadGateway, &utils.Error{
|
||||
Type: "internal_error",
|
||||
Message: "Can't sen request to persons service",
|
||||
Code: "0004",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
usersMap, err := services.GetUsers([]uuid.UUID{event.MedicId, authorId}, c.Request().Header, c)
|
||||
|
||||
if err != nil || len(usersMap) == 0 {
|
||||
return c.HTML(http.StatusBadGateway, "Error get users")
|
||||
return c.JSON(http.StatusBadGateway, &utils.Error{
|
||||
Type: "internal_error",
|
||||
Message: "Can't sen request to users service",
|
||||
Code: "0005",
|
||||
})
|
||||
}
|
||||
|
||||
medic := usersMap[event.MedicId]
|
||||
@@ -78,7 +106,11 @@ func (h *Handler) CreateEvent(c echo.Context) (err error) {
|
||||
tx := h.DB.Create(&newEvent)
|
||||
|
||||
if tx.RowsAffected == 0 {
|
||||
return c.NoContent(http.StatusBadGateway)
|
||||
return c.JSON(http.StatusBadGateway, &utils.Error{
|
||||
Type: "internal_error",
|
||||
Message: "Database error",
|
||||
Code: "0006",
|
||||
})
|
||||
}
|
||||
|
||||
newEvent.Author = author
|
||||
@@ -97,7 +129,11 @@ func (h *Handler) GetEvents(c echo.Context) (err error) {
|
||||
orgId, err := uuid.Parse(c.Request().Header.Get("X-User-Organization-Id"))
|
||||
|
||||
if err != nil {
|
||||
return c.NoContent(http.StatusUnauthorized)
|
||||
return c.JSON(http.StatusUnauthorized, &utils.Error{
|
||||
Type: "internal_error",
|
||||
Message: "User in not authorized",
|
||||
Code: "0002",
|
||||
})
|
||||
}
|
||||
|
||||
tx := h.DB.Where("org_id = ?", orgId)
|
||||
@@ -118,7 +154,11 @@ func (h *Handler) GetEventsToday(c echo.Context) (err error) {
|
||||
orgId, err := uuid.Parse(c.Request().Header.Get("X-User-Organization-Id"))
|
||||
|
||||
if err != nil {
|
||||
return c.NoContent(http.StatusUnauthorized)
|
||||
return c.JSON(http.StatusUnauthorized, &utils.Error{
|
||||
Type: "internal_error",
|
||||
Message: "User in not authorized",
|
||||
Code: "0002",
|
||||
})
|
||||
}
|
||||
|
||||
tx := h.DB.Where("org_id = ?", orgId)
|
||||
@@ -142,19 +182,31 @@ func (h *Handler) GetEvent(c echo.Context) error {
|
||||
orgId, err := uuid.Parse(c.Request().Header.Get("X-User-Organization-Id"))
|
||||
|
||||
if err != nil {
|
||||
return c.NoContent(http.StatusUnauthorized)
|
||||
return c.JSON(http.StatusUnauthorized, &utils.Error{
|
||||
Type: "internal_error",
|
||||
Message: "User in not authorized",
|
||||
Code: "0002",
|
||||
})
|
||||
}
|
||||
|
||||
tx := h.DB.Where("org_id = ?", orgId).First(&event, id)
|
||||
|
||||
if tx.RowsAffected == 0 {
|
||||
return c.NoContent(http.StatusNotFound)
|
||||
return c.JSON(http.StatusNotFound, &utils.Error{
|
||||
Type: "not_found_error",
|
||||
Message: "Event not found",
|
||||
Code: "0007",
|
||||
})
|
||||
}
|
||||
|
||||
usersMap, err := services.GetUsers([]uuid.UUID{event.MedicId, event.AuthorId}, c.Request().Header, c)
|
||||
|
||||
if err != nil {
|
||||
return c.HTML(http.StatusBadGateway, err.Error())
|
||||
return c.JSON(http.StatusBadGateway, &utils.Error{
|
||||
Type: "internal_error",
|
||||
Message: "Can't sen request to persons or users service",
|
||||
Code: "0005",
|
||||
})
|
||||
}
|
||||
|
||||
medic := usersMap[event.MedicId]
|
||||
@@ -176,13 +228,21 @@ func (h *Handler) DeleteEvent(c echo.Context) error {
|
||||
orgId, err := uuid.Parse(c.Request().Header.Get("X-User-Organization-Id"))
|
||||
|
||||
if err != nil {
|
||||
return c.NoContent(http.StatusUnauthorized)
|
||||
return c.JSON(http.StatusUnauthorized, &utils.Error{
|
||||
Type: "internal_error",
|
||||
Message: "User in not authorized",
|
||||
Code: "0002",
|
||||
})
|
||||
}
|
||||
|
||||
tx := h.DB.Where("org_id = ?", orgId).Delete(&model.Event{}, id)
|
||||
|
||||
if tx.RowsAffected == 0 {
|
||||
return c.NoContent(http.StatusNotFound)
|
||||
return c.JSON(http.StatusNotFound, &utils.Error{
|
||||
Type: "not_found_error",
|
||||
Message: "Event not found",
|
||||
Code: "0007",
|
||||
})
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusOK)
|
||||
@@ -204,7 +264,11 @@ func (h *Handler) queryEvents(tx *gorm.DB, c echo.Context) error {
|
||||
usersMap, err := services.GetUsers(userIds.Slice(), c.Request().Header, c)
|
||||
personsMap, err := services.GetPersons(personIds.Slice(), c.Request().Header, c)
|
||||
if err != nil {
|
||||
return c.HTML(http.StatusBadGateway, err.Error())
|
||||
return c.JSON(http.StatusBadGateway, &utils.Error{
|
||||
Type: "internal_error",
|
||||
Message: "Can't sen request to persons or users service",
|
||||
Code: "0005",
|
||||
})
|
||||
}
|
||||
|
||||
for i, event := range events {
|
||||
|
||||
14
pkg/utils/validation.go
Normal file
14
pkg/utils/validation.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package utils
|
||||
|
||||
type Error struct {
|
||||
Type string
|
||||
Message string
|
||||
Code string
|
||||
}
|
||||
|
||||
type ValidationError struct {
|
||||
Type string
|
||||
Message string
|
||||
Code string
|
||||
Fields map[string]string
|
||||
}
|
||||
Reference in New Issue
Block a user