scany icon indicating copy to clipboard operation
scany copied to clipboard

use fmt to wrap errors, not archived pkg/error

Open mrkagelui opened this issue 2 years ago • 7 comments

currently, github.com/pkg/errors is used to return error, however since go 1.13 introduced error wrapping, this is deprecated, and the repo has been archived since.

mrkagelui avatar Jul 25 '22 14:07 mrkagelui

Hi. Thanks for mentioning this; I didn't know that. Do you experience any particular problem with scany using the github.com/pkg/errors package? Although it's archived, I don't see any urgent reasons to replace it. But If it's something you would like to work on, I encourage you to shoot a PR with the change.

georgysavva avatar Jul 27 '22 15:07 georgysavva

firstly I'd say this error checking isn't very idiomatic after 1.13 release AFAIK.

it's more idiomatic to export the err, such as ErrNotFound, then users check it like errors.Is(err, scany.ErrNotFound), instead of the current scany.NotFound(err).

another major issue with pkg/errors especially WithStack is that, iirc, you can't call it more than 1 time. e.g., if I use this lib, and again wrap it with WithStack in my code, the previous stack info in this lib will be lost. or, if one day this lib needs to rely another lib which does WithStack there, the stack info there is lost. I haven't used it for quite long, feel free to correct me.

mrkagelui avatar Jul 28 '22 07:07 mrkagelui

I'll create a PR if you agree with this!

mrkagelui avatar Jul 28 '22 08:07 mrkagelui

it's more idiomatic to export the err, such as ErrNotFound, then users check it like errors.Is(err, scany.ErrNotFound), instead of the current scany.NotFound(err).

Maybe you are right, and after 1.13 errors.Is(err, scany.ErrNotFound) makes more sense. But I don't see any problems with the scany.NotFound(err) approach either.

another major issue with pkg/errors especially WithStack is that, iirc, you can't call it more than 1 time

I think you are mistaken; it captures all stacks and keeps piling them. Then, later, you unwrap them with errors.Is() or errors.As() when checking for known errors.

All in all, I am optimistic about replacing pkg/errors with the standard implementation, but I would want not to break the current scany API.

georgysavva avatar Aug 01 '22 05:08 georgysavva

Maybe you are right, and after 1.13 errors.Is(err, scany.ErrNotFound) makes more sense. But I don't see any problems with the scany.NotFound(err) approach either.

I suppose the issue is with idiom. users new to scany wouldn't know they need this to check for a certain error

All in all, I am optimistic about replacing pkg/errors with the standard implementation, but I would want not to break the current scany API.

sure, we can have both, i.e., export the ErrNotFound, wrap them, and change the scany.NotFound(err) with checks like errors.Is(err, ErrNotFound) (with the standard lib errors) , how does that sound to you?

mrkagelui avatar Aug 01 '22 06:08 mrkagelui

I suppose the issue is with idiom. users new to scany wouldn't know they need this to check for a certain error

We could solve it by providing the documentation. Besides that, the new users, after not founding any exported sentinel error in the library, would most likely discover the function with the corresponding name.

sure, we can have both, i.e., export the ErrNotFound, wrap them, and change the scany.NotFound(err) with checks like errors.Is(err, ErrNotFound) (with the standard lib errors) , how does that sound to you?

Sounds good!

georgysavva avatar Aug 01 '22 08:08 georgysavva

We could solve it by providing the documentation. Besides that, the new users, after not founding any exported sentinel error in the library, would most likely discover the function with the corresponding name.

Haha, exactly what I did. 😃

sure, we can have both, i.e., export the ErrNotFound, wrap them, and change the scany.NotFound(err) with checks like errors.Is(err, ErrNotFound) (with the standard lib errors) , how does that sound to you?

Sounds good!

I'll shoot a PR for a small subset of errors, if you'd find that appropriate, will do it for all.

mrkagelui avatar Aug 01 '22 09:08 mrkagelui

+1 on switching to stdlib, i use a framework that check errors with errors.Is and translates to a corresponding API error. Incompatible with the current approach, need to add some hacks around it

steeling avatar Aug 20 '22 00:08 steeling

Here is the new release with stdlib errors, thanks to @mrkagelui: https://github.com/georgysavva/scany/releases/tag/v1.2.0 cc: @steeling

georgysavva avatar Sep 05 '22 16:09 georgysavva