Skip to content

Commit

Permalink
consolidate error handling (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
HunterLarco authored Aug 29, 2020
1 parent c3e9f67 commit bfba167
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 71 deletions.
25 changes: 22 additions & 3 deletions src/TwitterError.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class TwitterError extends Error {
module.exports = class TwitterError extends Error {
constructor(message, code, details) {
super(message);

Expand All @@ -18,6 +18,25 @@ class TwitterError extends Error {
});
}
}
}
};

module.exports = TwitterError;
module.exports.fromJson = (json) => {
if (json.status && json.status != 200) {
return new module.exports(json.title, json.status, json.detail);
}

if (json.type) {
return new module.exports(`${json.title}: ${json.detail}`, null, json.type);
}

if (json.errors) {
const error = json.errors[0];
return new module.exports(
`${json.title}: ${error.message}`,
json.type,
json.detail
);
}

return null;
};
37 changes: 3 additions & 34 deletions src/TwitterStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,40 +57,9 @@ class TwitterStream {

const json = JSON.parse(line);

if (json.status && json.status != 200) {
this._emit(
Promise.reject(
new TwitterError(json.title, json.status, json.detail)
)
);
this.close();
return;
}

if (json.type) {
this._emit(
Promise.reject(
new TwitterError(
`${json.title}: ${json.detail}`,
null,
json.type
)
)
);
this.close();
return;
}

if (json.errors) {
this._emit(
Promise.reject(
new TwitterError(
`${json.title}: ${json.errors[0].message}`,
json.type,
json.detail
)
)
);
const error = TwitterError.fromJson(json);
if (error) {
this._emit(Promise.reject(error));
this.close();
return;
}
Expand Down
59 changes: 25 additions & 34 deletions src/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,6 @@ const Credentials = require('./Credentials.js');
const TwitterError = require('./TwitterError.js');
const TwitterStream = require('./TwitterStream.js');

async function cleanResponse(response) {
const body = await response.json();

if (body.status && body.status != 200) {
throw new TwitterError(body.title, body.status, body.detail);
}

if (body.errors) {
const error = body.errors[0];
throw new TwitterError(
`${body.title}: ${error.message}`,
body.type,
body.detail
);
}

return body;
}

function applyParameters(url, parameters, prefix) {
prefix = prefix || '';

Expand Down Expand Up @@ -56,27 +37,37 @@ class Twitter {
const url = new URL(`https://api.twitter.com/2/${endpoint}`);
applyParameters(url, parameters);

return cleanResponse(
await fetch(url.toString(), {
headers: {
Authorization: await this.credentials.authorizationHeader(url),
},
})
);
const json = await fetch(url.toString(), {
headers: {
Authorization: await this.credentials.authorizationHeader(url),
},
}).then((response) => response.json());

const error = TwitterError.fromJson(json);
if (error) {
throw error;
}

return json;
}

async post(endpoint, body, parameters) {
const url = new URL(`https://api.twitter.com/2/${endpoint}`);
applyParameters(url, parameters);

return cleanResponse(
await fetch(url.toString(), {
headers: {
Authorization: await this.credentials.authorizationHeader(url),
},
body: JSON.stringify(body || {}),
})
);
const json = await fetch(url.toString(), {
headers: {
Authorization: await this.credentials.authorizationHeader(url),
},
body: JSON.stringify(body || {}),
}).then((response) => response.json());

const error = TwitterError.fromJson(json);
if (error) {
throw error;
}

return json;
}

stream(endpoint, parameters) {
Expand Down

0 comments on commit bfba167

Please sign in to comment.