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

clients/horizonclient: add IsNotFoundError #2197

Merged
merged 2 commits into from
Jan 30, 2020
Merged

clients/horizonclient: add IsNotFoundError #2197

merged 2 commits into from
Jan 30, 2020

Conversation

leighmcculloch
Copy link
Member

PR Checklist

PR Structure

  • This PR has reasonably narrow scope (if not, break it down into smaller PRs).
  • This PR avoids mixing refactoring changes with feature changes (split into two PRs
    otherwise).
  • This PR's title starts with name of package that is most changed in the PR, ex.
    services/friendbot, or all or doc if the changes are broad or impact many
    packages.

Thoroughness

  • This PR adds tests for the most critical parts of the new functionality or fixes.
  • I've updated any docs (developer docs, .md
    files, etc... affected by this change). Take a look in the docs folder for a given service,
    like this one.

Release planning

  • I've updated the relevant CHANGELOG (here for Horizon) if
    needed with deprecations, added features, breaking changes, and DB schema changes.
  • I've decided if this PR requires a new major/minor version according to
    semver, or if it's mainly a patch change. The PR is targeted at the next
    release branch if it's not a patch change.

What

Add horizonclient.IsNotFoundError that returns true if an error is a Horizon not_found problem error.

Why

It is remarkably verbose and difficult to detect if an account exists in Horizon. The code to do so must unwrap the horizonclient.Error, know to do so as a pointer and not as the pointer-less type, and then check that the problem's type is a long URI. In application code this is very verbose and easy to get wrong.

I'm writing code that tests if an account is present in the exp/services/webauth service and attempting to simplifying it.

Closes #2194

Known limitations

N/A

return false
}

return hErr.Problem.Type == "https://stellar.org/horizon-errors/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.

@leighmcculloch Is there a particular reason for using Type rather than Status? Using Status could help us prevent possible issues if we decide to change the URL or something like that. A 404 will always be a 404, unless, it can't be found.

Copy link
Member Author

@leighmcculloch leighmcculloch Jan 30, 2020

Choose a reason for hiding this comment

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

According to the Horizon documentation the important thing is that the error is a not_found error, not that the status code is 404.

When Horizon can’t find whatever data you are requesting, it will return a not_found error. This is similar to a HTTP 404 Error error response.

Ref: https://www.stellar.org/developers/horizon/reference/errors/not-found.html.

Also, according to RFC7807 the type should uniquely identify the problem, so I don't think it's something we'd ever change, or if we did it would be a breaking API change.

"type" (string) - A URI reference [RFC3986] that identifies the problem type.

Ref: https://tools.ietf.org/html/draft-ietf-appsawg-http-problem-00#section-3.1.

Copy link
Member Author

Choose a reason for hiding this comment

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

Another reason: I think ideally we use the same pattern for any error helpers like this, and others will need to use the type. If the API ever returns a 404 that doesn't fit the horizon error, then it isn't really a horizon error and something else more significant has happened. This helps to protect a client that points to the wrong URL and is getting 404s from thinking resources don't exist on Horizon.

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense! thanks for the explanation

Copy link
Contributor

@abuiles abuiles left a comment

Choose a reason for hiding this comment

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

👍

@leighmcculloch leighmcculloch merged commit 8373a72 into stellar:master Jan 30, 2020
@leighmcculloch leighmcculloch deleted the horizonclientnotfound branch January 30, 2020 00:11
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.

Feature Request: Simple detection of accounts not existing in Horizon
2 participants