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

Wrap original error by goa.ServiceError #3063

Merged
merged 11 commits into from Jul 16, 2022

Conversation

tchssk
Copy link
Member

@tchssk tchssk commented May 3, 2022

I tried to implement #3058.

@raphael
Copy link
Member

raphael commented May 3, 2022

This looks good, a few questions:

  • What happens for errors that do not have the err field initialized (e.g. all the validation errors)? Unwrap would return nil so As and Is would always fail? Maybe that's OK.
  • Should ServiceError implement As and Is to make it more consistent? (use err if present otherwise compare the error names if the target is also a ServiceError). This is extending a bit the intent of this change but it seems to make sense.
  • Should newError set the err field? Maybe not unless it helps with implementing As and Is.

@tchssk
Copy link
Member Author

tchssk commented May 3, 2022

Thank you for your opinion.

I added As/Is to ServiceError. I think newError doesn't need to set err. It's a shorthand only used by error create functions and the functions have no error as argument.

@raphael
Copy link
Member

raphael commented May 3, 2022

Makes sense! A couple more questions:

  • It seems As doesn't set the target when it's a ServiceError? seems like it should initialize the fields in this case? Also I believe the target might be **ServiceError, from the docs (emphasis mine):

An error matches target if the error's concrete value is assignable to the value pointed to by target, or if the error has a method As(interface{}) bool such that As(target) returns true. In the latter case, the As method is responsible for setting target.

So the usage would be:

res, err := client.SomeMethod(ctx, payload)
se := &ServiceError{name: "fault"}
if errors.As(err, &se) {
    log.Errorf(ctx, "faulted with error %s: %s", se.ID, se.Message)
}

As would have set the ID and Message fields of se (and the other fields of ServiceError as well).

  • Should Unwrap be removed now that As and Is are implemented? If not should it check whether err is nil and if so return itself (the ServiceError) instead of nil?

Thank you!

@tchssk
Copy link
Member Author

tchssk commented Jul 16, 2022

Sorry for late reply.
I reverted As/Is. Would you merge this once?

@raphael
Copy link
Member

raphael commented Jul 16, 2022

The latest change makes sense, I guess we now rely on go-multierror for supporting errors.As and errors.Is. Thank you!

@raphael raphael merged commit 3fca273 into goadesign:v3 Jul 16, 2022
@tchssk tchssk deleted the pkg-serviceerror-err branch February 24, 2023 11:52
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.

None yet

2 participants