MastodonClient: fix HTTP 204 responses

This commit is contained in:
Alex Gleason 2024-11-15 14:11:39 -06:00
parent b07bcd3ead
commit 07c98a21e0
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 12 additions and 5 deletions

View File

@ -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);
}

View File

@ -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);