Skip to content

Commit

Permalink
Add optional chaining to reduce undefined errors (#834)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellemaxwell authored Mar 21, 2024
1 parent 7c7f5a9 commit 8e5d66f
Show file tree
Hide file tree
Showing 18 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion web/beacon-app/src/features/auth/hooks/useRegister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function useRegister(): RegistrationMutation {
message: 'Register failed',
},
});
if (error.response.status === 409) {
if (error?.response?.status === 409) {
toast.error(t`User already exists.`);
} else {
toast.error(error?.response?.data?.error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function useFetchMember(memberID: string): MemberQuery {
...memberDetailQuery(memberID),
onError: (error: any) => {
// stop logging 401 & 403 errors to sentry
if (error.response.status !== 401 && error.response.status !== 403) {
if (error?.response?.status !== 401 && error?.response?.status !== 403) {
Sentry.captureException(error);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function useFetchProfile(): ProfileQuery {
...profileQuery(),
onError: (error: any) => {
// stop logging 401 & 403 errors to sentry
if (error.response.status !== 401 && error.response.status !== 403) {
if (error?.response?.status !== 401 && error?.response?.status !== 403) {
Sentry.captureException(error);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const OrganizationStep = () => {
const state = useOrgStore((state: any) => state) as any;

// Display error if organization name is already taken.
const hasError = error && error.response.status === 409;
const hasError = error && error?.response?.status === 409;
const isInvited = isInvitedUser(profile);
const submitFormHandler = (values: any) => {
if (isInvited) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const UserPreferenceStep = () => {
const { profile } = useFetchProfile();
const { wasProfileUpdated, isUpdatingProfile, error, updateProfile, hasProfileFailed } =
useUpdateProfile();
const hasError = error && error.response.status === 400;
const hasError = error && error?.response?.status === 400;

const submitFormHandler = (values: any) => {
const requestPayload = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ const WorkspaceStep = () => {
const { updateProfile, wasProfileUpdated, isUpdatingProfile, reset, error } = useUpdateProfile();

// Check if the workspace is already taken.
const hasError = error && error.response.status === 409;
const hasError = error && error?.response?.status === 409;

// Check for workspace URL validation error.
const hasValidationError = error && error.response.status === 400;
const hasValidationError = error && error?.response?.status === 400;

const validationError = error?.response?.data?.validation_errors?.[0]?.error;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function useFetchProject(projectID: string): ProjectDetailQuery {
enabled: !!projectID,
onError: (error: any) => {
// stop logging 401 & 403 errors to sentry
if (error.response.status !== 401 && error.response.status !== 403) {
if (error?.response?.status !== 401 && error?.response?.status !== 403) {
Sentry.captureException(error);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function useFetchTenantProjects(tenantID: string): ProjectsQuery {
enabled: !!tenantID,
onError: (error: any) => {
// stop logging 401 & 403 errors to sentry
if (error.response.status !== 401 && error.response.status !== 403) {
if (error?.response?.status !== 401 && error?.response?.status !== 403) {
Sentry.captureException(error);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function ChangeRoleModal({ openChangeRoleModal, setOpenChangeRoleModal }: Change
{
onError(error, _variables, _context) {
toast.error((error as any)?.response?.data?.error || 'Something went wrong');
if ((error as any)?.response.status === 400) {
if ((error as any)?.response?.status === 400) {
setOpenChangeRoleModal({ ...openChangeRoleModal, opened: false });
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function useFetchTenants(): TenantsQuery {
const query = useQuery([RQK.TENANTS], tenantsRequest(axiosInstance), {
onError: (error: any) => {
// stop logging 401 & 403 errors to sentry
if (error.response.status !== 401 && error.response.status !== 403) {
if (error?.response?.status !== 401 && error?.response?.status !== 403) {
Sentry.captureException(error);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ const RevokeAPIKeyModal = ({ onOpen, onClose }: RevokeAPIKeyModalProps) => {

// handle error
useEffect(() => {
if (error && error.response.status === 401) {
if (error && error?.response?.status === 401) {
toast.error(
t`You do not have permission to revoke API keys. Please contact your administrator to change your role to a role with permission to revoke API keys.`
);
}

if (error && error.response.status !== 401) {
if (error && error?.response?.status !== 401) {
toast.error(
error?.response?.data?.error ||
t`Sorry, we were unable to revoke the API key. Please try again. If the issue persists, contact our support team for assistance.`
Expand Down
2 changes: 1 addition & 1 deletion web/beacon-app/src/features/topics/hooks/useFetchTopic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function useFetchTopic(topicID: string): TopicQuery {
enabled: !!topicID,
onError: (error: any) => {
// stop logging 401 & 403 errors to sentry
if (error.response.status !== 401 && error.response.status !== 403) {
if (error?.response?.status !== 401 && error?.response?.status !== 403) {
Sentry.captureException(error);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function useFetchTopicEvents(topicID: string): TopicEventsQuery {
refetchInterval: 60000,
onError: (error: any) => {
// stop logging 401 & 403 errors to sentry
if (error.response.status !== 401 && error.response.status !== 403) {
if (error?.response?.status !== 401 && error?.response?.status !== 403) {
Sentry.captureException(error);
}
},
Expand Down
2 changes: 1 addition & 1 deletion web/beacon-app/src/features/topics/hooks/useFetchTopics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function useFetchTopics(projectID: string): TopicsQuery {
enabled: !!projectID,
onError: (error: any) => {
// stop logging 401 & 403 errors to sentry
if (error.response.status !== 401 && error.response.status !== 403) {
if (error?.response?.status !== 401 && error?.response?.status !== 403) {
Sentry.captureException(error);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const TopicDetailPage = () => {
// if user switch to another organization and topic is not found then
// we need to redirect the user to the projects page
useEffect(() => {
if (error && error.response.status === 401) {
if (error && error?.response?.status === 401) {
navigate(PATH_DASHBOARD.PROJECTS);
}
}, [error, navigate]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function useFetchPermissions() {
const query = useQuery([RQK.PERMISSIONS], permissionsRequest(axiosInstance), {
onError: (error: any) => {
// stop logging 401 & 403 errors to sentry
if (error.response.status !== 401 && error.response.status !== 403) {
if (error?.response?.status !== 401 && error?.response?.status !== 403) {
Sentry.captureException(error);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function useFetchTenantQuickView(tenantID: string) {
enabled: !!tenantID,
onError: (error: any) => {
// stop logging 401 & 403 errors to sentry
if (error.response.status !== 401 && error.response.status !== 403) {
if (error?.response?.status !== 401 && error?.response?.status !== 403) {
Sentry.captureException(error);
}
},
Expand Down
2 changes: 1 addition & 1 deletion web/beacon-app/src/hooks/useResendEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const useResendEmail = () => {
}),
});
// send the status code to the client
const status = response.status;
const status = response?.status;

return status;
} catch (error) {
Expand Down

0 comments on commit 8e5d66f

Please sign in to comment.