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

Expose error creation functions #89

Merged
merged 2 commits into from
Feb 4, 2020
Merged

Expose error creation functions #89

merged 2 commits into from
Feb 4, 2020

Conversation

nirvdrum
Copy link
Contributor

I don't know if you really want to expose this functionality or not, but it helps me address the problem I raised in #88. With this change, I can add custom middleware to do the following:

next => async req => {
  Sentry.setTag('graphql_query', req.getID())

  try {
    const res = await next(req)

    if (res.errors) {
      const error = createRequestError(req, res)
      Sentry.captureException(error)
    }

    return res
  } catch (e) {
    Sentry.captureException(e)
    throw e
  }
}

Or:

next => async req => {
  Sentry.setTag('graphql_query', req.getID())

  try {
    const res = await next(req)

    if (res.errors) {
      const errorMessage =
        req instanceof RelayNetworkLayerRequest
          ? formatGraphQLErrors(req, res.errors)
          : JSON.stringify(res.errors)
      Sentry.captureMessage(errorMessage)
    }

    return res
  } catch (e) {
    Sentry.captureException(e)
    throw e
  }
}

That conditional creation of the error message comes from the implementation of createRequestError. I don't really like that it needs to do an instanceof check, but it's unavoidable without changing the type definition for formatGraphQLErrors.

I also am not 100% certain that I've provided the correct type for RelayResponse#errors. As far as I can tell, I did. The type definition appears to match what I saw in the debugger. But, I don't know if the field was typed as any for any particular reason.

@nirvdrum nirvdrum changed the title Expose error creation in ts Expose error creation functions Jan 28, 2020
@nirvdrum
Copy link
Contributor Author

Please feel free to suggest improvements or modify the commits directly as you see fit. If you don't like this PR at all, please comment on #88 with a better option, if you have the time, and I'll attempt another PR to solve the problem.

@nodkz nodkz merged commit ab32fe6 into relay-tools:master Feb 4, 2020
@nodkz
Copy link
Collaborator

nodkz commented Feb 4, 2020

Looks good! 👍
Sorry for the late response.

@nodkz
Copy link
Collaborator

nodkz commented Feb 4, 2020

🎉 This PR is included in version 4.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@nodkz nodkz added the released label Feb 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants