Skip to content

🩹 Fix error handling#1010

Merged
H1rono merged 1 commit intov2from
fix-error
Feb 23, 2026
Merged

🩹 Fix error handling#1010
H1rono merged 1 commit intov2from
fix-error

Conversation

@H1rono
Copy link
Copy Markdown
Member

@H1rono H1rono commented Feb 23, 2026

#998 の漏れ

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • エラー処理ロジックの改善により、エラーハンドリングの一貫性が向上しました。
  • Refactor

    • 内部エラーハンドリングを簡潔化し、コード保守性を向上させました。
    • エラーログに詳細情報を追加し、トラブルシューティングを容易にしました。

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Feb 23, 2026

📝 Walkthrough

ウォークスルー

ent固有のエラーハンドリング(ent.IsNotFound、ent.IsConstraintError)を削除し、エラーハンドリングを簡素化しました。GetFile、GetFileMeta、DeleteFileおよび認証処理でエラーを元々の形式で直接返すようにし、未使用のentインポートを削除してログにファイルIDを追加しました。

変更内容

コホート / ファイル(s) 概要
ルータのエラーハンドリング簡素化
internal/router/file.go, internal/router/middleware.go
ent固有のエラー判定分岐を削除し、エラーを元々の形式で直接返すよう単純化。GetFile、GetFileMeta、DeleteFileでのエラーログにファイルIDを追加。CheckLoginMiddlewareではコンテキストから派生したロガーの作成を廃止し、エラーパス処理を簡潔化。未使用のentインポートを削除。

推定コードレビュー作業量

🎯 2 (Simple) | ⏱️ ~12分

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive タイトルは絵文字を含むノイズがあり、変更の具体的な内容を明確に伝えていません。エラーハンドリングの修正という曖昧な説明となっています。 タイトルを具体的に修正してください。例えば「Remove ent.IsNotFound and error wrapping from router file and middleware」のように、実際の変更内容を明確に説明してください。
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-error

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@internal/router/file.go`:
- Around line 172-178: GetFile のエラーパスは GetFileMeta と同様に DB エラーなど全ての失敗を単に
logger.Info で「file not found in repository」と記録しているため、ログレベルと文言を修正してください —
h.Repository.GetFile 呼び出し後のエラーハンドリングで logger.Info を logger.Error
に変更し、メッセージを「failed to get file from repository」や「failed to get file
metadata」など失敗を示す文言にしつつ zap.String("ID", fileID.String()) と zap.Error(err)
を含める(GetFileMeta の該当箇所と同様の形式に合わせる)ことで、実際の DB エラーを適切なレベルで記録してください.
- Around line 118-124: GetFile error handling currently logs every error as info
"file not found in repository" and returns the raw error; change it to
distinguish ent.IsNotFound(err) from other errors: when ent.IsNotFound(err) keep
the Info-level "file not found" log (including fileID) and return the
appropriate not-found error, but for all other errors log with logger.Error
(include fileID and the error) and return a wrapped unexpected error (e.g.
NewUnexpectedError) so DB/infra failures are surfaced at error level; update
handling around h.Repository.GetFile, logger.Info/logger.Error, ent.IsNotFound
and the return path instead of returning the raw err.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1208d26 and dc49b6b.

📒 Files selected for processing (2)
  • internal/router/file.go
  • internal/router/middleware.go

Comment on lines 118 to 124
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
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

ログメッセージとレベルがすべてのエラーに誤適用されている

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
Verify each finding against the current code and only fix it if needed.

In `@internal/router/file.go` around lines 118 - 124, GetFile error handling
currently logs every error as info "file not found in repository" and returns
the raw error; change it to distinguish ent.IsNotFound(err) from other errors:
when ent.IsNotFound(err) keep the Info-level "file not found" log (including
fileID) and return the appropriate not-found error, but for all other errors log
with logger.Error (include fileID and the error) and return a wrapped unexpected
error (e.g. NewUnexpectedError) so DB/infra failures are surfaced at error
level; update handling around h.Repository.GetFile, logger.Info/logger.Error,
ent.IsNotFound and the return path instead of returning the raw err.

Comment on lines 172 to 178
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
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

GetFile エラーパスと同じ:ログメッセージ・レベルが誤り

GetFileMeta でも GetFile のエラーパスと同様に、DB エラーを含むすべての失敗に対して logger.Info("file not found in repository", ...) が記録されます。

🔧 修正案
 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

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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
}
file, err := h.Repository.GetFile(ctx, fileID)
if err != nil {
logger.Error(
"failed to get file from repository",
zap.String("ID", fileID.String()), zap.Error(err))
return err
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@internal/router/file.go` around lines 172 - 178, GetFile のエラーパスは GetFileMeta
と同様に DB エラーなど全ての失敗を単に logger.Info で「file not found in
repository」と記録しているため、ログレベルと文言を修正してください — h.Repository.GetFile 呼び出し後のエラーハンドリングで
logger.Info を logger.Error に変更し、メッセージを「failed to get file from
repository」や「failed to get file metadata」など失敗を示す文言にしつつ zap.String("ID",
fileID.String()) と zap.Error(err) を含める(GetFileMeta の該当箇所と同様の形式に合わせる)ことで、実際の DB
エラーを適切なレベルで記録してください.

@codecov
Copy link
Copy Markdown

codecov bot commented Feb 23, 2026

Codecov Report

❌ Patch coverage is 92.85714% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 7.29%. Comparing base (1208d26) to head (dc49b6b).
⚠️ Report is 2 commits behind head on v2.

Files with missing lines Patch % Lines
internal/router/middleware.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##              v2   #1010      +/-   ##
========================================
+ Coverage   7.24%   7.29%   +0.05%     
========================================
  Files        118     118              
  Lines      16931   16924       -7     
========================================
+ Hits        1226    1235       +9     
+ Misses     15620   15607      -13     
+ Partials      85      82       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@H1rono
Copy link
Copy Markdown
Member Author

H1rono commented Feb 23, 2026

うっせーーーーーー

@H1rono H1rono merged commit e9e7d75 into v2 Feb 23, 2026
9 checks passed
@H1rono H1rono deleted the fix-error branch February 23, 2026 23:55
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.

1 participant