Add logging across app and centralized error handling
This commit is contained in:
parent
3bbeab1199
commit
4c3f095109
10 changed files with 115 additions and 62 deletions
|
|
@ -1,7 +1,6 @@
|
|||
package link
|
||||
|
||||
import (
|
||||
. "cgnolink/err"
|
||||
"encoding/json"
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
"github.com/labstack/echo/v4"
|
||||
|
|
@ -16,7 +15,7 @@ func redirectHandler(c echo.Context, pool *pgxpool.Pool) error {
|
|||
|
||||
link, err := serv.GetById(linkId)
|
||||
if err != nil {
|
||||
return MapErrToHTTPErr(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Redirect(http.StatusSeeOther, link.RedirectURL.String())
|
||||
|
|
@ -36,7 +35,7 @@ func creationHandler(c echo.Context, pool *pgxpool.Pool) error {
|
|||
serv := PgService{rep: PgRepository{pool: pool}}
|
||||
|
||||
if err = serv.Create(entity); err != nil {
|
||||
return MapErrToHTTPErr(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusCreated)
|
||||
|
|
@ -49,7 +48,7 @@ func retrievalByIdHandler(c echo.Context, pool *pgxpool.Pool) error {
|
|||
|
||||
l, err := serv.GetById(linkId)
|
||||
if err != nil {
|
||||
return MapErrToHTTPErr(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, MapEntityToModel(l))
|
||||
|
|
@ -80,7 +79,7 @@ func allRetrievalHandler(c echo.Context, pool *pgxpool.Pool) error {
|
|||
|
||||
links, err := serv.GetAll(limit, offset)
|
||||
if err != nil {
|
||||
return MapErrToHTTPErr(err)
|
||||
return err
|
||||
}
|
||||
|
||||
models := make([]ResourceModel, len(links))
|
||||
|
|
@ -97,7 +96,7 @@ func removalHandler(c echo.Context, pool *pgxpool.Pool) error {
|
|||
serv := PgService{rep: PgRepository{pool: pool}}
|
||||
|
||||
if err := serv.DeleteById(linkId); err != nil {
|
||||
return MapErrToHTTPErr(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusNoContent)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
package link
|
||||
|
||||
import (
|
||||
. "cgnolink/err"
|
||||
apperrors "cgnolink/errors"
|
||||
)
|
||||
|
||||
type Service interface {
|
||||
|
|
@ -18,15 +18,15 @@ type PgService struct {
|
|||
func (s *PgService) Create(link *Link) error {
|
||||
existingLink, err := s.rep.FindById(link.Id)
|
||||
if err != nil {
|
||||
return UnknownError{}
|
||||
return apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
||||
if existingLink != nil {
|
||||
return AlreadyExistsError{Message: "Link with given ID already exists."}
|
||||
return apperrors.AlreadyExistsError{Message: "Link with given ID already exists."}
|
||||
}
|
||||
|
||||
if err = s.rep.Save(link); err != nil {
|
||||
return UnknownError{}
|
||||
return apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
@ -35,11 +35,11 @@ func (s *PgService) Create(link *Link) error {
|
|||
func (s *PgService) GetById(id string) (*Link, error) {
|
||||
link, err := s.rep.FindById(id)
|
||||
if err != nil {
|
||||
return nil, UnknownError{}
|
||||
return nil, apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
||||
if link == nil {
|
||||
return nil, NotFoundError{Message: "Link with given ID was not found."}
|
||||
return nil, apperrors.NotFoundError{Message: "Link with given ID was not found."}
|
||||
}
|
||||
|
||||
return link, nil
|
||||
|
|
@ -48,7 +48,7 @@ func (s *PgService) GetById(id string) (*Link, error) {
|
|||
func (s *PgService) GetAll(limit int, offset int) (Links, error) {
|
||||
links, err := s.rep.FindAll(limit, offset)
|
||||
if err != nil {
|
||||
return nil, UnknownError{}
|
||||
return nil, apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
||||
return links, nil
|
||||
|
|
@ -56,7 +56,7 @@ func (s *PgService) GetAll(limit int, offset int) (Links, error) {
|
|||
|
||||
func (s *PgService) DeleteById(id string) error {
|
||||
if err := s.rep.DeleteById(id); err != nil {
|
||||
return UnknownError{}
|
||||
return apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue