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

Returned HttpError from octokit.graphql request is not of type RequestError #246

Closed
rickstaa opened this issue Jul 21, 2022 · 5 comments
Closed
Assignees

Comments

@rickstaa
Copy link

rickstaa commented Jul 21, 2022

Not sure if this is a bug or a problem with my code, but I'm trying to catch HttpErrors returned by the octokit.graphql package using the Request error class. I use the following code in this sandbox to do this:

import "./styles.css";
import dotenv from "dotenv";
import { getOctokit } from "@actions/github";
import { RequestError } from "@octokit/request-error";

dotenv.config();

const octokit = getOctokit(`${process.env.GITHUB_TOKEN}`);

export default function App() {
  const fetchGraphql = async () => {
    try {
      await octokit.graphql(
        `
        {
          repository(owner: "octokit", name: "graphql.js") {
            issues(last: 3) {
              edges {
                node {
                  title
                }
              }
            }
          }
        }
      `
      );
    } catch (error) {
      if (error instanceof RequestError) {
        console.log("A RequestError");
      } else if (error instanceof Error && error.name === "HttpError") {
        console.log("An error with name set to 'HttpError'");
      } else {
        console.log("Something else");
      }
    }
  };
  fetchGraphql();

  return (
    <div className="App">
      <h1>HttpError catch example</h1>
      <h2>The error returned from octokit is not a RequestError.</h2>
    </div>
  );
}

Unfortunately, the code always ends up at console.log("An error with name set to 'HttpError'"); statement. I, however, was under the impression that all the request errors returned by the octokit packages are of type RequestError (see https://github.com/octokit/request.js/blob/39d8b114ac1455dc0266705e546f981748935433/src/fetch-wrapper.ts#L3) and they code would end up at console.log("A RequestError"); statement.

@oscard0m oscard0m self-assigned this Jul 21, 2022
@oscard0m
Copy link
Member

oscard0m commented Jul 21, 2022

Hi @rickstaa,

I'm sorry you are experiencing this issue. Also, thanks for the detailed explanation and for taking the time of sharing a CodeSandbok. It really helps a lot for reproducing the issue and debugging ❤️ .


It seems to be a mismatch of @octokit dependencies:

Using @octokit/graphql works for me: https://codesandbox.io/s/octokit-httperror-catch-bug-forked-ptp98l?file=/src/App.tsx (just in case it unblocks you in the meantime).

I'm digging into why using @actions/github's Octokit does not work. I will update this issue with more info ASAP.

@rickstaa
Copy link
Author

@oscard0m Thanks a lot for your fast response and for providing me with the workaround. Feel free to let me know if you need more information.

@oscard0m
Copy link
Member

@oscard0m Thanks a lot for your fast response and for providing me with the workaround. Feel free to let me know if you need more information.

@actions/github@5.0.3 is using @octokit/core@3.6.0 (as you can see here).

Apparently 3.6.0 version of octokit/core (which internally uses @octokit/graphql) does not support @octokit/request-error@3.0.0 but it supports the previous version: 2.1.0

The 2 options you have here are:

  • Use @octokit/core or @octokit/graphql if you want to be up to date with latest releases
  • Use the versions of @octokit accordingly to the version that uses @actions/github

Combinations of dependencies I've checked:

It works with:


@rickstaa
Copy link
Author

@oscard0m Thanks a lot for giving me such a detailed answer. Until @actions/github@5.0.3 is updated to include a newer version of @actions/core, I will use @octokit/request-error v2.1.0.

@oscard0m
Copy link
Member

I'm glad this information has been useful @rickstaa.

I'm closing this issue but feel free to re-open if there is something else we can do.

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

No branches or pull requests

2 participants