Skip to content

Conversation

@erunion
Copy link
Member

@erunion erunion commented Jul 13, 2020

🧰 What's being changed?

This updates the SDK so when error HTTP statuses (>=400, <=599) are hit, the promise is automatically rejected.

It will turn the following, unnecessarily verbose code:

readme
  .auth(apiKey)
  .createChangelog({ title, body: body.join('\n'), hidden: true })
  .then(res => {
    if (res.status !== 201) {
      console.error('Error creating Changelog:', res.status, res.statusText);
      process.exit(1);
    }
    return res.json();
  })
  .then(json => {
    console.log('Changelog created!', json._id);
  })
  .catch(err => {
    console.error(err);
    process.exit(1);
  });

Into the following:

readme
  .auth(apiKey)
  .createChangelog({ title, body: body.join('\n'), hidden: true })
  .then(res => res.json())
  .then(json => {
    console.log('Changelog created!', json._id);
  })
  .catch(err => {
    console.error('Error creating Changelog:', err.status, err.statusText);
    process.exit(1);
  });

err in this case, is the full error response from the API request so if you wish to inspect it for JSON content you can do so within the .catch() by doing an await err.json().

Resolves #103

@erunion erunion added enhancement New feature or request area:core Issues related to `core`, which is the package that powers the SDKs at runtime labels Jul 13, 2020
@erunion erunion requested a review from kanadgupta July 13, 2020 00:39
Copy link
Member

@kanadgupta kanadgupta left a comment

Choose a reason for hiding this comment

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

🙌

const json = await err.json();
expect(json).toStrictEqual(response);

mock.done();
Copy link
Member

Choose a reason for hiding this comment

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

Huh I've never known about this nock function

@erunion erunion merged commit 827f32a into master Jul 13, 2020
@erunion erunion deleted the feat/reject-for-error-statuses branch July 13, 2020 19:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core Issues related to `core`, which is the package that powers the SDKs at runtime enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SDK promise should reject if status received is not 2xx or 3xx

3 participants