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

Fix hardware not found response #365

Merged
merged 4 commits into from
Nov 19, 2020

Conversation

kqdeng
Copy link
Contributor

@kqdeng kqdeng commented Nov 13, 2020

Description

When getting hardware and hardware is not found, the error unexpected end of JSON input is no longer returned.
The error returned will be a hardware not found: sql: no rows in result set instead.

In addition, the error message received when trying to create a workflow with a worker/hardware not present in the database is now:

2020/11/18 21:24:01 rpc error: code = Unknown desc = failed to create workflow: unable to insert into action list: no worker found: mac: b4:96:91:5f:ac:00

Why is this needed

The error message was vague and misleading.

Error message before (as seen in boots):

boots_1        | {"level":"info","ts":1605204343.6437285,"caller":"src/dhcp.go:76","msg":"retrieved job is empty","service":"github.com/tinkerbell/boots","pkg":"main","type":"DHCPDISCOVER","mac":"f8:f2:1e:a6:cd:10","err":"discover from dhcp message: get hardware by mac from tink: rpc error: code = Unknown desc = unexpected end of JSON input","errVerbose":"rpc error: code = Unknown desc = unexpected end of JSON input\nget hardware by mac from tink\ngithub.51.al/tinkerbell/boots/packet.
...

Error message after:

boots_1        | {​​​​​​​​"level":"info","ts":1605289315.5323317,"caller":"myapp/dhcp.go:76","msg":"retrieved job is empty","service":"github.com/tinkerbell/boots","pkg":"main","type":"DHCPDISCOVER","mac":"b4:96:91:5f:ac:e0","err":"discover from dhcp message: get hardware by mac from tink: rpc error: code = Unknown desc = sql: no rows in result set","errVerbose":"rpc error: code = Unknown desc = sql: no rows in result set\nget hardware by mac from tink\ngithub.51.al/tinkerbell/boots/packet..
...

(I know, sorry for the bad formatting)

How Has This Been Tested?

Manually

How are existing users impacted? What migration steps/scripts do we need?

N/A

Checklist:

I have:

  • updated the documentation and/or roadmap (if required)
  • added unit or e2e tests
  • provided instructions on how to upgrade

@codecov
Copy link

codecov bot commented Nov 13, 2020

Codecov Report

Merging #365 (99800d5) into master (47f1e12) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #365   +/-   ##
=======================================
  Coverage   24.74%   24.74%           
=======================================
  Files          14       14           
  Lines        1277     1277           
=======================================
  Hits          316      316           
  Misses        940      940           
  Partials       21       21           
Impacted Files Coverage Δ
grpc-server/workflow.go 21.00% <ø> (ø)

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 47f1e12...99800d5. Read the comment docs.

@kqdeng kqdeng force-pushed the fix-hardware-not-found-response branch from ff43d32 to 1de2a45 Compare November 13, 2020 17:34
db/db.go Outdated
err = errors.Wrap(err, "SELECT")
if err != nil {
if err == sql.ErrNoRows {
return "", err // if sql.ErrNoRows, return error as is
Copy link
Contributor

Choose a reason for hiding this comment

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

we should wrap this error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops i missed this comment. The reason I didn't wrap this error was because sql.ErrNoRows is checked here:

if err == sql.ErrNoRows {

If I wrap the error, it becomes harder to check (unless you have any suggestions).

Copy link
Contributor

@mmlb mmlb Nov 13, 2020

Choose a reason for hiding this comment

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

ah true, that line should actually do if err.Cause() == sql.ErrNoRows { then.

db/workflow.go Outdated
@@ -108,7 +108,7 @@ func insertActionList(ctx context.Context, db *sql.DB, yamlData string, id uuid.
}

workerID, err := getWorkerID(ctx, db, task.WorkerAddr)
if err != nil {
if err != nil && err != sql.ErrNoRows {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you clear up why its ok to not return an error when err == sql.ErrNoRows, its not immediately obvious to me.
On second thought why don't we return an error in that case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh sorry it's because workerID == "" is basically the same as sql.ErrNoRows in this case. But I realize it's a better idea to switch the order of the check so it checks for ErrNoRows first and then any other error.

Copy link
Contributor

Choose a reason for hiding this comment

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

ah yes I see. Yeah lets handle ErrNoRows first&explicitly so its clearer :D 👍

@kqdeng kqdeng force-pushed the fix-hardware-not-found-response branch 2 times, most recently from 71a1c33 to d82eefd Compare November 16, 2020 15:17
@@ -49,7 +49,7 @@ func (d TinkDB) CreateTemplate(ctx context.Context, name string, data string, id
func (d TinkDB) GetTemplate(ctx context.Context, fields map[string]string) (string, string, string, error) {
getCondition, err := buildGetCondition(fields)
if err != nil {
return "", "", "", errors.Wrap(err, "failed to build get condition")
return "", "", "", errors.Wrap(err, "failed to get template")
Copy link
Contributor

Choose a reason for hiding this comment

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

this should actually be errors.WithMessage not wrap but we don't need to change that now. Using errors.Wrap on an already Wrapped error will show up with a new stack trace for each Wrap which isn't really what we want. WithMessage just adds more messages to the original stack trace.

db/workflow.go Outdated
@@ -109,9 +109,10 @@ func insertActionList(ctx context.Context, db *sql.DB, yamlData string, id uuid.

workerID, err := getWorkerID(ctx, db, task.WorkerAddr)
if err != nil {
if err == sql.ErrNoRows {
Copy link
Contributor

Choose a reason for hiding this comment

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

this should be if errors.Cause(err) == sql.ErrNoRows { right?

Copy link
Contributor Author

@kqdeng kqdeng Nov 16, 2020

Choose a reason for hiding this comment

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

Right... I put the check for errors.Cause somewhere else.. mistakenly 🤦‍♀️

db/db.go Outdated
Comment on lines 107 to 111
return "", errors.Wrap(func() error {
return func() error {
return err
}()
}(), "hardware not found")
Copy link
Contributor

Choose a reason for hiding this comment

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

what is going on here? :D. Why isn't this just return "", errors.Wrap(err, "hardware not found")?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is implementing the method for the Cause to work, basically making the sql.ErrNoRows error the "cause"

Copy link
Contributor Author

@kqdeng kqdeng Nov 16, 2020

Choose a reason for hiding this comment

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

Should I just create a global var HardwareNotFound and then check for that instead?

@kqdeng kqdeng force-pushed the fix-hardware-not-found-response branch 2 times, most recently from 0d31a6b to 09e19c1 Compare November 16, 2020 22:10
@mmlb mmlb self-requested a review November 16, 2020 22:46
Signed-off-by: Kelly Deng <kelly@packet.com>
@kqdeng kqdeng force-pushed the fix-hardware-not-found-response branch from 43d0527 to 4aab209 Compare November 18, 2020 14:32
db/workflow.go Outdated
@@ -109,9 +109,10 @@ func insertActionList(ctx context.Context, db *sql.DB, yamlData string, id uuid.

workerID, err := getWorkerID(ctx, db, task.WorkerAddr)
if err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return fmt.Errorf("hardware mentioned with reference %s not found", task.WorkerAddr)
Copy link
Contributor

Choose a reason for hiding this comment

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

why not `errors.WithMessage() here instead of dropping the stack frame and all the goodness from errors.Wrap?

…own desc = unexpected end of JSON input' error

Signed-off-by: Kelly Deng <kelly@packet.com>
@kqdeng kqdeng force-pushed the fix-hardware-not-found-response branch 2 times, most recently from bae07fb to 14c327f Compare November 18, 2020 21:34
@kqdeng kqdeng requested a review from mmlb November 18, 2020 21:55
Copy link
Contributor

@mmlb mmlb left a comment

Choose a reason for hiding this comment

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

one last change and we're done!

db/workflow.go Outdated
return get(ctx, db, query, arg)
id, err := get(ctx, db, query, arg)
if errors.Cause(err) == sql.ErrNoRows {
err = errors.WithMessagef(errors.New(mac), "mac")
Copy link
Contributor

Choose a reason for hiding this comment

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

errors.WithMessage

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops

Signed-off-by: Kelly Deng <kelly@packet.com>
Signed-off-by: Kelly Deng <kelly@packet.com>
@kqdeng kqdeng force-pushed the fix-hardware-not-found-response branch from 14c327f to 99800d5 Compare November 19, 2020 18:29
@kqdeng kqdeng added the ready-to-merge Signal to Mergify to merge the PR. label Nov 19, 2020
@mergify mergify bot merged commit a2d22a0 into tinkerbell:master Nov 19, 2020
@mmlb mmlb removed the ready-to-merge Signal to Mergify to merge the PR. label Jan 25, 2021
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.

2 participants