From da77c227c1ac5f9cd292b5154d7fc22847cf57d7 Mon Sep 17 00:00:00 2001 From: Nevo David Date: Sat, 2 Aug 2025 14:21:10 +0700 Subject: [PATCH] feat: error handling that is not an http request --- .../src/integrations/social.abstract.ts | 35 +++++++++++++------ .../src/integrations/social/x.provider.ts | 15 ++++++++ 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/libraries/nestjs-libraries/src/integrations/social.abstract.ts b/libraries/nestjs-libraries/src/integrations/social.abstract.ts index a9311d94..193c3ee9 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(func: (...args: any[]) => Promise) { - const value = await concurrencyService(this.identifier.split('-')[0], async () => { - try { - return await func(); - } catch (err) { - return {type: 'error', value: err}; + const value = await concurrencyService( + 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 87fe9972..98e9db98 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',