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

implement ".Unwrap() error" on Error type #2525

Closed
LeGEC opened this issue Oct 9, 2020 · 4 comments
Closed

implement ".Unwrap() error" on Error type #2525

LeGEC opened this issue Oct 9, 2020 · 4 comments

Comments

@LeGEC
Copy link
Contributor

LeGEC commented Oct 9, 2020

Description

The 'Error' type defined in errors.go does not implement ".Unwrap() error", which prevents the usage of "errors.Is()" and "errors.As()"

How to reproduce

package main

import (
	"github.com/gin-gonic/gin"
)

type TestStrErr string

func (e TestStrErr) Error() string { return string(e) }

func main() {
	testErr := TestStrErr("some error")

	err := &gin.Error{Err: testErr}

	if errors.Is(err, testErr) {
	    fmt.Println("match with testErr ok")
	}
	
	var unwrap TestStrErr
	if errors.As(err, &unwrap) {
	    fmt.Println("succesfully unwrapped as a TestStrErr")
	}
}

Expectations

output :

match with testErr ok
succesfully unwrapped as a TestStrErr

Actual result

no output

Environment

  • go version: go version go1.14.2 linux/amd64
  • gin version (or commit ref): 540b1ef
  • operating system: linux
LeGEC added a commit to LeGEC/gin that referenced this issue Oct 9, 2020
@LeGEC
Copy link
Contributor Author

LeGEC commented Oct 13, 2020

A more useful use case was asked by this user on StackOverflow :
How to assert error type json.UnmarshalTypeError when caught by gin c.BindJSON ?

He wants to tell apart "this is a JSON parsing error" from "the JSON is ok, this is a validation error"

With the correct implementation of the .Unwrap() error method, this boils down to :

// gErr is a gin.Error
var jsErr *json.UnmarshalTypeError
if errors.As(gErr, &jsErr) {
    fmt.Println("the json is invalid")
} else {
    fmt.Println("this is something else")
}

@thejdavid
Copy link

It would also help for other issues for other data types such as trying to un parse an invalid time.Time. in such case it wouldn't return a json parsing error but a time parsing error.
It might be useful to differentiate / group parsing errors from binding errors.

LeGEC added a commit to LeGEC/gin that referenced this issue Oct 14, 2020
thinkerou added a commit that referenced this issue Oct 17, 2020
Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
Co-authored-by: thinkerou <thinkerou@gmail.com>
@thinkerou
Copy link
Member

#2526 merged

@jhannah-mm
Copy link

@thejdavid wrote:

It would also help for other issues for other data types such as trying to un parse an invalid time.Time. in such case it wouldn't return a json parsing error but a time parsing error.
It might be useful to differentiate / group parsing errors from binding errors.

Ya, it seems that even under 1.7.1 there's still no useful information regarding where in a JSON POST the UnmarshalType casting failure occurred...

POST {
   "start_date": "invalid_date"    // time.Time
}
parsing time ""invalid_date"" as ""2006-01-02T15:04:05Z07:00"": cannot parse "invalid_date"" as "2006"

Hoping it would say start_date... somewhere / somehow

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

No branches or pull requests

5 participants
@LeGEC @thinkerou @thejdavid @jhannah-mm and others