-
Notifications
You must be signed in to change notification settings - Fork 67
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
Faulty Tolly
committed
Sep 12, 2024
1 parent
bf0e7eb
commit c7939c7
Showing
23 changed files
with
2,012 additions
and
138 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
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,52 @@ | ||
package fraud | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
// Handler is an interface that defines a method to handle faults. | ||
// Contract: should not be blocking. | ||
type Handler interface { | ||
// HandleFault handles a fault that occurred in the system. | ||
// The fault is passed as an error type. | ||
HandleFault(ctx context.Context, fault error) | ||
} | ||
|
||
type GenericHandler struct { | ||
handlers map[error]Handler | ||
} | ||
|
||
func NewGenericHandler() *GenericHandler { | ||
return &GenericHandler{ | ||
handlers: make(map[error]Handler), | ||
} | ||
} | ||
|
||
func (gh *GenericHandler) RegisterHandler(errorType error, handler Handler) error { | ||
if errorType == nil { | ||
return errors.New("error type cannot be nil") | ||
} | ||
if handler == nil { | ||
return errors.New("handler cannot be nil") | ||
} | ||
if !errors.Is(errorType, ErrFraud) { | ||
return fmt.Errorf("error type must be or wrap ErrFraud") | ||
} | ||
if _, exists := gh.handlers[errorType]; exists { | ||
return fmt.Errorf("handler for error type %v already registered", errorType) | ||
} | ||
gh.handlers[errorType] = handler | ||
return nil | ||
} | ||
|
||
func (gh *GenericHandler) HandleFault(ctx context.Context, fault error) { | ||
for errType, handler := range gh.handlers { | ||
if errors.Is(fault, errType) { | ||
handler.HandleFault(ctx, fault) | ||
return | ||
} | ||
} | ||
Check warning Code scanning / CodeQL Iteration over map Warning
Iteration over map may be a possible source of non-determinism
|
||
panic(fmt.Sprintf("No handler registered for error type: %T", fault)) | ||
} |
Oops, something went wrong.