Skip to content

Commit

Permalink
Implemented error handling for form submition
Browse files Browse the repository at this point in the history
- quote fetching
- wrong formdata format
- error saving to mongodb

resolves #68
  • Loading branch information
skorphil committed Mar 12, 2024
1 parent ccd26c3 commit ed97f65
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
4 changes: 2 additions & 2 deletions components/RecordForm/RecordForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ export function RecordForm() {
toast({
title: "Record saved",
status: "success",
duration: 4000,
duration: 3000,
});
} catch (error) {
toast({
title: "Error saving record",
description: error.message,
status: "error",
duration: 4000,
isClosable: true,
});
}
})}
Expand Down
6 changes: 3 additions & 3 deletions serverActions/appendRecord.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export async function appendRecord(formData) {
)
);

const record = new Record({
const record = await new Record({
date: Date.now(),
quotes: await getQuotes({ baseCurrencies, recordCurrencies }),
institutions: recordInstitutions,
Expand All @@ -97,8 +97,8 @@ export async function appendRecord(formData) {
try {
await connect();
await record.save();
console.log("Document saved to db");
console.info("record saved to db");
} catch (error) {
throw new Error("Error saving document:", error.message);
throw new Error(`Error saving data to database: ${error.message}`);
}
}
24 changes: 14 additions & 10 deletions serverActions/getQuotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@ export async function getQuotes({ baseCurrencies, recordCurrencies }) {
);

async function fetchQuotes(fetchUrls) {
try {
const promises = fetchUrls.map((url) => fetch(url));
const responses = await Promise.all(promises);
const data = await Promise.all(
responses.map((response) => response.json())
);
return data;
} catch (error) {
console.error(error);
}
const promises = await fetchUrls.map(async (url) => fetch(url));

const responses = await Promise.all(promises);
const data = await Promise.all(
responses.map(async (response) => {
if (!response.ok) {
throw new Error(
`Quotes API request failed with status ${response.status}. Url: ${response.url}`
);
}
return await response.json();
})
);
return data;
}

const quotes = (await fetchQuotes(fetchUrls))
Expand Down

0 comments on commit ed97f65

Please sign in to comment.