Skip to content

Commit

Permalink
feat: Improve Logger.error and fatal, return error (#48)
Browse files Browse the repository at this point in the history
Co-authored-by: pike <pike@SilverAir>
  • Loading branch information
commonpike and pike authored Dec 3, 2023
1 parent c67d60a commit 3aa598f
Show file tree
Hide file tree
Showing 16 changed files with 159 additions and 179 deletions.
40 changes: 19 additions & 21 deletions src/auth/FacebookAuth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import OAuth2Client from "./OAuth2Client";
import Logger from "../services/Logger";
import OAuth2Client from "./OAuth2Client";
import Storage from "../services/Storage";

export default class FacebookAuth extends OAuth2Client {
Expand Down Expand Up @@ -51,18 +51,15 @@ export default class FacebookAuth extends OAuth2Client {
const result = await this.requestRemotePermissions("Facebook", url.href);
if (result["error"]) {
const msg = result["error_reason"] + " - " + result["error_description"];
Logger.error(msg, result);
throw new Error(msg);
throw Logger.error(msg, result);
}
if (result["state"] !== state) {
const msg = "Response state does not match request state";
Logger.error(msg, result);
throw new Error(msg);
throw Logger.error(msg, result);
}
if (!result["code"]) {
const msg = "Remote response did not return a code";
Logger.error(msg, result);
throw new Error(msg);
throw Logger.error(msg, result);
}
return result["code"] as string;
}
Expand All @@ -83,8 +80,7 @@ export default class FacebookAuth extends OAuth2Client {

if (!result["access_token"]) {
const msg = "Remote response did not return a access_token";
Logger.error(msg, result);
throw new Error(msg);
throw Logger.error(msg, result);
}

return result["access_token"];
Expand Down Expand Up @@ -129,9 +125,9 @@ export default class FacebookAuth extends OAuth2Client {
];

if (!llPageAccessToken) {
console.error(data);
throw new Error(
throw Logger.error(
"No llPageAccessToken for page " + pageId + " in response.",
data,
);
}

Expand Down Expand Up @@ -160,8 +156,10 @@ export default class FacebookAuth extends OAuth2Client {
access_token: string;
};
if (!data["access_token"]) {
console.error(data);
throw new Error("No llUserAccessToken access_token in response.");
throw Logger.error(
"No llUserAccessToken access_token in response.",
data,
);
}

return data["access_token"];
Expand All @@ -182,8 +180,7 @@ export default class FacebookAuth extends OAuth2Client {
name: string;
};
if (!data["id"]) {
console.error(data);
throw new Error("Can not get app scoped user id.");
throw Logger.error("Can not get app scoped user id.", data);
}
return data["id"];
}
Expand Down Expand Up @@ -221,8 +218,11 @@ export default class FacebookAuth extends OAuth2Client {
*/
private async handleApiResponse(response: Response): Promise<object> {
if (!response.ok) {
Logger.error("FacebookAuth.handleApiResponse", response);
throw new Error(response.status + ":" + response.statusText);
throw Logger.error(
"FacebookAuth.handleApiResponse",
response.status + ":" + response.statusText,
response,
);
}
const data = await response.json();
if (data.error) {
Expand All @@ -236,8 +236,7 @@ export default class FacebookAuth extends OAuth2Client {
data.error.error_subcode +
") " +
data.error.message;
Logger.error("FacebookAuth.handleApiResponse", error);
throw new Error(error);
throw Logger.error("FacebookAuth.handleApiResponse", error);
}
Logger.trace("FacebookAuth.handleApiResponse", "success");
return data;
Expand All @@ -248,7 +247,6 @@ export default class FacebookAuth extends OAuth2Client {
* @param error - the error returned from fetch
*/
private handleApiError(error: Error): never {
Logger.error("FacebookAuth.handleApiError", error);
throw error;
throw Logger.error("FacebookAuth.handleApiError", error);
}
}
9 changes: 3 additions & 6 deletions src/auth/InstagramAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,15 @@ export default class InstagramAuth extends FacebookAuth {

if (result["error"]) {
const msg = result["error_reason"] + " - " + result["error_description"];
Logger.error(msg, result);
throw new Error(msg);
throw Logger.error(msg, result);
}
if (result["state"] !== state) {
const msg = "Response state does not match request state";
Logger.error(msg, result);
throw new Error(msg);
throw Logger.error(msg, result);
}
if (!result["code"]) {
const msg = "Remote response did not return a code";
Logger.error(msg, result);
throw new Error(msg);
throw Logger.error(msg, result);
}
return result["code"] as string;
}
Expand Down
27 changes: 11 additions & 16 deletions src/auth/LinkedInAuth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import OAuth2Client from "./OAuth2Client";
import Logger from "../services/Logger";
import OAuth2Client from "./OAuth2Client";
import Storage from "../services/Storage";

export default class LinkedInAuth extends OAuth2Client {
Expand Down Expand Up @@ -41,8 +41,7 @@ export default class LinkedInAuth extends OAuth2Client {

if (!result["access_token"]) {
const msg = "Remote response did not return a access_token";
Logger.error(msg, result);
throw new Error(msg);
throw Logger.error(msg, result);
}
this.accessToken = result["access_token"];
// now store it
Expand Down Expand Up @@ -74,18 +73,15 @@ export default class LinkedInAuth extends OAuth2Client {
const result = await this.requestRemotePermissions("LinkedIn", url.href);
if (result["error"]) {
const msg = result["error_reason"] + " - " + result["error_description"];
Logger.error(msg, result);
throw new Error(msg);
throw Logger.error(msg, result);
}
if (result["state"] !== state) {
const msg = "Response state does not match request state";
Logger.error(msg, result);
throw new Error(msg);
throw Logger.error(msg, result);
}
if (!result["code"]) {
const msg = "Remote response did not return a code";
Logger.error(msg, result);
throw new Error(msg);
throw Logger.error(msg, result);
}
return result["code"] as string;
}
Expand Down Expand Up @@ -117,8 +113,7 @@ export default class LinkedInAuth extends OAuth2Client {

if (!result["access_token"]) {
const msg = "Remote response did not return a access_token";
Logger.error(msg, result);
throw new Error(msg);
throw Logger.error(msg, result);
}

return result;
Expand Down Expand Up @@ -156,10 +151,11 @@ export default class LinkedInAuth extends OAuth2Client {
*/
private async handleApiResponse(response: Response): Promise<object> {
if (!response.ok) {
Logger.error("LinkedInAuth.handleApiResponse", "not ok");
Logger.error("LinkedInAuth.handleApiResponse", await response.json());
throw new Error(
Logger.warn("LinkedInAuth.handleApiResponse", "not ok");
throw Logger.error(
"LinkedInAuth.handleApiResponse",
response.url + ":" + response.status + ", " + response.statusText,
await response.json(),
);
}
const data = await response.json();
Expand All @@ -174,8 +170,7 @@ export default class LinkedInAuth extends OAuth2Client {
data.error.error_subcode +
") " +
data.error.message;
Logger.error("LinkedInAuth.handleApiResponse", error);
throw new Error(error);
throw Logger.error("LinkedInAuth.handleApiResponse", error);
}
Logger.trace("LinkedInAuth.handleApiResponse", "success");
return data;
Expand Down
30 changes: 13 additions & 17 deletions src/auth/RedditAuth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import OAuth2Client from "./OAuth2Client";
import Logger from "../services/Logger";
import OAuth2Client from "./OAuth2Client";
import Storage from "../services/Storage";

export default class RedditAuth extends OAuth2Client {
Expand Down Expand Up @@ -38,8 +38,7 @@ export default class RedditAuth extends OAuth2Client {

if (!result["access_token"]) {
const msg = "Remote response did not return a access_token";
Logger.error(msg, result);
throw new Error(msg);
throw Logger.error(msg, result);
}
this.accessToken = result["access_token"];
return this.accessToken;
Expand All @@ -66,18 +65,15 @@ export default class RedditAuth extends OAuth2Client {
const result = await this.requestRemotePermissions("Reddit", url.href);
if (result["error"]) {
const msg = result["error_reason"] + " - " + result["error_description"];
Logger.error(msg, result);
throw new Error(msg);
throw Logger.error(msg, result);
}
if (result["state"] !== state) {
const msg = "Response state does not match request state";
Logger.error(msg, result);
throw new Error(msg);
throw Logger.error(msg, result);
}
if (!result["code"]) {
const msg = "Remote response did not return a code";
Logger.error(msg, result);
throw new Error(msg);
throw Logger.error(msg, result);
}
return result["code"] as string;
}
Expand Down Expand Up @@ -106,8 +102,7 @@ export default class RedditAuth extends OAuth2Client {

if (!result["access_token"]) {
const msg = "Remote response did not return a access_token";
Logger.error(msg, result);
throw new Error(msg);
throw Logger.error(msg, result);
}

return result;
Expand Down Expand Up @@ -153,8 +148,11 @@ export default class RedditAuth extends OAuth2Client {
*/
private async handleApiResponse(response: Response): Promise<object> {
if (!response.ok) {
Logger.error("RedditAuth.handleApiResponse", "not ok");
throw new Error(response.status + ":" + response.statusText);
throw Logger.error(
"RedditAuth.handleApiResponse",
"not ok",
response.status + ":" + response.statusText,
);
}
const data = await response.json();
if (data.error) {
Expand All @@ -168,8 +166,7 @@ export default class RedditAuth extends OAuth2Client {
data.error.error_subcode +
") " +
data.error.message;
Logger.error("RedditAuth.handleApiResponse", error);
throw new Error(error);
throw Logger.error("RedditAuth.handleApiResponse", error);
}
Logger.trace("RedditAuth.handleApiResponse", "success");
return data;
Expand All @@ -180,7 +177,6 @@ export default class RedditAuth extends OAuth2Client {
* @param error - the error returned from fetch
*/
private handleApiError(error: Error): never {
Logger.error("RedditAuth.handleApiError", error);
throw error;
throw Logger.error("RedditAuth.handleApiError", error);
}
}
16 changes: 6 additions & 10 deletions src/auth/TwitterAuth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { TwitterApi } from "twitter-api-v2";

import OAuth2Client from "./OAuth2Client";
import Logger from "../services/Logger";
import OAuth2Client from "./OAuth2Client";
import Storage from "../services/Storage";
import { TwitterApi } from "twitter-api-v2";

export default class TwitterAuth extends OAuth2Client {
async setup() {
Expand Down Expand Up @@ -31,18 +30,15 @@ export default class TwitterAuth extends OAuth2Client {
const result = await this.requestRemotePermissions("Twitter", url);
if (result["error"]) {
const msg = result["error_reason"] + " - " + result["error_description"];
Logger.error(msg, result);
throw new Error(msg);
throw Logger.error(msg, result);
}
if (result["state"] !== state) {
const msg = "Response state does not match request state";
Logger.error(msg, result);
throw new Error(msg);
throw Logger.error(msg, result);
}
if (!result["code"]) {
const msg = "Remote response did not return a code";
Logger.error(msg, result);
throw new Error(msg);
throw Logger.error(msg, result);
}

const tokens = await client.loginWithOAuth2({
Expand All @@ -51,7 +47,7 @@ export default class TwitterAuth extends OAuth2Client {
redirectUri: this.getCallbackUrl(),
});
if (!tokens["accessToken"]) {
throw new Error("An accessToken was not returned");
throw Logger.error("An accessToken was not returned");
}

return tokens;
Expand Down
12 changes: 6 additions & 6 deletions src/models/Feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export default class Feed {
Logger.trace("Feed", "getPlatform", platformId);
const platform = this.platforms[platformId];
if (!platform) {
throw new Error("Unknown platform: " + platformId);
throw Logger.error("Unknown platform: " + platformId);
}
return platform;
}
Expand Down Expand Up @@ -285,10 +285,10 @@ export default class Feed {
Logger.trace("Feed", "schedulePost", path, platformId, date);
const post = this.getPost(path, platformId);
if (!post.valid) {
throw new Error("Post is not valid");
throw Logger.error("Post is not valid");
}
if (post.status !== PostStatus.UNSCHEDULED) {
throw new Error("Post is not unscheduled");
throw Logger.error("Post is not unscheduled");
}
post.schedule(date);
return post;
Expand Down Expand Up @@ -319,10 +319,10 @@ export default class Feed {
for (const folder of folders) {
const post = platform.getPost(folder);
if (!post.valid) {
throw new Error("Post is not valid");
throw Logger.error("Post is not valid");
}
if (post.status !== PostStatus.UNSCHEDULED) {
throw new Error("Post is not unscheduled");
throw Logger.error("Post is not unscheduled");
}
post.schedule(date);
posts.push(post);
Expand Down Expand Up @@ -356,7 +356,7 @@ export default class Feed {
Logger.info("Posting", platformId, path);
await platform.publishPost(post, dryrun);
} else {
throw new Error("Post is not valid");
throw Logger.error("Post is not valid");
}
return post;
}
Expand Down
2 changes: 1 addition & 1 deletion src/models/Platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export default class Platform {
* @returns - any object
*/
async setup() {
throw new Error(
throw Logger.error(
"No setup implemented for " +
this.id +
". Read the docs in the docs folder.",
Expand Down
Loading

0 comments on commit 3aa598f

Please sign in to comment.