From 07c98a21e05765ff1f76ceaada3ecd86619c974d Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 15 Nov 2024 14:11:39 -0600 Subject: [PATCH] MastodonClient: fix HTTP 204 responses --- src/api/MastodonClient.ts | 6 ++++++ src/api/hooks/captcha/useCaptcha.ts | 11 ++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/api/MastodonClient.ts b/src/api/MastodonClient.ts index a41036b27..3fa46f4bf 100644 --- a/src/api/MastodonClient.ts +++ b/src/api/MastodonClient.ts @@ -90,6 +90,12 @@ export class MastodonClient { throw new HTTPError(response, request); } + // Fix for non-compliant browsers. + // https://developer.mozilla.org/en-US/docs/Web/API/Response/body + if (response.status === 204 || request.method === 'HEAD') { + return new MastodonResponse(null, response); + } + return new MastodonResponse(response.body, response); } diff --git a/src/api/hooks/captcha/useCaptcha.ts b/src/api/hooks/captcha/useCaptcha.ts index d5d9e6339..2463b869f 100644 --- a/src/api/hooks/captcha/useCaptcha.ts +++ b/src/api/hooks/captcha/useCaptcha.ts @@ -1,8 +1,8 @@ -import { AxiosError } from 'axios'; import { useEffect, useState } from 'react'; import { defineMessages, useIntl } from 'react-intl'; import { closeModal } from 'soapbox/actions/modals.ts'; +import { HTTPError } from 'soapbox/api/HTTPError.ts'; import { useApi } from 'soapbox/hooks/useApi.ts'; import { useAppDispatch } from 'soapbox/hooks/useAppDispatch.ts'; import { useInstance } from 'soapbox/hooks/useInstance.ts'; @@ -43,7 +43,7 @@ const useCaptcha = () => { setYPosition(topI); setXPosition(leftI); } catch (error) { - toast.error('Error loading captcha:'); + toast.error('Error loading captcha'); } }; @@ -72,11 +72,10 @@ const useCaptcha = () => { dispatch(closeModal('CAPTCHA')); toast.success(messages.sucessMessage); }); - } catch (e) { + } catch (error) { setTryAgain(true); - const error = e as AxiosError; - const status = error.request?.status; + const status = error instanceof HTTPError ? error.response.status : undefined; let message; switch (status) { @@ -88,8 +87,10 @@ const useCaptcha = () => { break; default: message = intl.formatMessage(messages.errorMessage); + console.error(error); break; } + toast.error(message); } setIsSubmitting(false);