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

Consolidate error types into a single error type #12

Closed

Conversation

jsternberg
Copy link

@jsternberg jsternberg commented Jul 16, 2024

This consolidates the various error types to a single error type with a code that designates the type of error. This reduces the amount and complexity of the overall code.

The individual methods like IsInvalidArgument that support both errors from this package and moby errors are now moved to the compat.go file to organize them together.

@jsternberg jsternberg force-pushed the consolidate-error-types branch 2 times, most recently from 29a646d to f5b96b5 Compare July 16, 2024 15:41
errors.go Outdated
"errors"
import "context"

type code int
Copy link
Member

Choose a reason for hiding this comment

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

This could use an int8.

This type could also just be the base error type without wrapping. Then keep the error struct only when adding a custom message.

Copy link
Author

Choose a reason for hiding this comment

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

I restored the customMessage type and the wrapping code related to that and changed this type to an int8 and renamed this type to Error. This will still have the consolidated type but it should be more simple.

@jsternberg jsternberg force-pushed the consolidate-error-types branch from f5b96b5 to 2682980 Compare July 19, 2024 16:42
This consolidates the various error types to a single error type with a
code that designates the type of error. This reduces the amount and
complexity of the overall code.

The individual methods like `IsInvalidArgument` that support both errors
from this package and moby errors are now moved to the `compat.go` file
to organize them together.

Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
@jsternberg jsternberg force-pushed the consolidate-error-types branch from 2682980 to acf19c1 Compare July 22, 2024 17:08
@jsternberg jsternberg requested a review from dmcgowan July 22, 2024 17:15
}

func (c customMessage) Error() string {
return c.msg
func (m customMessage) Unwrap() error {
Copy link
Member

Choose a reason for hiding this comment

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

Why Unwrap vs using the Is and As or even keeping both?

Copy link
Author

Choose a reason for hiding this comment

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

I honestly thought I was reverting this and thought this was the original code.

I do think Unwrap() is probably better since it causes the standard library to do things properly and makes it so there's fewer things to test. While it isn't in this PR, I think customMessage is likely not needed as its own type and could be replaced by Attach or Mask from #16, but Attach and Mask provide a pattern that's more composable in other situations.

For example, say I want to annotate an existing error with an error from errdefs. I would either have to make another error type that wraps errdefs or I could utilize one of the functions in that PR and get the same behavior.

switch e := err.(type) {
case customMessage:
err = e.err
case Error:
Copy link
Member

Choose a reason for hiding this comment

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

Should keep the custom error case rather than rely on Unwrap

Copy link
Author

Choose a reason for hiding this comment

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

I suspect that keeping customMessage here when the more traditional Unwrap() works might be an anti-pattern and creates the possibility of more dead code.

Is there a reason why it would be better to keep customMessage here rather than rely on Unwrap()?

)

type Error int8
Copy link
Member

Choose a reason for hiding this comment

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

Can you rename this to errNum or ErrNum if a const must be an exported type.

@dmcgowan
Copy link
Member

Please split out the changes to customMessage, those are no longer needed with this change and could be considered separately

@AkihiroSuda
Copy link
Member

@jsternberg Could you update the PR?

@dmcgowan
Copy link
Member

dmcgowan commented Sep 1, 2024

We don't want a single error type, see #18 for explanation. If we have a major version bump of this package, the errors implementing the defined interface will guarantee that at least the error types remain consistent.

@dmcgowan dmcgowan closed this Sep 1, 2024
@jsternberg jsternberg deleted the consolidate-error-types branch September 5, 2024 20:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants