[PR #2062] [MERGED] refactor: centralize HTTP error handling #9742

Closed
opened 2026-04-23 09:11:14 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/go-vikunja/vikunja/pull/2062
Author: @kolaente
Created: 1/7/2026
Status: Merged
Merged: 1/8/2026
Merged by: @kolaente

Base: mainHead: fix-invalid-data-details


📝 Commits (10+)

  • ef55dd6 fix: return invalid fields when returning validation errors
  • 04c0645 refactor: centralize HTTP error handling
  • 874ccd8 refactor: simplify handlers to return raw errors
  • da1941c refactor: add ErrInvalidModel type for bind errors
  • 59aa315 fix: wrap string error messages in consistent JSON structure
  • 69b8d29 fix: return 400 for invalid avatar size parameter
  • c6bd657 fix: correct TOTP disable success message
  • 4aa2cc5 refactor: remove redundant return statements in web handlers
  • 650dbb3 fix: add defensive slice length check in error response test
  • 6da9c73 fix: handle HTTPErrorProcessor in getHTTPErrorMessage helper

📊 Changes

42 files changed (+750 additions, -447 deletions)

View changed files

📝 pkg/models/error.go (+57 -0)
📝 pkg/modules/auth/openid/openid.go (+5 -6)
📝 pkg/modules/background/handler/background.go (+12 -13)
📝 pkg/modules/background/unsplash/proxy.go (+2 -3)
📝 pkg/modules/migration/handler/common.go (+2 -3)
📝 pkg/modules/migration/handler/handler.go (+3 -4)
📝 pkg/modules/migration/handler/handler_file.go (+4 -5)
📝 pkg/routes/api/v1/avatar.go (+8 -9)
📝 pkg/routes/api/v1/link_sharing_auth.go (+4 -5)
📝 pkg/routes/api/v1/login.go (+12 -13)
📝 pkg/routes/api/v1/task_attachment.go (+36 -14)
📝 pkg/routes/api/v1/user_caldav_token.go (+7 -8)
📝 pkg/routes/api/v1/user_confirm_email.go (+2 -3)
📝 pkg/routes/api/v1/user_deletion.go (+14 -15)
📝 pkg/routes/api/v1/user_export.go (+10 -11)
📝 pkg/routes/api/v1/user_list.go (+7 -8)
📝 pkg/routes/api/v1/user_password_reset.go (+4 -5)
📝 pkg/routes/api/v1/user_register.go (+4 -5)
📝 pkg/routes/api/v1/user_settings.go (+13 -14)
📝 pkg/routes/api/v1/user_show.go (+2 -3)

...and 22 more files

📄 Description

Summary

  • Create centralized HTTP error handler in pkg/routes/error_handler.go that converts all error types to proper HTTP responses
  • Simplify all handlers to return raw domain errors instead of wrapping with HandleHTTPError
  • Add ErrInvalidModel type (code 2004) for consistent bind/parse error handling
  • Update test helpers to handle both echo.HTTPError and domain error types

Changes

  • 51 files changed, ~714 insertions, ~595 deletions
  • Removed HandleHTTPError function from pkg/web/handler/helper.go
  • Updated ~199 call sites across 28 handler files
  • Added comprehensive error response tests in pkg/webtests/error_responses_test.go

Benefits

  • Single source of truth for error → HTTP response conversion
  • Handlers are simpler - just return errors
  • Consistent error responses across all endpoints
  • Easier to add new error types - just implement interfaces

Test Plan

  • All web tests pass
  • All feature tests pass
  • Linter passes (0 issues)
  • Error response formats verified (validation, bind, 404, 403, 401)

Summary by CodeRabbit

  • New Features

    • Added structured error response handling with field-level validation details
    • Enhanced error responses to include HTTP status codes and detailed error codes
  • Bug Fixes

    • Improved error propagation and consistency across API endpoints
    • Fixed error response formatting for validation and domain errors
  • Refactor

    • Centralized HTTP error handling mechanism
    • Simplified error handling patterns across multiple API routes and modules

✏️ Tip: You can customize this high-level summary in your review settings.

🐰 Hop along, dear errors, no more wrapping today!
We've centralized handlers in a shiny new way,
From scattered to unified, the code flows so clean,
ValidationHTTPError marshals JSON supreme!
Direct propagation hops forward with glee,
A refactor so grand—what a sight to see! 🎉


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/go-vikunja/vikunja/pull/2062 **Author:** [@kolaente](https://github.com/kolaente) **Created:** 1/7/2026 **Status:** ✅ Merged **Merged:** 1/8/2026 **Merged by:** [@kolaente](https://github.com/kolaente) **Base:** `main` ← **Head:** `fix-invalid-data-details` --- ### 📝 Commits (10+) - [`ef55dd6`](https://github.com/go-vikunja/vikunja/commit/ef55dd673ffb52fc0cc71f7ea30b99536e3d4ac0) fix: return invalid fields when returning validation errors - [`04c0645`](https://github.com/go-vikunja/vikunja/commit/04c064570c0759023559177b23bb6b9fc09fe000) refactor: centralize HTTP error handling - [`874ccd8`](https://github.com/go-vikunja/vikunja/commit/874ccd80798f80630f354fae8294183e5357d0a5) refactor: simplify handlers to return raw errors - [`da1941c`](https://github.com/go-vikunja/vikunja/commit/da1941c5c42004c2a6eeaeca242d76125398256f) refactor: add ErrInvalidModel type for bind errors - [`59aa315`](https://github.com/go-vikunja/vikunja/commit/59aa315b273adb392182081a12d7978d5cf1be9d) fix: wrap string error messages in consistent JSON structure - [`69b8d29`](https://github.com/go-vikunja/vikunja/commit/69b8d2943c84477217740cf784964bfa441da33a) fix: return 400 for invalid avatar size parameter - [`c6bd657`](https://github.com/go-vikunja/vikunja/commit/c6bd657040508946d8bd1f30baf47462b2a47630) fix: correct TOTP disable success message - [`4aa2cc5`](https://github.com/go-vikunja/vikunja/commit/4aa2cc5a3669389a44cf20171a5e973d20ec325d) refactor: remove redundant return statements in web handlers - [`650dbb3`](https://github.com/go-vikunja/vikunja/commit/650dbb352a5b951055dc9ee8e5427dc4fb6a7297) fix: add defensive slice length check in error response test - [`6da9c73`](https://github.com/go-vikunja/vikunja/commit/6da9c73eb6c65d28b350680760ecd3541d35bf1e) fix: handle HTTPErrorProcessor in getHTTPErrorMessage helper ### 📊 Changes **42 files changed** (+750 additions, -447 deletions) <details> <summary>View changed files</summary> 📝 `pkg/models/error.go` (+57 -0) 📝 `pkg/modules/auth/openid/openid.go` (+5 -6) 📝 `pkg/modules/background/handler/background.go` (+12 -13) 📝 `pkg/modules/background/unsplash/proxy.go` (+2 -3) 📝 `pkg/modules/migration/handler/common.go` (+2 -3) 📝 `pkg/modules/migration/handler/handler.go` (+3 -4) 📝 `pkg/modules/migration/handler/handler_file.go` (+4 -5) 📝 `pkg/routes/api/v1/avatar.go` (+8 -9) 📝 `pkg/routes/api/v1/link_sharing_auth.go` (+4 -5) 📝 `pkg/routes/api/v1/login.go` (+12 -13) 📝 `pkg/routes/api/v1/task_attachment.go` (+36 -14) 📝 `pkg/routes/api/v1/user_caldav_token.go` (+7 -8) 📝 `pkg/routes/api/v1/user_confirm_email.go` (+2 -3) 📝 `pkg/routes/api/v1/user_deletion.go` (+14 -15) 📝 `pkg/routes/api/v1/user_export.go` (+10 -11) 📝 `pkg/routes/api/v1/user_list.go` (+7 -8) 📝 `pkg/routes/api/v1/user_password_reset.go` (+4 -5) 📝 `pkg/routes/api/v1/user_register.go` (+4 -5) 📝 `pkg/routes/api/v1/user_settings.go` (+13 -14) 📝 `pkg/routes/api/v1/user_show.go` (+2 -3) _...and 22 more files_ </details> ### 📄 Description ## Summary - Create centralized HTTP error handler in `pkg/routes/error_handler.go` that converts all error types to proper HTTP responses - Simplify all handlers to return raw domain errors instead of wrapping with `HandleHTTPError` - Add `ErrInvalidModel` type (code 2004) for consistent bind/parse error handling - Update test helpers to handle both `echo.HTTPError` and domain error types ## Changes - **51 files changed**, ~714 insertions, ~595 deletions - Removed `HandleHTTPError` function from `pkg/web/handler/helper.go` - Updated ~199 call sites across 28 handler files - Added comprehensive error response tests in `pkg/webtests/error_responses_test.go` ## Benefits - Single source of truth for error → HTTP response conversion - Handlers are simpler - just return errors - Consistent error responses across all endpoints - Easier to add new error types - just implement interfaces ## Test Plan - [x] All web tests pass - [x] All feature tests pass - [x] Linter passes (0 issues) - [x] Error response formats verified (validation, bind, 404, 403, 401) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added structured error response handling with field-level validation details * Enhanced error responses to include HTTP status codes and detailed error codes * **Bug Fixes** * Improved error propagation and consistency across API endpoints * Fixed error response formatting for validation and domain errors * **Refactor** * Centralized HTTP error handling mechanism * Simplified error handling patterns across multiple API routes and modules <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> 🐰 Hop along, dear errors, no more wrapping today! We've centralized handlers in a shiny new way, From scattered to unified, the code flows so clean, ValidationHTTPError marshals JSON supreme! Direct propagation hops forward with glee, A refactor so grand—what a sight to see! 🎉 <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-23 09:11:14 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/vikunja#9742