Skip to content

πŸ“¨ The ultimate agentic social media scheduling tool πŸ€–

feat: error handling that is not an http request

Nevo Davidda77c227c1ac
diff --git a/libraries/nestjs-libraries/src/integrations/social.abstract.ts b/libraries/nestjs-libraries/src/integrations/social.abstract.ts
index a9311d943..193c3ee99 100644
--- a/libraries/nestjs-libraries/src/integrations/social.abstract.ts
+++ b/libraries/nestjs-libraries/src/integrations/social.abstract.ts
@@ -32,21 +32,30 @@ export abstract class SocialAbstract {
return undefined;
}
- public async mention(token: string, d: { query: string }, id: string, integration: Integration): Promise<{ id: string; label: string; image: string }[] | {none: true}> {
- return {none: true};
+ public async mention(
+ token: string,
+ d: { query: string },
+ id: string,
+ integration: Integration
+ ): Promise<{ id: string; label: string; image: string }[] | { none: true }> {
+ return { none: true };
}
async runInConcurrent<T>(func: (...args: any[]) => Promise<T>) {
- const value = await concurrencyService<any>(this.identifier.split('-')[0], async () => {
- try {
- return await func();
- } catch (err) {
- return {type: 'error', value: err};
+ const value = await concurrencyService<any>(
+ this.identifier.split('-')[0],
+ async () => {
+ try {
+ return await func();
+ } catch (err) {
+ const handle = this.handleErrors(JSON.stringify(err));
+ return { err: true, ...(handle || {}) };
+ }
}
- });
+ );
- if (value && value.type === 'error') {
- throw value.value;
+ if (value && value?.err && value?.value) {
+ throw new BadBody('', JSON.stringify({}), {} as any, value.value || '');
}
return value;
@@ -78,7 +87,11 @@ export abstract class SocialAbstract {
json = '{}';
}
- if (request.status === 500 || json.includes('rate_limit_exceeded') || json.includes('Rate limit')) {
+ if (
+ request.status === 500 ||
+ json.includes('rate_limit_exceeded') ||
+ json.includes('Rate limit')
+ ) {
await timer(5000);
return this.fetch(url, options, identifier, totalRetries + 1);
}
diff --git a/libraries/nestjs-libraries/src/integrations/social/x.provider.ts b/libraries/nestjs-libraries/src/integrations/social/x.provider.ts
index 87fe99727..98e9db98e 100644
--- a/libraries/nestjs-libraries/src/integrations/social/x.provider.ts
+++ b/libraries/nestjs-libraries/src/integrations/social/x.provider.ts
@@ -28,6 +28,21 @@ export class XProvider extends SocialAbstract implements SocialProvider {
editor = 'normal' as const;
+ override handleErrors(body: string):
+ | {
+ type: 'refresh-token' | 'bad-body';
+ value: string;
+ }
+ | undefined {
+ if (body.includes('The Tweet contains an invalid URL.')) {
+ return {
+ type: 'bad-body',
+ value: 'The Tweet contains a URL that is not allowed on X',
+ };
+ }
+ return undefined;
+ }
+
@Plug({
identifier: 'x-autoRepostPost',
title: 'Auto Repost Posts',