-
Notifications
You must be signed in to change notification settings - Fork 87
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
Changes from 7 commits
198821f
3b16da6
f73d027
1470f25
5cad20f
11dd986
2b4cfc8
c5cecb2
edb7827
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can add them when needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
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]") | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package errors | ||
|
||
var ( | ||
ModuleSession = "session" | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems unnecessary.
There was a problem hiding this comment.
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
.