Skip to content

Commit

Permalink
add recovery middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
lerenn committed Aug 27, 2023
1 parent 765d519 commit 2736e4b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/middleware/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/lerenn/asyncapi-codegen/pkg/log"
)

// Logging is a middleware that logs messages in reception and in publication
func Logging(logger log.Interface) Middleware {
return func(ctx context.Context, next Next) context.Context {
apiContext.IfEquals(ctx, apiContext.KeyIsMessageDirection, "reception", func() {
Expand Down
26 changes: 26 additions & 0 deletions pkg/middleware/recovery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package middleware

import (
"context"
"fmt"

"github.com/lerenn/asyncapi-codegen/pkg/log"
)

// Recovery is a middleware that recovers from panic in middlewares coming after
// it and user code from subscription.
func Recovery(logger log.Interface) Middleware {
return func(ctx context.Context, next Next) context.Context {
// Recover in case of panic
defer func() {
if r := recover(); r != nil {
logger.Error(ctx, fmt.Sprintf("Recovered from panic: %v", r))
}
}()

// Call next middleware
next(ctx)

return ctx
}
}
1 change: 1 addition & 0 deletions pkg/utils/duplicates.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package utils

// RemoveDuplicate removes duplicate values from a slice
func RemoveDuplicate[T string | int](sliceList []T) []T {
allKeys := make(map[T]bool)
list := []T{}
Expand Down
1 change: 1 addition & 0 deletions pkg/utils/reference.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package utils

// ToReference returns a pointer to the given value
func ToReference[T any](t T) *T {
return &t
}
1 change: 1 addition & 0 deletions pkg/utils/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import "unicode"

// UpperFirstLetter returns the given string with the first letter in uppercase
func UpperFirstLetter(str string) string {
r := []rune(str)
r[0] = unicode.ToUpper(r[0])
Expand Down
1 change: 1 addition & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package utils

// IsInSlice checks if a string is in a slice
func IsInSlice(slice []string, match string) bool {
for _, v := range slice {
if v == match {
Expand Down

0 comments on commit 2736e4b

Please sign in to comment.