Skip to content

Conversation

combodga
Copy link
Owner

No description provided.

id, err := h.fetchID(c, link)
uniqueErr := false
id, err := h.fetchID(c, user, link)
if strings.Contains(fmt.Sprintf("%v", err), "shortener_short_key") {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Сравнение ошибки со строкой лишает нас преимущества проверки типов и чревато ошибками. Правильный путь проверки ошибки описан в учебнике в разделе "Интроспекция и логирование ошибок".
Тут самым простым вариантом будет создание своего типа или своей ошибки в пакете storage, например,
var ErrDupKey = fmt.Errorf("duplicate key") и проверка на ошибку на уровне хэндлера через errors.Is

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Кроме того вижу, что код дублируется в функции fetchID

user := getUser(c, h.Key)
body, err := io.ReadAll(c.Request().Body)
if err != nil {
return err
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ни в коем случае не стоит возвращать ошибки без дополнительной информации о том, где произошла ошибка. Иначе по ошибке нельзя будет локализовать место, в котором она произошла, либо на локализацию потребуются значительные усилия. Этот момент хорошо описан в учебнике в разделе "Интроспекция и логирование ошибок". Можно воспользоваться стандартным средством fmt.Errorf("read request body: %w", err) или функцией errors.Wrap из пакета github.com/pkg/errors


var id string
id, err = h.fetchID(c, user, link.OriginalURL)
if strings.Contains(fmt.Sprintf("%v", err), "shortener_short_key") {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Заменить на кастомную ошибку


err = h.Storage.SetURL(id, link)
err = h.Storage.SetURL(user, id, link)
if strings.Contains(fmt.Sprintf("%v", err), "shortener_short_key") {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Заменить на кастомную ошибку

}

func (s *Storage) Ping() bool {
db, err := sql.Open("postgres", s.DBCredentials)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Сам ping отсутствует. Нужен вызов метода db.PingContext

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants