node-oauth-error

Convert oauth object-literal errors into actual Error() objects.

Usage no npm install needed!

<script type="module">
  import nodeOauthError from 'https://cdn.skypack.dev/node-oauth-error';
</script>

README

node-oauth-error

Convert oauth object-literal errors into actual Error() objects.

Build Status Coverage Status

Why?

For some reason, the popular oauth module returns object literals instead of Error() objects, so this module simply converts oauth object-literal errors into actual Error() objects. See oauth #250, #84, and #155.

Contribute

Please test this module in your projects and open issues or send pull requests if you would like to improve, extend or fix anything in this module. :smile:

Install

Assuming you have oauth installed, now install node-oauth-error:

$ npm install --save node-oauth-error

Usage

To use node-oauth-error, simply pass the original oauth error object into the OAuthError() constructor. Below is a usage example where a typical oauth setup is shown, but some irrelevant configuration details are omitted so that the error handler portion of the code can be highlighted.

const OAuth = require('oauth');
const OAuthError = require('node-oauth-error');

const oauth = new OAuth.OAuth(/* ... */);

oauth.get('some/url/endpoint',
  // ...
  (err, data) => {
    if (err) {
      // convert the oauth error into a real `Error()`.
      throw new OAuthError(err);
    }
    // ...
  }
);

Notes

This module knows how to convert oauth errors that have the following formats:

Format #1:

{
  statusCode: 401,
  data: '{"request": "\\/1.1\\/statuses\\/user_timeline.json", "error": "Not authorized."}'
}

Format #2:

{
  statusCode: 401,
  data: '{"errors": [{"code":89, "message": "Invalid or expired token."}]}'
}

If you see something I am missing, please open an issue or send a pull request. Thanks!

API

OAuthError(input)

The Error constructor.

input

Required
Type: object

The original oauth error object literal you want to convert to an actual Error.

License

MIT © Michael Wuergler