[PR #1031] [MERGED] servemux: NotFoundHandler returns ErrHandlerNotFound error #3007

Closed
opened 2026-03-15 21:15:16 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/hibiken/asynq/pull/1031
Author: @dnaeon
Created: 3/7/2025
Status: Merged
Merged: 11/4/2025
Merged by: @kamikazechaser

Base: masterHead: feat/err-not-found


📝 Commits (1)

  • 0eead63 servemux: NotFoundHandler returns ErrHandlerNotFound error

📊 Changes

1 file changed (+5 additions, -1 deletions)

View changed files

📝 servemux.go (+5 -1)

📄 Description

The asynq.NotFoundHandler returns an error, which wraps asynq.ErrHandlerNotFound error.

This allows the use of middlewares, which can then inspect the returned error and decide to silence them, thus avoiding a retry.

Example middleware, which ignores handler not found for task.

func NewIgnoreHandlerNotFoundMiddleware() asynq.MiddlewareFunc {
	middleware := func(handler asynq.Handler) asynq.Handler {
		mw := func(ctx context.Context, task *asynq.Task) error {
			err := handler.ProcessTask(ctx, task)
			if errors.Is(err, asynq.ErrHandlerNotFound) {
				slog.Info("ignoring task", "reason", asynq.ErrHandlerNotFound.Error())
				return nil
			}
			return err
		}
		return asynq.HandlerFunc(mw)
	}

	return asynq.MiddlewareFunc(middleware)
}

Use this middleware with a multiplexer.

...
mux := asynq.NewServeMux()
mux.Use(NewIgnoreHandlerNotFoundMiddleware())
...
server.Run(mux)

🔄 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/hibiken/asynq/pull/1031 **Author:** [@dnaeon](https://github.com/dnaeon) **Created:** 3/7/2025 **Status:** ✅ Merged **Merged:** 11/4/2025 **Merged by:** [@kamikazechaser](https://github.com/kamikazechaser) **Base:** `master` ← **Head:** `feat/err-not-found` --- ### 📝 Commits (1) - [`0eead63`](https://github.com/hibiken/asynq/commit/0eead6348ab5c4ac65b3901315bc1b99a508a462) servemux: NotFoundHandler returns ErrHandlerNotFound error ### 📊 Changes **1 file changed** (+5 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `servemux.go` (+5 -1) </details> ### 📄 Description The `asynq.NotFoundHandler` returns an error, which wraps `asynq.ErrHandlerNotFound` error. This allows the use of middlewares, which can then inspect the returned error and decide to silence them, thus avoiding a retry. Example middleware, which ignores `handler not found for task`. ```go func NewIgnoreHandlerNotFoundMiddleware() asynq.MiddlewareFunc { middleware := func(handler asynq.Handler) asynq.Handler { mw := func(ctx context.Context, task *asynq.Task) error { err := handler.ProcessTask(ctx, task) if errors.Is(err, asynq.ErrHandlerNotFound) { slog.Info("ignoring task", "reason", asynq.ErrHandlerNotFound.Error()) return nil } return err } return asynq.HandlerFunc(mw) } return asynq.MiddlewareFunc(middleware) } ``` Use this middleware with a multiplexer. ``` go ... mux := asynq.NewServeMux() mux.Use(NewIgnoreHandlerNotFoundMiddleware()) ... server.Run(mux) ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-03-15 21:15:16 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/asynq#3007
No description provided.