Skip to content

Commit

Permalink
refactor(console): improve webhook test request error handling (#6017)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyijun authored Jun 15, 2024
1 parent 15a51a4 commit dc6fbe2
Showing 1 changed file with 31 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import dayjs from 'dayjs';
import { HTTPError } from 'ky';
import { useRef, useState } from 'react';
import { useFormContext } from 'react-hook-form';
import { toast } from 'react-hot-toast';
import { useTranslation } from 'react-i18next';

import Button from '@/ds-components/Button';
Expand Down Expand Up @@ -33,7 +32,9 @@ function TestWebhook({ hookId }: Props) {
const { result, setResult } = useWebhookTestResult();

const [isSendingPayload, setIsSendingPayload] = useState(false);
const api = useApi({ hideErrorToast: true });
const api = useApi({
hideErrorToast: ['hook.send_test_payload_failed', 'hook.endpoint_responded_with_error'],
});

const sendTestPayload = async () => {
if (isSendingPayload) {
Expand All @@ -57,37 +58,34 @@ function TestWebhook({ hookId }: Props) {
requestTime,
});
} catch (error: unknown) {
if (!(error instanceof HTTPError)) {
toast.error(error instanceof Error ? String(error) : t('general.unknown_error'));
return;
}

const { code, data, message } = await error.response.clone().json<RequestErrorBody>();

if (code === 'hook.send_test_payload_failed') {
setResult({
result: 'error',
endpointUrl,
requestTime,
message,
});
return;
}

if (code === 'hook.endpoint_responded_with_error') {
const { responseStatus, responseBody } =
trySafe(() => hookTestErrorResponseDataGuard.parse(data)) ?? {};

setResult({
result: 'error',
endpointUrl,
requestTime,
message,
responseStatus,
responseBody,
});

return;
if (error instanceof HTTPError) {
const { code, data, message } = await error.response.clone().json<RequestErrorBody>();

if (code === 'hook.send_test_payload_failed') {
setResult({
result: 'error',
endpointUrl,
requestTime,
message,
});
return;
}

if (code === 'hook.endpoint_responded_with_error') {
const { responseStatus, responseBody } =
trySafe(() => hookTestErrorResponseDataGuard.parse(data)) ?? {};

setResult({
result: 'error',
endpointUrl,
requestTime,
message,
responseStatus,
responseBody,
});

return;
}
}

throw error;
Expand Down

0 comments on commit dc6fbe2

Please sign in to comment.