feat: better errors

This commit is contained in:
Nevo David 2024-09-29 19:50:42 +07:00
parent 98384105b5
commit 44688ee77a
3 changed files with 40 additions and 15 deletions

View File

@ -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
);
}
}

View File

@ -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[]) {

View File

@ -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();