Skip to content

Commit

Permalink
fix: Fix handling of upstream errors (#413)
Browse files Browse the repository at this point in the history
* Fix error propagation

* Update some pkgs

* Better erro msg
  • Loading branch information
amaury1093 authored Jan 8, 2023
1 parent 05e86cc commit 096274e
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 152 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
"@sentry/nextjs": "^7.24.2",
"@stripe/stripe-js": "^1.46.0",
"@supabase/supabase-js": "^1.35.7",
"@types/cors": "^2.8.12",
"@types/cors": "^2.8.13",
"@types/mailgun-js": "^0.22.13",
"@types/markdown-pdf": "^9.0.1",
"@types/mustache": "^4.2.2",
"@types/react": "^17.0.43",
"@types/react-dom": "^18.0.3",
"@types/request-ip": "^0.0.37",
"@types/uuid": "^9.0.0",
"axios": "^1.2.1",
"axios": "^1.2.2",
"cors": "^2.8.5",
"date-fns": "^2.29.3",
"mailgun-js": "^0.22.0",
Expand All @@ -44,8 +44,8 @@
},
"devDependencies": {
"@amaurym/config": "^1.3.6",
"eslint-config-next": "^13.0.6",
"typescript": "^4.8.2",
"vercel": "^28.8.0"
"eslint-config-next": "^13.1.1",
"typescript": "^4.9.4",
"vercel": "^28.10.3"
}
}
24 changes: 7 additions & 17 deletions src/components/ApiUsage.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
import { Capacity, Loading, Spacer, Text } from '@geist-ui/react';
import { Loader } from '@geist-ui/react-icons';
import { User } from '@supabase/supabase-js';
import { format, parseISO } from 'date-fns';
import React, { useEffect, useState } from 'react';

import { sentryException } from '../util/sentry';
import { subApiMaxCalls } from '../util/subs';
import {
getApiUsageClient,
SupabaseSubscription,
} from '../util/supabaseClient';
import { getApiUsageClient } from '../util/supabaseClient';
import { useUser } from '../util/useUser';
import styles from './ApiUsage.module.css';
import { Demo } from './Demo';

function fetchApiCalls(
user: User,
subscription: SupabaseSubscription | null,
onResult: (count: number) => void
) {
getApiUsageClient(user, subscription).then(onResult).catch(sentryException);
}

export function ApiUsage(): React.ReactElement {
const { subscription, user, userFinishedLoading } = useUser();
const [apiCalls, setApiCalls] = useState<number | undefined>(undefined); // undefined means loading
Expand All @@ -30,7 +18,9 @@ export function ApiUsage(): React.ReactElement {
if (!user || !userFinishedLoading) {
return;
}
fetchApiCalls(user, subscription, setApiCalls);
getApiUsageClient(user, subscription)
.then(setApiCalls)
.catch(sentryException);
}, [user, userFinishedLoading, subscription]);

if (!user) {
Expand Down Expand Up @@ -71,9 +61,9 @@ export function ApiUsage(): React.ReactElement {

<Spacer />
<Demo
onVerified={() => {
fetchApiCalls(user, subscription, setApiCalls);
}}
onVerified={() =>
getApiUsageClient(user, subscription).then(setApiCalls)
}
/>
<Spacer />
</section>
Expand Down
7 changes: 4 additions & 3 deletions src/components/Demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function alertError(e: string) {
}

interface DemoProps {
onVerified?(result: CheckEmailOutput): void;
onVerified?(result: CheckEmailOutput): Promise<void>;
}

export function Demo({ onVerified }: DemoProps): React.ReactElement {
Expand Down Expand Up @@ -48,11 +48,12 @@ export function Demo({ onVerified }: DemoProps): React.ReactElement {
.then((r) => {
setResult(r);
setLoading(false);
onVerified && onVerified(r);
return onVerified && onVerified(r);
})
.catch((err: Error) => {
alertError(JSON.stringify(err));
sentryException(err);
alertError(err.message);
setLoading(false);
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/v0/check_email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ async function tryAllBackends(
}

return res.status(statusCode).json({
error: (err as AxiosError<unknown>).response?.data,
error: (err as AxiosError).response?.data,
});
}
}
Expand Down
25 changes: 17 additions & 8 deletions src/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,23 @@ export const postData = async <T = unknown>({

return res;
} catch (err) {
if ((err as AxiosError).response?.data) {
throw new Error(
(
err as AxiosError<{
error: string;
}>
).response?.data.error
);
if (err instanceof AxiosError) {
// Inspired by https://stackoverflow.com/questions/49967779/axios-handling-errors
let m: string;
if (err.response) {
// Request made and server responded
m = `${err.response.status?.toString()} ${JSON.stringify(
err.response.data
)}`; // eslint-disable-line
} else if (err.request) {
// The request was made but no response was received
m = 'Error in request';
} else {
// Something happened in setting up the request that triggered an Error
m = 'Error: ' + err.message;
}

throw new Error(m);
} else {
throw err;
}
Expand Down
Loading

1 comment on commit 096274e

@vercel
Copy link

@vercel vercel bot commented on 096274e Jan 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.