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 by adding types for Octokit error #156

Closed
sorenlouv opened this issue Aug 5, 2020 · 4 comments · Fixed by octokit/types.ts#147
Closed

Improve error handling by adding types for Octokit error #156

sorenlouv opened this issue Aug 5, 2020 · 4 comments · Fixed by octokit/types.ts#147

Comments

@sorenlouv
Copy link
Contributor

sorenlouv commented Aug 5, 2020

Hi there,

I'm trying to handle the errors from Octokit but I'm not able to find docs (or better: types) for the errors that can be thrown.
As an example, this is how I hoped I'd be able to get a typed error:

try {
    const octokit = new Octokit(...);
    return await octokit.search.commits(...);
} catch(e) {
  if(e instanceof OctokitError) {
    // `e` will now be typed
  }
}

But I can't find anything like OctokitError in the types. I can add my own error type like:

type OctokitError = {
  name: string;
  status: number;
  documentation_url: string;
  errors?: Array<{
    resource: string;
    code: string;
    field: string;
    message?: string;
  }>;
};

Is there a concrete contract for errors I can see somewhere to ensure above type is correct? I looked in the docs and they seem incorrect (or out of date):

See for example this error I got back from octokit.search.commits:

Notice how there is a message in: e.errors[i].message where the docs only mentions e.message.

@gr2m
Copy link
Contributor

gr2m commented Aug 5, 2020

We could add an OctokitRequestError type to https://github.com/octokit/types.ts/tree/master/src, so you can import it from @octokit/types.

I don't think it's currently possible to set types for errors that can be thrown by a method (microsoft/TypeScript#13219), that'd be cool though

@sorenlouv
Copy link
Contributor Author

sorenlouv commented Aug 5, 2020

I don't think it's currently possible to set types for errors that can be thrown by a method (microsoft/TypeScript#13219), that'd be cool though

yeah, that would be neat. But until then I think if(e instanceof OctokitRequestError) is the recommended approach. Having an explicit type like OctokitRequestError will be great for documentation purposes also.

@gr2m
Copy link
Contributor

gr2m commented Aug 5, 2020

could you send a pull request to https://github.com/octokit/types.ts/?

@sorenlouv
Copy link
Contributor Author

@gr2m Done!

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 a pull request may close this issue.

2 participants