From 0eead6348ab5c4ac65b3901315bc1b99a508a462 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Nikolov Date: Fri, 7 Mar 2025 12:58:08 +0200 Subject: [PATCH] servemux: NotFoundHandler returns ErrHandlerNotFound error This allows asynq middlewares to be used to inspect returned errors and decide to silence errors about `handlers not found for task'. --- servemux.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/servemux.go b/servemux.go index 411dcf8..26bbf44 100644 --- a/servemux.go +++ b/servemux.go @@ -6,12 +6,16 @@ package asynq import ( "context" + "errors" "fmt" "sort" "strings" "sync" ) +// ErrHandlerNotFound indicates that no task handler was found for a given pattern. +var ErrHandlerNotFound = errors.New("handler not found for task") + // ServeMux is a multiplexer for asynchronous tasks. // It matches the type of each task against a list of registered patterns // and calls the handler for the pattern that most closely matches the @@ -149,7 +153,7 @@ func (mux *ServeMux) Use(mws ...MiddlewareFunc) { // NotFound returns an error indicating that the handler was not found for the given task. func NotFound(ctx context.Context, task *Task) error { - return fmt.Errorf("handler not found for task %q", task.Type()) + return fmt.Errorf("%w %q", ErrHandlerNotFound, task.Type()) } // NotFoundHandler returns a simple task handler that returns a ``not found`` error.