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
13 changes: 13 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,13 @@
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
// Location explicitly sets the location in the error message
Location(string) Error
// WithLocation enables or disables the inclusion of the location in the error message
WithLocation(bool) Error
}
70 changes: 70 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package errors

import (
"errors"
"fmt"

errorscontracts "github.com/goravel/framework/contracts/errors"
kkumar-gcc marked this conversation as resolved.
Show resolved Hide resolved
)

type errorString struct {
text string
location string
args []any
withLocation bool
}

// New creates a new error with the provided text and optional location
func New(text string, location ...string) errorscontracts.Error {
err := &errorString{
text: text,
withLocation: true,

Check warning on line 21 in errors/errors.go

View check run for this annotation

Codecov / codecov/patch

errors/errors.go#L18-L21

Added lines #L18 - L21 were not covered by tests
}

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

Check warning on line 25 in errors/errors.go

View check run for this annotation

Codecov / codecov/patch

errors/errors.go#L24-L25

Added lines #L24 - L25 were not covered by tests
}

return err

Check warning on line 28 in errors/errors.go

View check run for this annotation

Codecov / codecov/patch

errors/errors.go#L28

Added line #L28 was not covered by tests
}

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

Check warning on line 33 in errors/errors.go

View check run for this annotation

Codecov / codecov/patch

errors/errors.go#L31-L33

Added lines #L31 - L33 were not covered by tests
}

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

Check warning on line 37 in errors/errors.go

View check run for this annotation

Codecov / codecov/patch

errors/errors.go#L36-L37

Added lines #L36 - L37 were not covered by tests

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

Check warning on line 40 in errors/errors.go

View check run for this annotation

Codecov / codecov/patch

errors/errors.go#L39-L40

Added lines #L39 - L40 were not covered by tests
}

if e.withLocation && e.location != "" {
formattedText = fmt.Sprintf("[%s] %s", e.location, formattedText)

Check warning on line 44 in errors/errors.go

View check run for this annotation

Codecov / codecov/patch

errors/errors.go#L43-L44

Added lines #L43 - L44 were not covered by tests
}

return formattedText

Check warning on line 47 in errors/errors.go

View check run for this annotation

Codecov / codecov/patch

errors/errors.go#L47

Added line #L47 was not covered by tests
}

func (e *errorString) Location(location string) errorscontracts.Error {
e.location = location
return e

Check warning on line 52 in errors/errors.go

View check run for this annotation

Codecov / codecov/patch

errors/errors.go#L50-L52

Added lines #L50 - L52 were not covered by tests
}

func (e *errorString) WithLocation(flag bool) errorscontracts.Error {
e.withLocation = flag
return e

Check warning on line 57 in errors/errors.go

View check run for this annotation

Codecov / codecov/patch

errors/errors.go#L55-L57

Added lines #L55 - L57 were not covered by tests
}

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

Check warning on line 61 in errors/errors.go

View check run for this annotation

Codecov / codecov/patch

errors/errors.go#L60-L61

Added lines #L60 - L61 were not covered by tests
}

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

Check warning on line 65 in errors/errors.go

View check run for this annotation

Codecov / codecov/patch

errors/errors.go#L64-L65

Added lines #L64 - L65 were not covered by tests
}

func Unwrap(err error) error {
return errors.Unwrap(err)

Check warning on line 69 in errors/errors.go

View check run for this annotation

Codecov / codecov/patch

errors/errors.go#L68-L69

Added lines #L68 - L69 were not covered by tests
}
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

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.

Loading