Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [#495] Centralize Error Messages in Framework (PR#1) #663

Merged
merged 9 commits into from
Oct 3, 2024
Merged
11 changes: 11 additions & 0 deletions contracts/errors/errors.go
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems unnecessary.

Copy link
Member Author

@kkumar-gcc kkumar-gcc Oct 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good to have, don't want to return errorString.

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)
}
Comment on lines +53 to +63
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add them when needed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is is being used at some places

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"
)
233 changes: 233 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
Loading
Loading