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

[Feature] Improved Errors Type #400

Closed
Tracked by #391
MSevey opened this issue May 29, 2024 · 3 comments
Closed
Tracked by #391

[Feature] Improved Errors Type #400

MSevey opened this issue May 29, 2024 · 3 comments
Labels

Comments

@MSevey
Copy link
Member

MSevey commented May 29, 2024

Right now the way these errors are being defined and used is quite error prone, specifically as it relates to how we are using the message and params. If a developer doesn't correctly match the number of formatting directives in the message, it will panic when Error is called.

We can do better :-)

First, we should have a New method as that is much safer than manually defining the structs.

Second we should make the params more dynamic.

New Method

The new method should be something like this.

// We should also add some type safety here
type Code string

func New(code Code, msg string) (Error, error) {
	e := Error{Code: code, Message: msg}

       
	err = e.validate()
        if err != nil {
               return nil, err
        }
	return e, nil
}

func (e *Error) validate() error {
        defer func() {
            if r := recover(); r != nil {
                return fmt.Errorf("invalid Error: %v",r)
            }
        }()
        _ = e.Error()
        return nil
}

Dynamic Params

Instead of relying on a fixed formatted method, we could allow for dynamic params by expecting a key value pair.

func (e *Error) WithParams(params ...interface{}) {
	if len(params)%2 != 0 {
                // Using panic for simplicity of example, but this is a discussion point as to how to handle the error
		panic("Error: Odd number of parameters, expected key-value pairs")
	}
        e.Params = append(e.Params, params...)
        return e
}

func (e *Error) Error() string {
	msg := e.Message
	for i := 0; i < len(e.Params); i += 2 {
		key := params[i]
		value := params[i+1]
		msg = fmt.Sprintf("%s; %v: %v",msg, key, value)
	}
	if e.Err != nil {
		return fmt.Sprintf("%s; error: %v", msg, e.Err)
	}
	return msg
}
@mojtaba-esk
Copy link
Contributor

Saw your comment about creating an issue a bit late, I have applied those on the original PR.

@MSevey
Copy link
Member Author

MSevey commented May 29, 2024

Saw your comment about creating an issue a bit late, I have applied those on the original PR.

Thanks for adding the New method, that is a good start.
I think there are still other parts of this issue that we should discuss.
Lower priority though.

@smuu
Copy link
Member

smuu commented Jul 4, 2024

done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

No branches or pull requests

3 participants