-
Notifications
You must be signed in to change notification settings - Fork 3
🩹 Fix error handling #1010
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🩹 Fix error handling #1010
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,7 +8,6 @@ import ( | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| "github.com/google/uuid" | ||||||||||||||||||||||||||||||||||||||||||
| "github.com/labstack/echo/v5" | ||||||||||||||||||||||||||||||||||||||||||
| "github.com/traPtitech/Jomon/internal/ent" | ||||||||||||||||||||||||||||||||||||||||||
| "github.com/traPtitech/Jomon/internal/logging" | ||||||||||||||||||||||||||||||||||||||||||
| "github.com/traPtitech/Jomon/internal/service" | ||||||||||||||||||||||||||||||||||||||||||
| "go.uber.org/zap" | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -118,12 +117,10 @@ func (h Handlers) GetFile(c *echo.Context) error { | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| file, err := h.Repository.GetFile(ctx, fileID) | ||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||
| if ent.IsNotFound(err) { | ||||||||||||||||||||||||||||||||||||||||||
| logger.Info("could not find file in repository", zap.String("ID", fileID.String())) | ||||||||||||||||||||||||||||||||||||||||||
| return echo.ErrNotFound.Wrap(err) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| logger.Error("failed to get file from repository", zap.Error(err)) | ||||||||||||||||||||||||||||||||||||||||||
| return service.NewUnexpectedError(err) | ||||||||||||||||||||||||||||||||||||||||||
| logger.Info( | ||||||||||||||||||||||||||||||||||||||||||
| "file not found in repository", | ||||||||||||||||||||||||||||||||||||||||||
| zap.String("ID", fileID.String()), zap.Error(err)) | ||||||||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| modifiedAt := file.CreatedAt.Truncate(time.Second) | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -174,12 +171,10 @@ func (h Handlers) GetFileMeta(c *echo.Context) error { | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| file, err := h.Repository.GetFile(ctx, fileID) | ||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||
| if ent.IsNotFound(err) { | ||||||||||||||||||||||||||||||||||||||||||
| logger.Info("could not find file in repository", zap.String("ID", fileID.String())) | ||||||||||||||||||||||||||||||||||||||||||
| return echo.ErrNotFound.Wrap(err) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| logger.Error("failed to get file from repository", zap.Error(err)) | ||||||||||||||||||||||||||||||||||||||||||
| return service.NewUnexpectedError(err) | ||||||||||||||||||||||||||||||||||||||||||
| logger.Info( | ||||||||||||||||||||||||||||||||||||||||||
| "file not found in repository", | ||||||||||||||||||||||||||||||||||||||||||
| zap.String("ID", fileID.String()), zap.Error(err)) | ||||||||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
172
to
178
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🔧 修正案 file, err := h.Repository.GetFile(ctx, fileID)
if err != nil {
- logger.Info(
- "file not found in repository",
- zap.String("ID", fileID.String()), zap.Error(err))
- return err
+ logger.Error(
+ "failed to get file from repository",
+ zap.String("ID", fileID.String()), zap.Error(err))
+ return err
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return c.JSON(http.StatusOK, &FileMetaResponse{ | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -208,17 +203,16 @@ func (h Handlers) DeleteFile(c *echo.Context) error { | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| err = h.Repository.DeleteFile(ctx, fileID) | ||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||
| if ent.IsConstraintError(err) { | ||||||||||||||||||||||||||||||||||||||||||
| logger.Info("constraint error while deleting file", zap.Error(err)) | ||||||||||||||||||||||||||||||||||||||||||
| return echo.ErrBadRequest.Wrap(err) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| logger.Error("failed to delete file in repository", zap.Error(err)) | ||||||||||||||||||||||||||||||||||||||||||
| logger.Error("failed to delete file in repository", | ||||||||||||||||||||||||||||||||||||||||||
| zap.String("ID", fileID.String()), zap.Error(err)) | ||||||||||||||||||||||||||||||||||||||||||
| return service.NewUnexpectedError(err) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| err = h.Storage.Delete(ctx, fileID.String()) | ||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||
| logger.Error("failed to delete file in storage", zap.Error(err)) | ||||||||||||||||||||||||||||||||||||||||||
| logger.Error( | ||||||||||||||||||||||||||||||||||||||||||
| "failed to delete file in storage", | ||||||||||||||||||||||||||||||||||||||||||
| zap.String("ID", fileID.String()), zap.Error(err)) | ||||||||||||||||||||||||||||||||||||||||||
| return service.NewUnexpectedError(err) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ログメッセージとレベルがすべてのエラーに誤適用されている
GetFileが返すエラーは「ファイルが存在しない」ケースに限りません。DB 接続失敗やクエリエラーが発生した場合でもlogger.Info("file not found in repository", ...)が記録されてしまい、実際のエラー内容を誤解させます。以前はent.IsNotFoundの場合のみこのメッセージが使われ、それ以外は別経路(logger.Error+NewUnexpectedError)で処理されていました。DB 障害を
Infoレベルで "file not found" として記録すると、インフラ問題の検知が遅れます。🔧 修正案
file, err := h.Repository.GetFile(ctx, fileID) if err != nil { - logger.Info( - "file not found in repository", - zap.String("ID", fileID.String()), zap.Error(err)) - return err + logger.Error( + "failed to get file from repository", + zap.String("ID", fileID.String()), zap.Error(err)) + return err }また
return errで生の ent エラーが返ることによる HTTP ステータスのマッピングについては、middleware.go側のコメントで生成した確認スクリプトで合わせて検証してください。🤖 Prompt for AI Agents