Skip to content

Commit

Permalink
Merge pull request #41 from LuigiAzevedo/improve-book-errors
Browse files Browse the repository at this point in the history
refactor: improve book errors
  • Loading branch information
LuigiAzevedo committed May 22, 2023
2 parents 29df058 + 2ae4929 commit 1b80f90
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 38 deletions.
7 changes: 4 additions & 3 deletions internal/database/repository/book_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"

"github.com/LuigiAzevedo/public-library-v2/internal/domain/entity"
"github.com/LuigiAzevedo/public-library-v2/internal/errs"
r "github.com/LuigiAzevedo/public-library-v2/internal/ports/repository"
)

Expand Down Expand Up @@ -35,7 +36,7 @@ func (r *bookRepository) Get(ctx context.Context, id int) (*entity.Book, error)
var updatedAt sql.NullTime
err = row.Scan(&b.ID, &b.Title, &b.Author, &b.Amount, &updatedAt, &b.CreatedAt)
if err != nil {
return nil, err
return nil, errors.New(errs.ErrBookNotFound)
}

// check if updated_at is NULL before scanning it
Expand Down Expand Up @@ -81,7 +82,7 @@ func (r *bookRepository) List(ctx context.Context) ([]*entity.Book, error) {
}

if books == nil {
return nil, sql.ErrNoRows
return nil, errors.New(errs.ErrBookNotFound)
}

return books, nil
Expand Down Expand Up @@ -122,7 +123,7 @@ func (r *bookRepository) Search(ctx context.Context, query string) ([]*entity.Bo
}

if books == nil {
return nil, sql.ErrNoRows
return nil, errors.New(errs.ErrBookNotFound)
}

return books, nil
Expand Down
13 changes: 6 additions & 7 deletions internal/delivery/http/book_handler.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package handler

import (
"database/sql"
"encoding/json"
"errors"
"io"
"net/http"
"strconv"

"github.com/go-chi/chi"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"

"github.com/LuigiAzevedo/public-library-v2/internal/domain/entity"
Expand Down Expand Up @@ -52,7 +51,7 @@ func (h *bookHandler) GetBook(w http.ResponseWriter, r *http.Request) {
case <-ctx.Done():
http.Error(w, errs.ErrTimeout, http.StatusGatewayTimeout)
default:
if errors.Is(err, sql.ErrNoRows) {
if errors.Unwrap(err).Error() == errs.ErrBookNotFound {
http.Error(w, errs.ErrBookNotFound, http.StatusNotFound)
} else {
http.Error(w, errs.ErrGetBook, http.StatusInternalServerError)
Expand All @@ -79,7 +78,7 @@ func (h *bookHandler) SearchBooks(w http.ResponseWriter, r *http.Request) {
var req SearchBookRequest

err := json.NewDecoder(r.Body).Decode(&req)
if err != nil && !errors.Is(err, io.EOF) {
if err != nil && err != io.EOF {
log.Error().Msg(err.Error())
http.Error(w, errs.ErrWrongBodyTitle, http.StatusBadRequest)
return
Expand All @@ -100,7 +99,7 @@ func (h *bookHandler) SearchBooks(w http.ResponseWriter, r *http.Request) {
case <-ctx.Done():
http.Error(w, errs.ErrTimeout, http.StatusGatewayTimeout)
default:
if errors.Is(err, sql.ErrNoRows) {
if errors.Unwrap(err).Error() == errs.ErrBookNotFound {
http.Error(w, errs.ErrBookNotFound, http.StatusNotFound)
} else {
http.Error(w, errs.ErrSearchBook, http.StatusInternalServerError)
Expand Down Expand Up @@ -179,7 +178,7 @@ func (h *bookHandler) UpdateBook(w http.ResponseWriter, r *http.Request) {
case <-ctx.Done():
http.Error(w, errs.ErrTimeout, http.StatusGatewayTimeout)
default:
if errors.Cause(err).Error() == errs.ErrBookNotFound {
if errors.Unwrap(err).Error() == errs.ErrBookNotFound {
http.Error(w, errs.ErrBookNotFound, http.StatusNotFound)
} else {
http.Error(w, errs.ErrUpdateBook, http.StatusInternalServerError)
Expand Down Expand Up @@ -208,7 +207,7 @@ func (h *bookHandler) DeleteBook(w http.ResponseWriter, r *http.Request) {
case <-ctx.Done():
http.Error(w, errs.ErrTimeout, http.StatusGatewayTimeout)
default:
if errors.Cause(err).Error() == errs.ErrBookNotFound {
if errors.Unwrap(err).Error() == errs.ErrBookNotFound {
http.Error(w, errs.ErrBookNotFound, http.StatusNotFound)
} else {
http.Error(w, errs.ErrDeleteBook, http.StatusInternalServerError)
Expand Down
16 changes: 8 additions & 8 deletions internal/delivery/http/book_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestGetBook(t *testing.T) {
uc.EXPECT().
GetBook(gomock.Any(), gomock.Any()).
Times(1).
Return(nil, sql.ErrNoRows)
Return(nil, fmt.Errorf("%s: %w", errs.ErrGetBook, errors.New(errs.ErrBookNotFound)))
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
assert.Equal(t, http.StatusNotFound, recorder.Code)
Expand All @@ -66,7 +66,7 @@ func TestGetBook(t *testing.T) {
uc.EXPECT().
GetBook(gomock.Any(), gomock.Any()).
Times(1).
Return(nil, sql.ErrConnDone)
Return(nil, fmt.Errorf("%s: %w", errs.ErrGetBook, sql.ErrConnDone))
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
assert.Equal(t, http.StatusInternalServerError, recorder.Code)
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestSearchBooks(t *testing.T) {
uc.EXPECT().
ListBooks(gomock.Any()).
Times(1).
Return(nil, sql.ErrNoRows)
Return(nil, fmt.Errorf("%s: %w", errs.ErrGetBook, errors.New(errs.ErrBookNotFound)))
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
assert.Equal(t, http.StatusNotFound, recorder.Code)
Expand All @@ -164,7 +164,7 @@ func TestSearchBooks(t *testing.T) {
uc.EXPECT().
ListBooks(gomock.Any()).
Times(1).
Return(nil, sql.ErrConnDone)
Return(nil, fmt.Errorf("%s: %w", errs.ErrGetBook, sql.ErrConnDone))
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
assert.Equal(t, http.StatusInternalServerError, recorder.Code)
Expand Down Expand Up @@ -327,7 +327,7 @@ func TestUpdateBook(t *testing.T) {
uc.EXPECT().
UpdateBook(gomock.Any(), gomock.Any()).
Times(1).
Return(errors.New(errs.ErrBookNotFound))
Return(fmt.Errorf("%s: %w", errs.ErrGetBook, errors.New(errs.ErrBookNotFound)))
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
assert.Equal(t, http.StatusNotFound, recorder.Code)
Expand All @@ -340,7 +340,7 @@ func TestUpdateBook(t *testing.T) {
uc.EXPECT().
UpdateBook(gomock.Any(), gomock.Any()).
Times(1).
Return(sql.ErrConnDone)
Return(fmt.Errorf("%s: %w", errs.ErrGetBook, sql.ErrConnDone))
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
assert.Equal(t, http.StatusInternalServerError, recorder.Code)
Expand Down Expand Up @@ -409,7 +409,7 @@ func TestDeleteBook(t *testing.T) {
uc.EXPECT().
DeleteBook(gomock.Any(), gomock.Any()).
Times(1).
Return(errors.New(errs.ErrBookNotFound))
Return(fmt.Errorf("%s: %w", errs.ErrGetBook, errors.New(errs.ErrBookNotFound)))
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
assert.Equal(t, http.StatusNotFound, recorder.Code)
Expand All @@ -421,7 +421,7 @@ func TestDeleteBook(t *testing.T) {
uc.EXPECT().
DeleteBook(gomock.Any(), gomock.Any()).
Times(1).
Return(sql.ErrConnDone)
Return(fmt.Errorf("%s: %w", errs.ErrGetBook, sql.ErrConnDone))
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
assert.Equal(t, http.StatusInternalServerError, recorder.Code)
Expand Down
19 changes: 9 additions & 10 deletions internal/domain/usecase/book_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package usecase

import (
"context"
"fmt"
"time"

"github.com/pkg/errors"

"github.com/LuigiAzevedo/public-library-v2/internal/domain/entity"
"github.com/LuigiAzevedo/public-library-v2/internal/errs"
r "github.com/LuigiAzevedo/public-library-v2/internal/ports/repository"
Expand All @@ -26,7 +25,7 @@ func NewBookUseCase(repository r.BookRepository) u.BookUsecase {
func (s *bookUseCase) GetBook(ctx context.Context, id int) (*entity.Book, error) {
book, err := s.bookRepo.Get(ctx, id)
if err != nil {
return nil, errors.Wrap(err, errs.ErrGetBook)
return nil, fmt.Errorf("%s: %w", errs.ErrGetBook, err)
}

return book, nil
Expand All @@ -35,7 +34,7 @@ func (s *bookUseCase) GetBook(ctx context.Context, id int) (*entity.Book, error)
func (s *bookUseCase) SearchBooks(ctx context.Context, query string) ([]*entity.Book, error) {
books, err := s.bookRepo.Search(ctx, query)
if err != nil {
return nil, errors.Wrap(err, errs.ErrSearchBook)
return nil, fmt.Errorf("%s: %w", errs.ErrSearchBook, err)
}

return books, nil
Expand All @@ -44,7 +43,7 @@ func (s *bookUseCase) SearchBooks(ctx context.Context, query string) ([]*entity.
func (s *bookUseCase) ListBooks(ctx context.Context) ([]*entity.Book, error) {
books, err := s.bookRepo.List(ctx)
if err != nil {
return nil, errors.Wrap(err, errs.ErrSearchBook)
return nil, fmt.Errorf("%s: %w", errs.ErrSearchBook, err)
}

return books, nil
Expand All @@ -53,12 +52,12 @@ func (s *bookUseCase) ListBooks(ctx context.Context) ([]*entity.Book, error) {
func (s *bookUseCase) CreateBook(ctx context.Context, b *entity.Book) (int, error) {
book, err := entity.NewBook(b.Title, b.Author, b.Amount)
if err != nil {
return 0, errors.Wrap(err, errs.ErrCreateBook)
return 0, fmt.Errorf("%s: %w", errs.ErrCreateBook, err)
}

id, err := s.bookRepo.Create(ctx, book)
if err != nil {
return 0, errors.Wrap(err, errs.ErrCreateBook)
return 0, fmt.Errorf("%s: %w", errs.ErrCreateBook, err)
}

return id, nil
Expand All @@ -69,12 +68,12 @@ func (s *bookUseCase) UpdateBook(ctx context.Context, b *entity.Book) error {

err := b.Validate()
if err != nil {
return errors.Wrap(err, errs.ErrUpdateBook)
return fmt.Errorf("%s: %w", errs.ErrUpdateBook, err)
}

err = s.bookRepo.Update(ctx, b)
if err != nil {
return errors.Wrap(err, errs.ErrUpdateBook)
return fmt.Errorf("%s: %w", errs.ErrUpdateBook, err)
}

return nil
Expand All @@ -83,7 +82,7 @@ func (s *bookUseCase) UpdateBook(ctx context.Context, b *entity.Book) error {
func (s *bookUseCase) DeleteBook(ctx context.Context, id int) error {
err := s.bookRepo.Delete(ctx, id)
if err != nil {
return errors.Wrap(err, errs.ErrDeleteBook)
return fmt.Errorf("%s: %w", errs.ErrDeleteBook, err)
}

return nil
Expand Down
10 changes: 5 additions & 5 deletions internal/domain/usecase/book_usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package usecase

import (
"context"
"database/sql"
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"

"github.com/LuigiAzevedo/public-library-v2/internal/domain/entity"
"github.com/LuigiAzevedo/public-library-v2/internal/errs"
"github.com/LuigiAzevedo/public-library-v2/internal/mock"
)

Expand All @@ -24,7 +24,7 @@ func TestGetBook(t *testing.T) {
})
t.Run("Not Found", func(t *testing.T) {
_, err := uc.GetBook(ctx, 0)
assert.Equal(t, sql.ErrNoRows, errors.Cause(err))
assert.Equal(t, errs.ErrBookNotFound, errors.Unwrap(err).Error())
})
}

Expand All @@ -40,7 +40,7 @@ func TestSearchBooks(t *testing.T) {
})
t.Run("Not Found", func(t *testing.T) {
_, err := uc.SearchBooks(ctx, "five")
assert.Equal(t, sql.ErrNoRows, errors.Cause(err))
assert.Equal(t, errs.ErrBookNotFound, errors.Unwrap(err).Error())
})
}

Expand Down Expand Up @@ -103,7 +103,7 @@ func TestUpdateBook(t *testing.T) {
}

err := uc.UpdateBook(ctx, b)
assert.Equal(t, sql.ErrNoRows, errors.Cause(err))
assert.Equal(t, errs.ErrBookNotFound, errors.Unwrap(err).Error())
})
}

Expand All @@ -119,6 +119,6 @@ func TestDeleteBook(t *testing.T) {

t.Run("Not Found", func(t *testing.T) {
err := uc.DeleteBook(ctx, 5)
assert.Equal(t, sql.ErrNoRows, errors.Cause(err))
assert.Equal(t, errs.ErrBookNotFound, errors.Unwrap(err).Error())
})
}
11 changes: 6 additions & 5 deletions internal/mock/book_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package mock

import (
"context"
"database/sql"
"errors"
"strings"
"time"

"github.com/LuigiAzevedo/public-library-v2/internal/domain/entity"
"github.com/LuigiAzevedo/public-library-v2/internal/errs"
ports "github.com/LuigiAzevedo/public-library-v2/internal/ports/repository"
)

Expand Down Expand Up @@ -42,7 +43,7 @@ func (r *mockBookRepository) Get(ctx context.Context, id int) (*entity.Book, err
}
}

return nil, sql.ErrNoRows
return nil, errors.New(errs.ErrBookNotFound)
}

func (r *mockBookRepository) List(ctx context.Context) ([]*entity.Book, error) {
Expand All @@ -59,7 +60,7 @@ func (r *mockBookRepository) Search(ctx context.Context, query string) ([]*entit
}

if len(result) == 0 {
return nil, sql.ErrNoRows
return nil, errors.New(errs.ErrBookNotFound)
}

return result, nil
Expand All @@ -82,7 +83,7 @@ func (r *mockBookRepository) Update(ctx context.Context, b *entity.Book) error {
}
}

return sql.ErrNoRows
return errors.New(errs.ErrBookNotFound)
}

func (r *mockBookRepository) Delete(ctx context.Context, id int) error {
Expand All @@ -93,5 +94,5 @@ func (r *mockBookRepository) Delete(ctx context.Context, id int) error {
}
}

return sql.ErrNoRows
return errors.New(errs.ErrBookNotFound)
}

0 comments on commit 1b80f90

Please sign in to comment.