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

Proposal: Produce efficient errors instead raw panic message #5

Closed
ilgooz opened this issue Sep 24, 2015 · 2 comments
Closed

Proposal: Produce efficient errors instead raw panic message #5

ilgooz opened this issue Sep 24, 2015 · 2 comments

Comments

@ilgooz
Copy link

ilgooz commented Sep 24, 2015

I always like to check my form fields to see if their values are convertible to needed types and if not then inform the user about invalid fields. form produces *errors.errorString and this is not useful when generating error responses for this purpose.

What about defining a new error type such as below to give more control over them

e.g.

type Error []Field

func (e Error) Error() string {
  return "todo"
}

type Field struct {
  Name string // tag name e.g. time from form:"time"
  Message string // original error message 
  Type reflect.Type // point out the expected type so we can generate even more graceful error messages by checking this e.g. "time must be a UTC date"
  SliceIndex int // point out the index if an invalid Type placed in a slice except []byte
}

A dummy usage:

type Post struct {
  Time time.Time `form:"time"`
}

var p Post
err := form.DecodeValues(&p, url.Values{"time", []string{"AnInvalidDate"}})
if (err.(form.Error))[0].Type == form.TimeType {
 // so I can response a nice error message to user like:
 // HTTP 400
 // {"message": "Invalid Form Data", fields: {"time": "must be UTC formatted"}}
}

I'd like to implement this if you'll consider accepting the PR

@ajg
Copy link
Owner

ajg commented Dec 29, 2015

@ilgooz Sorry for the late response. I would not mind extending the error returned by form in the manner you propose, if you'd like to implement it. Feel free to open a PR and assign it to me.

@ajg
Copy link
Owner

ajg commented Jan 9, 2017

(I'm closing this issue due to inactivity for more than a year. Please re-open if it's still relevant. Thank you.)

@ajg ajg closed this as completed Jan 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants