Skip to content

Commit

Permalink
feat: [#495] Centralize Error Messages in Framework (PR#1) (#663)
Browse files Browse the repository at this point in the history
* implement errors for framework

* chore: update mocks

* implement errors for framework

* rename contract import

* add errors for session

* rename location to module

* chore: update mocks

---------

Co-authored-by: kkumar-gcc <kkumar-gcc@users.noreply.github.com>
  • Loading branch information
kkumar-gcc and kkumar-gcc authored Oct 3, 2024
1 parent 2c6c7e7 commit 804ff1f
Show file tree
Hide file tree
Showing 11 changed files with 299 additions and 18 deletions.
11 changes: 11 additions & 0 deletions contracts/errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package errors

// Error is the interface that wraps the basic error methods
type Error interface {
// Args allows setting arguments for the placeholders in the text
Args(...any) Error
// Error implements the error interface and formats the error string
Error() string
// SetModule explicitly sets the module in the error message
SetModule(string) Error
}
63 changes: 63 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package errors

import (
"errors"
"fmt"

contractserrors "github.com/goravel/framework/contracts/errors"
)

type errorString struct {
text string
module string
args []any
}

// New creates a new error with the provided text and optional module
func New(text string, module ...string) contractserrors.Error {
err := &errorString{
text: text,
}

if len(module) > 0 {
err.module = module[0]
}

return err
}

func (e *errorString) Args(args ...any) contractserrors.Error {
e.args = args
return e
}

func (e *errorString) Error() string {
formattedText := e.text

if len(e.args) > 0 {
formattedText = fmt.Sprintf(e.text, e.args...)
}

if e.module != "" {
formattedText = fmt.Sprintf("[%s] %s", e.module, formattedText)
}

return formattedText
}

func (e *errorString) SetModule(module string) contractserrors.Error {
e.module = module
return e
}

func Is(err, target error) bool {
return errors.Is(err, target)
}

func As(err error, target any) bool {
return errors.As(err, &target)
}

func Unwrap(err error) error {
return errors.Unwrap(err)
}
12 changes: 12 additions & 0 deletions errors/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package errors

var (
ErrConfigFacadeNotSet = New("config facade is not initialized")
ErrJSONParserNotSet = New("JSON parser is not initialized")

ErrSessionNotFound = New("session [%s] not found")
ErrSessionDriverIsNotSet = New("session driver is not set")
ErrSessionDriverNotSupported = New("session driver [%s] not supported")
ErrSessionDriverAlreadyExists = New("session driver [%s] already exists")
ErrSessionDriverExtensionFailed = New("failed to extend session [%s] driver [%v]")
)
5 changes: 5 additions & 0 deletions errors/modules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package errors

var (
ModuleSession = "session"
)
185 changes: 185 additions & 0 deletions mocks/errors/Error.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions session/driver/file.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package driver

import (
"fmt"
"os"
"path/filepath"
"sync"

"github.com/goravel/framework/errors"
"github.com/goravel/framework/support/carbon"
"github.com/goravel/framework/support/file"
)
Expand Down Expand Up @@ -80,7 +80,7 @@ func (f *File) Read(id string) (string, error) {
}
}

return "", fmt.Errorf("session [%s] not found", id)
return "", errors.ErrSessionNotFound.Args(id)
}

func (f *File) Write(id string, data string) error {
Expand Down
12 changes: 6 additions & 6 deletions session/manager.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package session

import (
"fmt"
"sync"
"time"

"github.com/goravel/framework/contracts/config"
"github.com/goravel/framework/contracts/foundation"
sessioncontract "github.com/goravel/framework/contracts/session"
"github.com/goravel/framework/errors"
"github.com/goravel/framework/session/driver"
"github.com/goravel/framework/support/color"
)
Expand Down Expand Up @@ -35,7 +35,7 @@ func NewManager(config config.Config, json foundation.Json) *Manager {

func (m *Manager) BuildSession(handler sessioncontract.Driver, sessionID ...string) (sessioncontract.Session, error) {
if handler == nil {
return nil, ErrDriverNotSet
return nil, errors.ErrSessionDriverIsNotSet
}

session := m.acquireSession()
Expand All @@ -60,19 +60,19 @@ func (m *Manager) Driver(name ...string) (sessioncontract.Driver, error) {
}

if driverName == "" {
return nil, fmt.Errorf("driver is not set")
return nil, errors.ErrSessionDriverIsNotSet
}

if m.drivers[driverName] == nil {
return nil, fmt.Errorf("driver [%s] not supported", driverName)
return nil, errors.ErrSessionDriverNotSupported.Args(driverName)
}

return m.drivers[driverName], nil
}

func (m *Manager) Extend(driver string, handler func() sessioncontract.Driver) error {
if m.drivers[driver] != nil {
return fmt.Errorf("driver [%s] already exists", driver)
return errors.ErrSessionDriverAlreadyExists.Args(driver)
}
m.drivers[driver] = handler()
m.startGcTimer(m.drivers[driver])
Expand All @@ -98,7 +98,7 @@ func (m *Manager) getDefaultDriver() string {

func (m *Manager) extendDefaultDrivers() {
if err := m.Extend("file", m.createFileDriver); err != nil {
panic(fmt.Sprintf("failed to extend session file driver: %v", err))
panic(errors.ErrSessionDriverExtensionFailed.Args("file", err))
}
}

Expand Down
Loading

0 comments on commit 804ff1f

Please sign in to comment.