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

Improve error handling using pkg/errors #201

Closed
wants to merge 1 commit into from
Closed

Conversation

dc7303
Copy link
Member

@dc7303 dc7303 commented Jun 12, 2021

What this PR does / why we need it:
In the case of zap currently in use, the place where the log is output is shown as the error occurrence point in the stack
trace. Introduced pkg/errors to allow log output to be processed at the end of the logic.

Which issue(s) this PR fixes:

Fixes #199

Special notes for your reviewer:
Changed the code that wraps using fmt.Errorf() to use errors.Wrap(). If you have a better idea than this output format, please share.

Does this PR introduce a user-facing change?:


Additional documentation:


Checklist:

  • Added relevant tests or not required
  • Didn't break anything

In the case of zap currently in use, the place where the log is
output is shown as the error occurrence point in the stack trace.
Introduced pkg/errors to allow log output to be processed
at the end of the logic.
@codecov
Copy link

codecov bot commented Jun 12, 2021

Codecov Report

Merging #201 (4faa65b) into main (9a0f843) will decrease coverage by 0.11%.
The diff coverage is 17.47%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #201      +/-   ##
==========================================
- Coverage   60.41%   60.29%   -0.12%     
==========================================
  Files          42       42              
  Lines        3544     3564      +20     
==========================================
+ Hits         2141     2149       +8     
- Misses       1207     1219      +12     
  Partials      196      196              
Impacted Files Coverage Δ
api/converter/from_bytes.go 64.42% <0.00%> (ø)
api/converter/to_bytes.go 85.82% <0.00%> (+0.67%) ⬆️
api/converter/to_pb.go 82.93% <0.00%> (ø)
client/client.go 11.90% <0.00%> (-0.24%) ⬇️
yorkie/backend/db/change_info.go 29.16% <0.00%> (+2.24%) ⬆️
yorkie/backend/db/client_info.go 53.84% <0.00%> (ø)
yorkie/backend/db/db.go 66.66% <ø> (ø)
yorkie/backend/sync/etcd/locker.go 0.00% <0.00%> (ø)
yorkie/backend/sync/etcd/membermap.go 0.00% <0.00%> (ø)
yorkie/backend/sync/etcd/pubsub.go 0.00% <0.00%> (ø)
... and 10 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9a0f843...4faa65b. Read the comment docs.

@dc7303 dc7303 requested a review from a team June 12, 2021 01:27
Copy link
Member

@hackerwins hackerwins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution.

Let me ask you a few questions.

  1. Please explain the role of errors.Wrapf, errors.WithStack, and goerrors.New and where it should be called.
  2. Where should the call to log.Logger.Errorf be?

@hackerwins hackerwins assigned dc7303 and unassigned hackerwins Jun 12, 2021
@hackerwins hackerwins requested a review from a team June 12, 2021 07:17
@dc7303
Copy link
Member Author

dc7303 commented Jun 15, 2021

Thanks for leaving a comment.

About error

goerrors.New is used when creating predefined errors. At this point, I tried to use New() of pkg/errors, but it was not used because a stack trace didn't indicate the correct error source.

errors.Wrapf and errros.WithStack were used where the error occurred. The part where these functions are used appears as the point where the error occurred in the stack trace. (Previously, we called the error log output function at the point where error occurred.)

errors.Wrapf replaces fmt.Errorf which wraps the error that occurred. And errors.WithStack is used to return an error without wrapping the error that occurred.

About log

It would be good to call log.Logger.Error from the package that the process starts. For example, it could be a Controller in the MVC model. This way we can be responsible for the error output in one place.

@dc7303
Copy link
Member Author

dc7303 commented Jun 15, 2021

There is a caveat when we use pkg/errors. Errors returned using pkg/errors should not be wrapped again in pkg/errors or using WithStack.

This is because it can cause duplicate call stacks. More details are attached at the link below.
pkg/errors#242

@dc7303 dc7303 assigned hackerwins and unassigned dc7303 Jun 16, 2021

// ErrDocumentNotAttached occurs when the given document is not attached to
// this client.
ErrDocumentNotAttached = errors.New("document is not attached")
ErrDocumentNotAttached = goerrors.New("document is not attached")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋 I came here from pkg/errors#242

I tend to use fmt.Errorf for declaring error variables so the only errors package in the codebase is github.com/pkg/errors. This way I don't have to think about which errors package I'm using, it is never std errors.

@dc7303
Copy link
Member Author

dc7303 commented Aug 12, 2021

@hackerwins I think it would be better to close this PR first and proceed again.
I'm sorry I'm requesting a review of too many changes at once. I'd like to ask for a review of small units when I start again. What do you think? It would be good to continue the discussion then.

@hackerwins
Copy link
Member

@dc7303 I agree.
I think it would be better to start with a small scope while defining the problems together.

@hackerwins hackerwins closed this Aug 13, 2021
@hackerwins hackerwins deleted the improve-error branch April 6, 2023 01:46
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

Successfully merging this pull request may close these issues.

Questions and suggestions for handling error logs
5 participants