forked from LaconicNetwork/icns-frontend
Improve error handling for twitter api
This commit is contained in:
parent
d33788e2b1
commit
3b32610182
@ -48,6 +48,8 @@ export default withIronSessionApiRoute(async function handler(
|
|||||||
const {
|
const {
|
||||||
access_token: accessToken,
|
access_token: accessToken,
|
||||||
refresh_token,
|
refresh_token,
|
||||||
|
error: authTokenErrorTitle,
|
||||||
|
error_description: authTokenErrorDesc,
|
||||||
}: TwitterOAuth2TokenResponse = await (
|
}: TwitterOAuth2TokenResponse = await (
|
||||||
await fetch(`${twitterApiBaseUrl}/oauth2/token`, {
|
await fetch(`${twitterApiBaseUrl}/oauth2/token`, {
|
||||||
method: "post",
|
method: "post",
|
||||||
@ -61,9 +63,17 @@ export default withIronSessionApiRoute(async function handler(
|
|||||||
})
|
})
|
||||||
).json();
|
).json();
|
||||||
|
|
||||||
|
if (authTokenErrorTitle) {
|
||||||
|
throw new Error(`${authTokenErrorTitle}: ${authTokenErrorDesc}`);
|
||||||
|
}
|
||||||
|
|
||||||
req.session.refresh_token = refresh_token;
|
req.session.refresh_token = refresh_token;
|
||||||
await req.session.save();
|
await req.session.save();
|
||||||
const { data, title }: TwitterUsersMeResponse = await (
|
const {
|
||||||
|
data,
|
||||||
|
title: usersMeErrorTitle,
|
||||||
|
detail: usersMeErrorDesc,
|
||||||
|
}: TwitterUsersMeResponse = await (
|
||||||
await fetch(
|
await fetch(
|
||||||
`${twitterApiBaseUrl}/users/me?user.fields=profile_image_url,public_metrics,description`,
|
`${twitterApiBaseUrl}/users/me?user.fields=profile_image_url,public_metrics,description`,
|
||||||
{
|
{
|
||||||
@ -73,6 +83,11 @@ export default withIronSessionApiRoute(async function handler(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
).json();
|
).json();
|
||||||
|
|
||||||
|
if (usersMeErrorTitle) {
|
||||||
|
throw new Error(`${usersMeErrorTitle}: ${usersMeErrorDesc}`);
|
||||||
|
}
|
||||||
|
|
||||||
const {
|
const {
|
||||||
id,
|
id,
|
||||||
username,
|
username,
|
||||||
@ -93,12 +108,11 @@ export default withIronSessionApiRoute(async function handler(
|
|||||||
),
|
),
|
||||||
description,
|
description,
|
||||||
public_metrics,
|
public_metrics,
|
||||||
error: title,
|
|
||||||
});
|
});
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
captureException(error);
|
captureException(error);
|
||||||
res.status(500).json({ error: "Internal Server Error" });
|
res.status(500).json({ error: error.message });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ironOptions);
|
ironOptions);
|
||||||
@ -109,6 +123,8 @@ interface TwitterOAuth2TokenResponse {
|
|||||||
access_token: string;
|
access_token: string;
|
||||||
scope: string;
|
scope: string;
|
||||||
refresh_token: string;
|
refresh_token: string;
|
||||||
|
error?: string;
|
||||||
|
error_description?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface TwitterUsersMeResponse {
|
interface TwitterUsersMeResponse {
|
||||||
|
10
utils/url.ts
10
utils/url.ts
@ -17,13 +17,9 @@ export async function request<TResponse>(
|
|||||||
!customConfig?.isErrorIgnore
|
!customConfig?.isErrorIgnore
|
||||||
) {
|
) {
|
||||||
const { error, errors } = data;
|
const { error, errors } = data;
|
||||||
let errorMessage;
|
const errorMessage =
|
||||||
if (error && error.error) {
|
(error && error.toString()) || (errors && errors.toString());
|
||||||
errorMessage = error.error_description;
|
|
||||||
} else {
|
|
||||||
errorMessage =
|
|
||||||
(error && error.toString()) || (errors && errors.toString());
|
|
||||||
}
|
|
||||||
throw new Error(errorMessage);
|
throw new Error(errorMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user