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

Add beforeError hook #696

Merged
merged 11 commits into from Jan 14, 2019
Merged

Conversation

szmarczak
Copy link
Collaborator

Fixes https://github.com/sindresorhus/gh-got/blob/master/index.js

Instead using handler we could do:

hooks: {
	beforeRequest: [
		options => {
			if (options.token) {
				options.headers.authorization = options.headers.authorization || `token ${options.token}`;
			}
		}
	],
	afterResponse: [
		response => {
			response.rateLimit = getRateLimit(response);
			return response;
		}
	],
	onError: [
		error => {
			const {response} = error;

			if (response && response.body) {
				error.name = 'GitHubError';
				error.message = `${response.body.message} (${error.statusCode})`;
			}

			if (response) {
				error.rateLimit = getRateLimit(response);
			}

			return error;
		}
	]
}

Checklist

  • I have read the documentation.
  • I have included a pull request description of my changes.
  • I have included some tests.
  • If it's a new feature, I have included documentation updates.

@sindresorhus
Copy link
Owner

Fixes sindresorhus/gh-got:index.js@master

I wouldn't say it fixes that issue. This just works around it. We still need to properly fix handler.

@sindresorhus
Copy link
Owner

Does the hook catch absolutely all errors? If not, it should be documented what it doesn't catch. For example, I don't think it currently catches input validation errors (Neither should it IMHO).

@sindresorhus
Copy link
Owner

Wouldn't beforeError be a more correct name for this?

@sindresorhus sindresorhus changed the title onError hook Add onError hook Jan 13, 2019
readme.md Show resolved Hide resolved
@szmarczak
Copy link
Collaborator Author

I wouldn't say it fixes that issue. This just works around it. We still need to properly fix handler.

True. But it's not that easy, it will take a while to solve it :P

Does the hook catch absolutely all errors? If not, it should be documented what it doesn't catch. For example, I don't think it currently catches input validation errors (Neither should it IMHO).

You're right. I'll document that.

Wouldn't beforeError be a more correct name for this?

I have been still considering this. You just nailed it :) Will change that.

@szmarczak szmarczak changed the title Add onError hook Add beforeError hook Jan 13, 2019
readme.md Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

Can you fix the merge conflict?

@szmarczak
Copy link
Collaborator Author

done

@sindresorhus sindresorhus merged commit 29ffb44 into sindresorhus:master Jan 14, 2019
Kikobeats pushed a commit to microlinkhq/unavatar that referenced this pull request Jan 17, 2019
## The dependency [got](https://github.com/sindresorhus/got) was updated from `9.5.1` to `9.6.0`.
This version is **not covered** by your **current version range**.

If you don’t accept this pull request, your project will work just like it did before. However, you might be missing out on a bunch of new features, fixes and/or performance improvements from the dependency update.

---

<details>
<summary>Release Notes for v9.6.0</summary>

<ul>
<li>Add <code>init</code> hook (<a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="393703177" data-permission-text="Issue title is private" data-url="sindresorhus/got#683" data-hovercard-type="pull_request" data-hovercard-url="/sindresorhus/got/pull/683/hovercard" href="https://urls.greenkeeper.io/sindresorhus/got/pull/683">#683</a>)  <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/sindresorhus/got/commit/677d0a479bd99c0cbcffdc5847038745dfa72bd1/hovercard" href="https://urls.greenkeeper.io/sindresorhus/got/commit/677d0a479bd99c0cbcffdc5847038745dfa72bd1"><tt>677d0a4</tt></a></li>
<li>Add <code>beforeError</code> hook (<a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="398544251" data-permission-text="Issue title is private" data-url="sindresorhus/got#696" data-hovercard-type="pull_request" data-hovercard-url="/sindresorhus/got/pull/696/hovercard" href="https://urls.greenkeeper.io/sindresorhus/got/pull/696">#696</a>)  <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/sindresorhus/got/commit/29ffb44f6be951e1103bb076dadf2b0e5cbd62f1/hovercard" href="https://urls.greenkeeper.io/sindresorhus/got/commit/29ffb44f6be951e1103bb076dadf2b0e5cbd62f1"><tt>29ffb44</tt></a></li>
</ul>
<p><a class="commit-link" href="https://urls.greenkeeper.io/sindresorhus/got/compare/v9.5.1...v9.6.0"><tt>v9.5.1...v9.6.0</tt></a></p>
</details>

<details>
<summary>Commits</summary>
<p>The new version differs by 4 commits.</p>
<ul>
<li><a href="https://urls.greenkeeper.io/sindresorhus/got/commit/a45e071e8dc5027ab9c7fad5919195501cd9e9ca"><code>a45e071</code></a> <code>9.6.0</code></li>
<li><a href="https://urls.greenkeeper.io/sindresorhus/got/commit/29ffb44f6be951e1103bb076dadf2b0e5cbd62f1"><code>29ffb44</code></a> <code>Add <code>beforeError</code> hook (#696)</code></li>
<li><a href="https://urls.greenkeeper.io/sindresorhus/got/commit/677d0a479bd99c0cbcffdc5847038745dfa72bd1"><code>677d0a4</code></a> <code>Add <code>init</code> hook (#683)</code></li>
<li><a href="https://urls.greenkeeper.io/sindresorhus/got/commit/e2d360211e2101ab966ee6617abe8f22881caf84"><code>e2d3602</code></a> <code>Bump XO</code></li>
</ul>
<p>See the <a href="https://urls.greenkeeper.io/sindresorhus/got/compare/6ce603e99a17d258751ddce23b1c9d424b7be795...a45e071e8dc5027ab9c7fad5919195501cd9e9ca">full diff</a></p>
</details>

<details>
  <summary>FAQ and help</summary>

  There is a collection of [frequently asked questions](https://greenkeeper.io/faq.html). If those don’t help, you can always [ask the humans behind Greenkeeper](https://github.com/greenkeeperio/greenkeeper/issues/new).
</details>

---


Your [Greenkeeper](https://greenkeeper.io) bot 🌴
@szmarczak szmarczak deleted the onerror-hook branch January 17, 2019 18:55
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.

None yet

2 participants