Skip to content

feat: add handler wrapper#5

Merged
itsLeonB merged 1 commit into
mainfrom
dev
Oct 3, 2025
Merged

feat: add handler wrapper#5
itsLeonB merged 1 commit into
mainfrom
dev

Conversation

@itsLeonB

@itsLeonB itsLeonB commented Oct 3, 2025

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • New Features
    • Optional Git pre-push checks (lint and tests) with simple install/uninstall via Make commands.
  • Documentation
    • Help output updated to include and describe the new Make targets with clearer formatting.
  • Refactor
    • Standardized API response handling and error propagation through a unified HTTP handler wrapper, improving consistency of JSON responses.
  • Chores
    • Added Make targets to manage the pre-push hook lifecycle, including setup and removal steps with informative messages.

@coderabbitai

coderabbitai Bot commented Oct 3, 2025

Copy link
Copy Markdown

Walkthrough

Adds Makefile targets to install/uninstall a Git pre-push hook, introduces a shell script to run lint and tests before pushing, and adds a Gin helper function to wrap handlers that return status, message, data, and error into a gin.HandlerFunc.

Changes

Cohort / File(s) Summary of Changes
Git hook tooling
Makefile, scripts/git-pre-push.sh
Add install-pre-push-hook and uninstall-pre-push-hook targets; update .PHONY and help text; implement shell script that runs make lint then make test, aborting on failure and printing status messages.
HTTP handler utility
utils.go
Add WrapHandler(func(ctx *gin.Context) (int, string, any, error)) gin.HandlerFunc to execute handler, record error in context on failure, or respond with JSON and provided status code.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Dev as Developer
  participant Git as Git
  participant Hook as pre-push hook (script)
  participant Make as make

  Dev->>Git: git push
  Git->>Hook: invoke pre-push
  Hook->>Make: make lint
  alt Lint fails
    Hook-->>Git: exit 1 (abort push)
  else Lint passes
    Hook->>Make: make test
    alt Tests fail
      Hook-->>Git: exit 1 (abort push)
    else Tests pass
      Hook-->>Git: exit 0 (continue push)
      Git-->>Dev: push proceeds
    end
  end
Loading
sequenceDiagram
  autonumber
  participant C as Client
  participant Gin as Gin Router
  participant Wrap as WrapHandler
  participant H as Handler(ctx)->(status,msg,data,err)

  C->>Gin: HTTP request
  Gin->>Wrap: route match
  Wrap->>H: invoke with ctx
  alt err != nil
    Wrap->>Gin: set error in context
    Gin-->>C: framework error handling response
  else success
    Wrap-->>C: JSON {message,data} with status code
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I nudged the hooks with twitching nose,
Pre-push checks before code goes.
Lint then tests—thump-thump, all clear!
Gin wraps handlers, tidy, dear.
In burrows deep, our CI sings,
Hop, commit, and spread your wings. 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title “feat: add handler wrapper” accurately describes the addition of the WrapHandler utility but omits the Makefile and pre-push hook script changes, making it only partially representative of the full changeset.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch dev

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
utils.go (1)

92-93: Consider explicit error response.

When the handler returns an error, only ctx.Error(err) is called without sending a JSON response. Depending on your error handling middleware, this might leave the client without a proper error response body.

Consider sending an explicit error response:

 	return func(ctx *gin.Context) {
 		if statusCode, message, response, err := handler(ctx); err != nil {
 			_ = ctx.Error(err)
+			ctx.JSON(statusCode, NewResponse(message).WithData(response))
 		} else {
 			ctx.JSON(statusCode, NewResponse(message).WithData(response))
 		}
 	}

Or if you want different response structure for errors, use a dedicated error response helper.

scripts/git-pre-push.sh (1)

5-17: Use printf for better POSIX portability.

Lines 5, 11, and 17 use echo "\n..." for newlines. The #!/bin/sh shebang targets POSIX shell, where echo doesn't reliably interpret escape sequences like \n across all implementations. Some systems will print literal \n characters instead of newlines.

Apply this diff for better portability:

-echo "\n=== Running linting ==="
+printf "\n=== Running linting ===\n"
 if ! make lint; then
     echo "❌ Linting failed! Please fix the issues before pushing."
     exit 1
 fi
 
-echo "\n=== Running tests ==="
+printf "\n=== Running tests ===\n"
 if ! make test; then
     echo "❌ Tests failed! Please fix the test issues before pushing."
     exit 1
 fi
 
-echo "\n✅ All checks passed! Pushing can continue...\n"
+printf "\n✅ All checks passed! Pushing can continue...\n\n"
Makefile (1)

40-45: Consider verifying source script exists.

The install-pre-push-hook target doesn't verify that scripts/git-pre-push.sh exists before attempting to copy it. If the script is missing, cp will fail with an error message, but adding an explicit check could provide clearer feedback.

Apply this diff to add a check:

 install-pre-push-hook:
 	@echo "Installing pre-push git hook..."
+	@test -f scripts/git-pre-push.sh || (echo "Error: scripts/git-pre-push.sh not found" && exit 1)
 	@mkdir -p .git/hooks
 	@cp scripts/git-pre-push.sh .git/hooks/pre-push
 	@chmod +x .git/hooks/pre-push
 	@echo "Pre-push hook installed successfully!"
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0703b91 and df78b4b.

📒 Files selected for processing (3)
  • Makefile (2 hunks)
  • scripts/git-pre-push.sh (1 hunks)
  • utils.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
utils.go (1)
responses.go (1)
  • NewResponse (39-43)
🪛 checkmake (0.2.2)
Makefile

[warning] 3-3: Target body for "help" exceeds allowed length of 5 (10).

(maxbodylength)


[warning] 1-1: Missing required phony target "all"

(minphony)


[warning] 1-1: Missing required phony target "clean"

(minphony)

🔇 Additional comments (4)
scripts/git-pre-push.sh (1)

1-17: LGTM with a portability note.

The pre-push hook logic is sound and properly enforces quality checks before pushing. The error handling with exit codes is correct.

Makefile (2)

1-1: LGTM!

The .PHONY declaration correctly includes all the new targets.


40-50: LGTM!

The install and uninstall targets are well-implemented:

  • Properly creates the hooks directory if it doesn't exist
  • Sets correct executable permissions
  • Provides clear user feedback
  • Clean uninstall with rm -f (won't error if file doesn't exist)
utils.go (1)

90-98: Clarify error response handling via global middleware
WrapHandler rightly uses ctx.Error(err) and defers to the registered errorMiddleware—which calls ctx.Errors.Last() and ctx.AbortWithStatusJSON—to send structured JSON error responses. No explicit JSON in WrapHandler is needed as long as the error middleware is applied outermost.

@itsLeonB itsLeonB merged commit 546295e into main Oct 3, 2025
10 checks passed
@coderabbitai coderabbitai Bot mentioned this pull request Dec 17, 2025
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