mirror of
https://github.com/H0llyW00dzZ/fiber2fa.git
synced 2025-02-06 11:02:32 +00:00
Improve [2FA] Error Message (#15)
- [+] feat(error.go): add global error variables for common error cases - [+] refactor(middleware.go): replace inline errors with global error variables - [+] refactor(storage.go): replace inline errors with global error variables - [+] refactor(storage.go): wrap errors using fmt.Errorf for ErrorFailedToDeleteInfo and ErrorFailedToResetStorage
This commit is contained in:
parent
91cfbb252a
commit
574a30a003
20
error.go
Normal file
20
error.go
Normal file
@ -0,0 +1,20 @@
|
||||
// Copyright (c) 2024 H0llyW00dz All rights reserved.
|
||||
//
|
||||
// License: BSD 3-Clause License
|
||||
|
||||
package twofa
|
||||
|
||||
import "errors"
|
||||
|
||||
// Global error variables
|
||||
var (
|
||||
ErrorFailedToRetrieveInfo = errors.New("failed to retrieve 2FA information")
|
||||
ErrorFailedToUnmarshalInfo = errors.New("failed to unmarshal 2FA information")
|
||||
ErrorFailedToMarshalInfo = errors.New("failed to marshal updated 2FA information")
|
||||
ErrorFailedToStoreInfo = errors.New("failed to store updated 2FA information")
|
||||
ErrorFailedToDeleteInfo = errors.New("failed to delete 2FA information")
|
||||
ErrorFailedToResetStorage = errors.New("failed to reset storage")
|
||||
ErrorFailedToCloseStorage = errors.New("failed to close storage")
|
||||
ErrorContextKeyNotSet = errors.New("ContextKey is not set")
|
||||
ErrorFailedToRetrieveContextKey = errors.New("failed to retrieve context key")
|
||||
)
|
@ -5,7 +5,6 @@
|
||||
package twofa
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -110,12 +109,12 @@ func (m *Middleware) isPathSkipped(path string) bool {
|
||||
// getContextKey retrieves the context key from c.Locals using the provided ContextKey.
|
||||
func (m *Middleware) getContextKey(c *fiber.Ctx) (string, error) {
|
||||
if m.Config.ContextKey == "" {
|
||||
return "", fmt.Errorf("ContextKey is not set")
|
||||
return "", ErrorContextKeyNotSet
|
||||
}
|
||||
|
||||
contextKeyValue, ok := c.Locals(m.Config.ContextKey).(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("failed to retrieve context key")
|
||||
return "", ErrorFailedToRetrieveContextKey
|
||||
}
|
||||
|
||||
return contextKeyValue, nil
|
||||
|
14
storage.go
14
storage.go
@ -55,7 +55,7 @@ func (i *Info) SetExpirationTime(expiration time.Time) {
|
||||
func (m *Middleware) getInfoFromStorage(contextKey string) (*Info, error) {
|
||||
rawInfo, err := m.Config.Storage.Get(contextKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to retrieve 2FA information")
|
||||
return nil, ErrorFailedToRetrieveInfo
|
||||
}
|
||||
|
||||
if rawInfo == nil {
|
||||
@ -64,7 +64,7 @@ func (m *Middleware) getInfoFromStorage(contextKey string) (*Info, error) {
|
||||
|
||||
var info Info
|
||||
if err := m.Config.JSONUnmarshal(rawInfo, &info); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal 2FA information")
|
||||
return nil, ErrorFailedToUnmarshalInfo
|
||||
}
|
||||
|
||||
return &info, nil
|
||||
@ -74,12 +74,12 @@ func (m *Middleware) getInfoFromStorage(contextKey string) (*Info, error) {
|
||||
func (m *Middleware) updateInfoInStorage(contextKey string, info *Info) error {
|
||||
updatedRawInfo, err := m.Config.JSONMarshal(info)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal updated 2FA information")
|
||||
return ErrorFailedToMarshalInfo
|
||||
}
|
||||
|
||||
err = m.Config.Storage.Set(contextKey, updatedRawInfo, time.Duration(m.Config.CookieMaxAge)*time.Second)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to store updated 2FA information")
|
||||
return ErrorFailedToStoreInfo
|
||||
}
|
||||
|
||||
return nil
|
||||
@ -89,7 +89,7 @@ func (m *Middleware) updateInfoInStorage(contextKey string, info *Info) error {
|
||||
func (m *Middleware) deleteInfoFromStorage(contextKey string) error {
|
||||
err := m.Config.Storage.Delete(contextKey)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete 2FA information: %w", err)
|
||||
return fmt.Errorf("%w: %v", ErrorFailedToDeleteInfo, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
@ -99,7 +99,7 @@ func (m *Middleware) deleteInfoFromStorage(contextKey string) error {
|
||||
func (m *Middleware) resetStorage() error {
|
||||
err := m.Config.Storage.Reset()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to reset storage: %w", err)
|
||||
return fmt.Errorf("%w: %v", ErrorFailedToResetStorage, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
@ -109,7 +109,7 @@ func (m *Middleware) resetStorage() error {
|
||||
func (m *Middleware) closeStorage() error {
|
||||
err := m.Config.Storage.Close()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to close storage: %w", err)
|
||||
return ErrorFailedToCloseStorage
|
||||
}
|
||||
|
||||
return nil
|
||||
|
Loading…
x
Reference in New Issue
Block a user