Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ニュース投稿のエラー表示詳細に #157

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ S3_SECRET_ACCESS_KEY=
S3_BUCKET_NAME=

CURRENTS_API_KEY=
NEWS_USER_ID=
NEWS_POST_API_KEY=
OUR_ID=
35 changes: 22 additions & 13 deletions backend/src/lib/getNews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,43 @@ const getNews = async () => {
`https://api.currentsapi.services/v1/latest-news?language=ja&apiKey=${env.CURRENTS_API_KEY}`
);

// HTTPエラーが発生した場合
// HTTPエラーが発生した場合(たまにここでエラーが起きる)
if (!response.ok) {
throw new Error(`HTTPエラー: ${response.status}`);
throw new Error(`【getNews】HTTPエラー: ${response.status}`);
}

const data = await response.json();

console.log(data);
// console.log(data);

// ニュースの取得に失敗した場合
if (data.status !== 'ok') {
throw new Error('ニュースの取得に失敗しました。');
throw new Error(`【getNews】ニュースの取得に失敗しました。(status: ${data.status})`);
}

const newsArray = data.news;
// ニュースの配列
const newsAllArray = data.news;

let news;
for (let i = 0; i < newsArray.length; i++) {
const newsTemp = newsArray[i];
if (newsTemp.title !== '' && newsTemp.description !== '' && newsTemp.url !== '') {
news = newsTemp;
break;
// 表示できるニュースの配列
let satisfiedNewsArray = [];
for (let i = 0; i < newsAllArray.length; i++) {
if (
newsAllArray[i].title !== '' &&
newsAllArray[i].description !== '' &&
newsAllArray[i].url !== ''
) {
satisfiedNewsArray.push(newsAllArray[i]);
}
}

if (!news) {
throw new Error('表示できるニュースがありません。');
if (satisfiedNewsArray.length === 0) {
throw new Error('【getNews】表示できるニュースがありません。');
}

// 表示できるニュースの配列からランダムに1つ選択
const randomIndex = Math.floor(Math.random() * satisfiedNewsArray.length);
const news = satisfiedNewsArray[randomIndex];

return {
isSuccess: true,
news: {
Expand Down
30 changes: 14 additions & 16 deletions backend/src/lib/postNews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const postNews = async (requestApiKey: string) => {
return {
isSuccess: false,
tanka: {
line0: 'ニュースの取得に失敗しました',
line0: newsResponse.message,
line1: '',
line2: '',
line3: '',
Expand All @@ -56,33 +56,31 @@ const postNews = async (requestApiKey: string) => {
body: formData,
});

console.log('postResponse', postResponse);
// console.log('postResponse', postResponse);

if (!postResponse.ok) {
return {
isSuccess: false,
tanka: {
line0: '',
line1: '',
line2: '',
line3: '',
line4: '',
},
};
throw new Error(`【postNews】HTTPエラー: ${postResponse.status}`);
}

const json = await postResponse.json();
const jsonPostResponse = await postResponse.json();
// console.log('jsonPostResponse', jsonPostResponse);

if (jsonPostResponse.message !== '投稿しました.') {
throw new Error(
`【postNews】ニュースの投稿に失敗しました。(status: ${jsonPostResponse.message})`
);
}

return {
isSuccess: true,
tanka: json.tanka,
tanka: jsonPostResponse.tanka,
};
} catch (error) {
} catch (error: any) {
console.error(error);
return {
isSuccess: false,
tanka: {
line0: '投稿に失敗しました',
line0: error.message,
line1: '',
line2: '',
line3: '',
Expand Down