Skip to content

Commit

Permalink
Merge pull request #566 from traPtitech/fix/gorm-error-handling
Browse files Browse the repository at this point in the history
♻️ only log db error
  • Loading branch information
ras0q authored Aug 3, 2023
2 parents e053305 + ad12f2b commit 7c9b6bc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
9 changes: 8 additions & 1 deletion infrastructure/sqlhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ const (

func (h *SQLHandler) Error() error {
err := h.conn.Error
if err == nil {
return nil
}

// TODO: use correct context
h.conn.Logger.Error(context.TODO(), err.Error())

if errors.Is(err, gorm.ErrRecordNotFound) {
return repository.ErrNotFound
}
Expand All @@ -156,7 +163,7 @@ func (h *SQLHandler) Error() error {
return repository.ErrInvalidArg
}

return err
return repository.ErrDBInternal
}

// Interface guards
Expand Down
2 changes: 2 additions & 0 deletions interfaces/handler/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func newHTTPErrorHandler(e *echo.Echo) echo.HTTPErrorHandler {
case errors.Is(err, repository.ErrNotFound):
code = http.StatusNotFound

case errors.Is(err, repository.ErrDBInternal):
fallthrough
default:
e.Logger.Error(err)
code = http.StatusInternalServerError
Expand Down
2 changes: 2 additions & 0 deletions usecases/repository/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ var (
ErrAlreadyExists = errors.New("already exists")
// ErrInvalidArg argument error
ErrInvalidArg = errors.New("argument error")
// ErrDBInternal database internal error
ErrDBInternal = errors.New("database internal error")
// ErrBind failed to bind request
ErrBind = errors.New("bind error")
// ErrValidate failed to validate request
Expand Down

0 comments on commit 7c9b6bc

Please sign in to comment.