eminetto / clean-architecture-go-v2

Clean Architecture sample
1.28k stars 228 forks source link

Invalid memory access on book validator fail #8

Open devadomas opened 3 years ago

devadomas commented 3 years ago

Hey,

First of all, nice work, I really loved your implementation clean architecture, since I'm huge fan of Uncle Bob's. I do implement and follow those rulesets in my node projects.

Anyway, onto the problem. F.e. when making request with an invalid quantity value, validator throws an error. Error is handled on service, but in incorrect way.

Code snipped from area of issue:

func (s *Service) CreateBook(title string, author string, pages int, quantity int) (entity.ID, error) {
    b, err := entity.NewBook(title, author, pages, quantity)
    if err != nil {
        return b.ID, err
    }
    return s.repo.Create(b)
}

When validator throws an error, it sets return values to nil, error, then on service function tries to access unaccessible area(nil in our case) and throws runtime error.

This can be fixed by not accessing unaccessible element(b.ID).

Cheers, Adam.