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

🤖 Refactor About component to improve error handling and parallelize API requests #683

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
89 changes: 51 additions & 38 deletions react/src/components/About.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,57 @@ import Noah from './employees/noah';

const employees = [Jane, Lily, Keith, Mason, Emma, Noah];


function About({ backend }) {
useEffect(() => {
if (!isOddReleaseWeek()) {
// can't have async sleep in a constructor
busy_sleep(Math.random(25) + 100);
}

// Http requests to make in parallel, so the Transaction has more Spans
let request1 = fetch(backend + '/api', {
method: 'GET',
});
let request2 = fetch(backend + '/organization', {
method: 'GET',
});
let request3 = fetch(backend + '/connect', {
method: 'GET',
});
const fetchData = async () => {
if (!isOddReleaseWeek()) {
busy_sleep(Math.random(25) + 100);
}

// Need Safari13 in tests/config.py in order for this modern javascript to work in Safari Browser
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled#browser_compatibility
// let response = await Promise.allSettled([request1, request2, request3])
try {
// Create requests in parallel
const requests = [
fetch(backend + '/api'),
fetch(backend + '/organization'),
fetch(backend + '/connect')
];

const response = [request1, request2, request3];
// Wait for all requests to complete
const responses = await Promise.allSettled(requests);

// Error Handling
response.forEach((r) => {
if (!r.ok) {
Sentry.withScope((scope) => {
scope.setContext('response', r);
Sentry.captureException(
new Error(
r.status + ' - ' + (response.statusText || 'Server Error for API')
)
);
// Process each response
responses.forEach((result) => {
if (result.status === 'rejected') {
// Handle network errors
Sentry.withScope((scope) => {
scope.setContext('error', { message: result.reason.message });
Sentry.captureException(result.reason);
});
} else if (!result.value.ok) {
// Handle HTTP error responses
Sentry.withScope((scope) => {
scope.setContext('response', {
status: result.value.status,
statusText: result.value.statusText,
url: result.value.url
});
Sentry.captureException(
new Error(
`${result.value.status} - ${result.value.statusText || 'Server Error'}`
)
);
});
}
});
} catch (error) {
// Handle any unexpected errors
Sentry.captureException(error);
}
});
}, []);
};

fetchData();
}, [backend]); // Add backend to dependency array

return (
<div className="about-page">
Expand All @@ -60,14 +73,14 @@ function About({ backend }) {
<h1>About us</h1>
<p>
Empower Plant is an IoT company determined to keep house plants
happy. After reading Michael Pollans 2013 New Yorker article (The
Intelligent Plant), the wife-and-wife founding team invested their
happy. After reading Michael Pollan's 2013 New Yorker article ("The
Intelligent Plant"), the wife-and-wife founding team invested their
life savings in measuring and improving the emotional state of their
leafy housemates. Seven years later, the companys grown from its
humble roots in the couples backyard greenhouse (converted from a
garage) into a Series C-funded San Francisco startup and the worlds
most accurate plant mood measurer (a must-have for any responsible
plant parent, according to Plant Parenthood Magazine). Their
leafy housemates. Seven years later, the company's grown from its
humble roots in the couples' backyard greenhouse (converted from a
garage) into a Series C-funded San Francisco startup and the world's
most accurate plant mood measurer ("a must-have for any responsible
plant parent," according to Plant Parenthood Magazine). Their
original state-of-the-art product is a technological marvel built
with a plant-first mindset, and they now offer a range of
plant-centric options.
Expand Down
Loading