render icon indicating copy to clipboard operation
render copied to clipboard

Make error a predefined variable for later comparision

Open rskumar opened this issue 6 years ago • 3 comments

https://github.com/go-chi/render/blob/3215478343fbc559bd3fc08f7031bb134d6bdad5/decoder.go#L30

If the returned error are defined var, it would be possible to compare and know what exactly happened. The error message from render package cannot be directly to client in most cases, but application developer would like to return some custom error.

var (
    ErrContentTypeNotSet = errors.New("render: missing content-type, unable to automatically decode the request content") 
)

would be helpful.

rskumar avatar Aug 27 '19 01:08 rskumar

https://dave.cheney.net/2016/04/07/constant-errors

ptman avatar Aug 29 '19 13:08 ptman

@ptman error interface is generic. Just Error() doesn't let me know exact what happened and can we handle it and what specifc msg we need to show to client. Even in link you shared, there are example of where to use vars like iot.EOF or sql.ErrNoRows. For me, anonymous errors.New("") are useless and I consider it as one that cannot be processed confidently other than printing to log. cockroachdb/errors repo has a very indepth comparative analysis on different mechanisms.

rskumar avatar Aug 30 '19 14:08 rskumar

You misunderstand me. I suggest not using errors.New() but instead using constant errors, like in the article, that satisfy the error interface. Those can be compared.

ptman avatar Aug 30 '19 14:08 ptman