From 44688ee77a56f4586028ddd344bc4c803f0e609c Mon Sep 17 00:00:00 2001 From: Nevo David Date: Sun, 29 Sep 2024 19:50:42 +0700 Subject: [PATCH] feat: better errors --- .../database/prisma/posts/posts.service.ts | 19 ++++++++++++++- .../src/integrations/social.abstract.ts | 24 +++++++++++-------- .../integrations/social/facebook.provider.ts | 12 ++++++---- 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts b/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts index fbd051f2..70308a97 100644 --- a/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts +++ b/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts @@ -162,7 +162,24 @@ export class PostsService { true ); - console.error('[Error] posting on', firstPost.integration?.providerIdentifier, err); + if (err instanceof BadBody) { + console.error( + '[Error] posting on', + firstPost.integration?.providerIdentifier, + err.identifier, + err.json, + err.body, + err + ); + + return ; + } + + console.error( + '[Error] posting on', + firstPost.integration?.providerIdentifier, + err + ); } } diff --git a/libraries/nestjs-libraries/src/integrations/social.abstract.ts b/libraries/nestjs-libraries/src/integrations/social.abstract.ts index eee510db..9d06d017 100644 --- a/libraries/nestjs-libraries/src/integrations/social.abstract.ts +++ b/libraries/nestjs-libraries/src/integrations/social.abstract.ts @@ -1,14 +1,22 @@ export class RefreshToken { - constructor(public json: string, public body: BodyInit) {} + constructor( + public identifier: string, + public json: string, + public body: BodyInit + ) {} } export class BadBody { - constructor(public json: string, public body: BodyInit) {} + constructor( + public identifier: string, + public json: string, + public body: BodyInit + ) {} } export class NotEnoughScopes {} export abstract class SocialAbstract { - async fetch(url: string, options: RequestInit = {}) { + async fetch(url: string, options: RequestInit = {}, identifier = '') { const request = await fetch(url, options); if (request.status === 200 || request.status === 201) { @@ -17,20 +25,16 @@ export abstract class SocialAbstract { let json = '{}'; try { - json = await request.json(); + json = await request.text(); } catch (err) { json = '{}'; } if (request.status === 401) { - throw new RefreshToken(json, options.body!); + throw new RefreshToken(identifier, json, options.body!); } - if (request.status === 400) { - throw new BadBody(json, options.body!); - } - - return request; + throw new BadBody(identifier, json, options.body!); } checkScopes(required: string[], got: string | string[]) { diff --git a/libraries/nestjs-libraries/src/integrations/social/facebook.provider.ts b/libraries/nestjs-libraries/src/integrations/social/facebook.provider.ts index 7332848a..13c24f74 100644 --- a/libraries/nestjs-libraries/src/integrations/social/facebook.provider.ts +++ b/libraries/nestjs-libraries/src/integrations/social/facebook.provider.ts @@ -187,7 +187,8 @@ export class FacebookProvider extends SocialAbstract implements SocialProvider { description: firstPost.message, published: true, }), - } + }, + 'upload mp4' ) ).json(); @@ -210,7 +211,8 @@ export class FacebookProvider extends SocialAbstract implements SocialProvider { url: media.url, published: false, }), - } + }, + 'upload images slides' ) ).json(); @@ -235,7 +237,8 @@ export class FacebookProvider extends SocialAbstract implements SocialProvider { message: firstPost.message, published: true, }), - } + }, + 'finalize upload' ) ).json(); @@ -259,7 +262,8 @@ export class FacebookProvider extends SocialAbstract implements SocialProvider { : {}), message: comment.message, }), - } + }, + 'add comment' ) ).json();