0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-13 21:30:30 -05:00

refactor(console): improve webhook test request error handling (#6017)

This commit is contained in:
Xiao Yijun 2024-06-15 11:21:44 +08:00 committed by GitHub
parent 15a51a4426
commit dc6fbe212e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4,7 +4,6 @@ import dayjs from 'dayjs';
import { HTTPError } from 'ky'; import { HTTPError } from 'ky';
import { useRef, useState } from 'react'; import { useRef, useState } from 'react';
import { useFormContext } from 'react-hook-form'; import { useFormContext } from 'react-hook-form';
import { toast } from 'react-hot-toast';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import Button from '@/ds-components/Button'; import Button from '@/ds-components/Button';
@ -33,7 +32,9 @@ function TestWebhook({ hookId }: Props) {
const { result, setResult } = useWebhookTestResult(); const { result, setResult } = useWebhookTestResult();
const [isSendingPayload, setIsSendingPayload] = useState(false); 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 () => { const sendTestPayload = async () => {
if (isSendingPayload) { if (isSendingPayload) {
@ -57,37 +58,34 @@ function TestWebhook({ hookId }: Props) {
requestTime, requestTime,
}); });
} catch (error: unknown) { } catch (error: unknown) {
if (!(error instanceof HTTPError)) { if (error instanceof HTTPError) {
toast.error(error instanceof Error ? String(error) : t('general.unknown_error')); const { code, data, message } = await error.response.clone().json<RequestErrorBody>();
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.send_test_payload_failed') { if (code === 'hook.endpoint_responded_with_error') {
setResult({ const { responseStatus, responseBody } =
result: 'error', trySafe(() => hookTestErrorResponseDataGuard.parse(data)) ?? {};
endpointUrl,
requestTime,
message,
});
return;
}
if (code === 'hook.endpoint_responded_with_error') { setResult({
const { responseStatus, responseBody } = result: 'error',
trySafe(() => hookTestErrorResponseDataGuard.parse(data)) ?? {}; endpointUrl,
requestTime,
message,
responseStatus,
responseBody,
});
setResult({ return;
result: 'error', }
endpointUrl,
requestTime,
message,
responseStatus,
responseBody,
});
return;
} }
throw error; throw error;