-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
282 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package middleware | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/bxcodec/goqueue/errors" | ||
"github.com/bxcodec/goqueue/interfaces" | ||
) | ||
|
||
// PublisherDefaultErrorMapper returns a middleware function that maps publisher errors to specific error types. | ||
// It takes a next PublisherFunc as input and returns a new PublisherFunc that performs error mapping. | ||
// If an error occurs during publishing, it will be mapped to a specific error type based on the error code. | ||
// The mapped error will be returned, or nil if no error occurred. | ||
func PublisherDefaultErrorMapper() interfaces.PublisherMiddlewareFunc { | ||
return func(next interfaces.PublisherFunc) interfaces.PublisherFunc { | ||
return func(ctx context.Context, e interfaces.Message) (err error) { | ||
err = next(ctx, e) | ||
if err != nil { | ||
switch err { | ||
case errors.ErrInvalidMessageFormat: | ||
return errors.ErrInvalidMessageFormat | ||
case errors.ErrEncodingFormatNotSupported: | ||
return errors.ErrEncodingFormatNotSupported | ||
default: | ||
return errors.Error{ | ||
Code: errors.UnKnownError, | ||
Message: err.Error(), | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
// InboundMessageHandlerDefaultErrorMapper returns a middleware function that maps specific errors to predefined error types. | ||
// It takes the next inbound message handler function as input and returns a new inbound message handler function. | ||
// The returned function checks if an error occurred during the execution of the next handler function. | ||
// If an error is found, it maps the error to a predefined error type and returns it. | ||
// If no error is found, it returns nil. | ||
func InboundMessageHandlerDefaultErrorMapper() interfaces.InboundMessageHandlerMiddlewareFunc { | ||
return func(next interfaces.InboundMessageHandlerFunc) interfaces.InboundMessageHandlerFunc { | ||
return func(ctx context.Context, m interfaces.InboundMessage) (err error) { | ||
err = next(ctx, m) | ||
if err != nil { | ||
switch err { | ||
case errors.ErrInvalidMessageFormat: | ||
return errors.ErrInvalidMessageFormat | ||
case errors.ErrEncodingFormatNotSupported: | ||
return errors.ErrEncodingFormatNotSupported | ||
default: | ||
return errors.Error{ | ||
Code: errors.UnKnownError, | ||
Message: err.Error(), | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package middleware | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/bxcodec/goqueue/interfaces" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// HelloWorldMiddlewareExecuteAfterInboundMessageHandler returns an inbound message handler middleware function. | ||
// This middleware function executes after the inbound message handler and performs additional tasks. | ||
// It logs any errors that occur during the execution of the next handler and provides an opportunity to handle them. | ||
// You can customize the error handling logic by adding your own error handler, such as sending errors to Sentry or other error tracking tools. | ||
// After error handling, it logs a message indicating that the hello-world-last-middleware has been executed. | ||
// The function signature follows the `interfaces.InboundMessageHandlerMiddlewareFunc` type. | ||
func HelloWorldMiddlewareExecuteAfterInboundMessageHandler() interfaces.InboundMessageHandlerMiddlewareFunc { | ||
return func(next interfaces.InboundMessageHandlerFunc) interfaces.InboundMessageHandlerFunc { | ||
return func(ctx context.Context, m interfaces.InboundMessage) (err error) { | ||
err = next(ctx, m) | ||
if err != nil { | ||
logrus.Error("Error: ", err, "add your custom error handler here, eg send to Sentry or other error tracking tools") | ||
} | ||
logrus.Info("hello-world-last-middleware executed") | ||
return err | ||
} | ||
} | ||
} | ||
|
||
// HelloWorldMiddlewareExecuteBeforeInboundMessageHandler is a function that returns an inbound message handler middleware. | ||
// This middleware logs a message and then calls the next inbound message handler in the chain. | ||
func HelloWorldMiddlewareExecuteBeforeInboundMessageHandler() interfaces.InboundMessageHandlerMiddlewareFunc { | ||
return func(next interfaces.InboundMessageHandlerFunc) interfaces.InboundMessageHandlerFunc { | ||
return func(ctx context.Context, m interfaces.InboundMessage) (err error) { | ||
logrus.Info("hello-world-first-middleware executed") | ||
return next(ctx, m) | ||
} | ||
} | ||
} | ||
|
||
// HelloWorldMiddlewareExecuteAfterPublisher is a function that returns a PublisherMiddlewareFunc. | ||
// It wraps the provided PublisherFunc with additional functionality to be executed after publishing a message. | ||
func HelloWorldMiddlewareExecuteAfterPublisher() interfaces.PublisherMiddlewareFunc { | ||
return func(next interfaces.PublisherFunc) interfaces.PublisherFunc { | ||
return func(ctx context.Context, m interfaces.Message) (err error) { | ||
err = next(ctx, m) | ||
if err != nil { | ||
logrus.Error("got error while publishing the message: ", err) | ||
return err | ||
} | ||
logrus.Info("hello-world-last-middleware executed") | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
// HelloWorldMiddlewareExecuteBeforePublisher is a function that returns a PublisherMiddlewareFunc. | ||
// It wraps the provided PublisherFunc with a middleware that logs a message before executing the next middleware or the actual publisher function. | ||
func HelloWorldMiddlewareExecuteBeforePublisher() interfaces.PublisherMiddlewareFunc { | ||
return func(next interfaces.PublisherFunc) interfaces.PublisherFunc { | ||
return func(ctx context.Context, e interfaces.Message) (err error) { | ||
logrus.Info("hello-world-first-middleware executed") | ||
return next(ctx, e) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,29 @@ | ||
package middleware | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/bxcodec/goqueue/interfaces" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// ApplyHandlerMiddleware applies a series of middleware functions to an inbound message handler function. | ||
// It takes an inbound message handler function `h` and a variadic list of middleware functions `middleware`. | ||
// Each middleware function is applied to the handler function in the order they are provided. | ||
// The resulting handler function with all the middleware applied is returned. | ||
func ApplyHandlerMiddleware(h interfaces.InboundMessageHandlerFunc, middlewares ...interfaces.InboundMessageHandlerMiddlewareFunc) interfaces.InboundMessageHandlerFunc { | ||
// ApplyHandlerMiddleware applies a series of middlewares to an inbound message handler function. | ||
// It takes an inbound message handler function and a variadic list of middlewares as input. | ||
// Each middleware is applied to the handler function in the order they are provided. | ||
// The resulting function is returned as the final handler function with all the middlewares applied. | ||
func ApplyHandlerMiddleware(h interfaces.InboundMessageHandlerFunc, | ||
middlewares ...interfaces.InboundMessageHandlerMiddlewareFunc) interfaces.InboundMessageHandlerFunc { | ||
for _, middleware := range middlewares { | ||
h = middleware(h) | ||
} | ||
return h | ||
} | ||
|
||
// ApplyPublisherMiddleware applies the given publisher middleware functions to the provided publisher function. | ||
// It iterates over the middleware functions and applies them in the order they are provided. | ||
// ApplyPublisherMiddleware applies a series of middlewares to a publisher function. | ||
// It takes a publisher function and a variadic list of publisher middleware functions as input. | ||
// Each middleware function is applied to the publisher function in the order they are provided. | ||
// The resulting publisher function is returned. | ||
func ApplyPublisherMiddleware(p interfaces.PublisherFunc, middlewares ...interfaces.PublisherMiddlewareFunc) interfaces.PublisherFunc { | ||
func ApplyPublisherMiddleware(p interfaces.PublisherFunc, | ||
middlewares ...interfaces.PublisherMiddlewareFunc) interfaces.PublisherFunc { | ||
for _, middleware := range middlewares { | ||
p = middleware(p) | ||
} | ||
return p | ||
} | ||
|
||
// HelloWorldMiddlewareExecuteAfterInboundMessageHandler returns an inbound message handler middleware function. | ||
// It wraps the provided `next` inbound message handler function and executes some additional logic after it. | ||
// The additional logic includes logging any error that occurred during the execution of the `next` function | ||
// and logging a message indicating that the middleware has been executed. | ||
func HelloWorldMiddlewareExecuteAfterInboundMessageHandler() interfaces.InboundMessageHandlerMiddlewareFunc { | ||
return func(next interfaces.InboundMessageHandlerFunc) interfaces.InboundMessageHandlerFunc { | ||
return func(ctx context.Context, m interfaces.InboundMessage) (err error) { | ||
err = next(ctx, m) | ||
if err != nil { | ||
logrus.Error("Error: ", err, "add your custom error handler here, eg send to Sentry or other error tracking tools") | ||
} | ||
logrus.Info("hello-world-last-middleware executed") | ||
return err | ||
} | ||
} | ||
} | ||
|
||
// HelloWorldMiddlewareExecuteBeforeInboundMessageHandler returns a middleware function that logs a message before executing the handler. | ||
func HelloWorldMiddlewareExecuteBeforeInboundMessageHandler() interfaces.InboundMessageHandlerMiddlewareFunc { | ||
return func(next interfaces.InboundMessageHandlerFunc) interfaces.InboundMessageHandlerFunc { | ||
return func(ctx context.Context, m interfaces.InboundMessage) (err error) { | ||
logrus.Info("hello-world-first-middleware executed") | ||
return next(ctx, m) | ||
} | ||
} | ||
} | ||
|
||
// HelloWorldMiddlewareExecuteAfterPublisher returns a PublisherMiddlewareFunc that executes after the publisher function. | ||
// It logs any error that occurs during publishing and logs a message indicating that the last middleware has been executed. | ||
func HelloWorldMiddlewareExecuteAfterPublisher() interfaces.PublisherMiddlewareFunc { | ||
return func(next interfaces.PublisherFunc) interfaces.PublisherFunc { | ||
return func(ctx context.Context, m interfaces.Message) (err error) { | ||
err = next(ctx, m) | ||
if err != nil { | ||
logrus.Error("got error while publishing the message: ", err) | ||
return err | ||
} | ||
logrus.Info("hello-world-last-middleware executed") | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
// HelloWorldMiddlewareExecuteBeforePublisher is a function that returns a PublisherMiddlewareFunc. | ||
// It wraps the provided PublisherFunc with a middleware that logs a message before executing the next function. | ||
func HelloWorldMiddlewareExecuteBeforePublisher() interfaces.PublisherMiddlewareFunc { | ||
return func(next interfaces.PublisherFunc) interfaces.PublisherFunc { | ||
return func(ctx context.Context, e interfaces.Message) (err error) { | ||
logrus.Info("hello-world-first-middleware executed") | ||
return next(ctx, e) | ||
} | ||
} | ||
} |
Oops, something went wrong.