ardanlabs / service

Starter-kit for writing services in Go using Kubernetes.
https://www.ardanlabs.com
Apache License 2.0
3.4k stars 612 forks source link

Type assertion on errors fails on wrapped error #336

Closed mrbardia72 closed 5 months ago

mrbardia72 commented 5 months ago

https://github.com/ardanlabs/service/blob/master/foundation/validate/validate.go#L47

Convert to the following code:


    var verrors validator.ValidationErrors
        ok := errors.As(err, &verrors)
        if !ok {
            return err
        }
‍‍```
ardan-bkennedy commented 5 months ago

Yea, that is a good point. I guess the current code base doesn't not have this situation. Your PR does not have this change, so I can't accept that. I will make this change.

ardan-bkennedy commented 5 months ago

I looked at this closer. The error will never be wrapped.

if err := validate.Struct(val); err != nil {

    // Use a type assertion to get the real error value.
    verrors, ok := err.(validator.ValidationErrors)
     if !ok {
         return err
     }

Notice the error we are evaulating is coming from the validate.Struct call. This is not an error we are constructing and could be wrapped.