refactor(app): remove RequestError class

This commit is contained in:
Michael Thomas
2024-11-12 12:15:12 -05:00
parent 4db1d75e22
commit 033ccac813
3 changed files with 28 additions and 39 deletions

View File

@@ -2,7 +2,6 @@ import Alert from '@app/components/Common/Alert';
import Modal from '@app/components/Common/Modal';
import useSettings from '@app/hooks/useSettings';
import { useUser } from '@app/hooks/useUser';
import { RequestError } from '@app/types/error';
import defineMessages from '@app/utils/defineMessages';
import { Transition } from '@headlessui/react';
import { MediaServerType } from '@server/constants/server';
@@ -94,23 +93,25 @@ const LinkJellyfinModal: React.FC<LinkJellyfinModalProps> = ({
}),
}
);
if (!res.ok) throw new RequestError(res);
onSave();
} catch (e) {
if (e instanceof RequestError && e.status == 401) {
setError(
intl.formatMessage(messages.errorUnauthorized, {
mediaServerName,
})
);
} else if (e instanceof RequestError && e.status == 422) {
setError(
intl.formatMessage(messages.errorExists, { applicationName })
);
if (!res.ok) {
if (res.status === 401) {
setError(
intl.formatMessage(messages.errorUnauthorized, {
mediaServerName,
})
);
} else if (res.status === 422) {
setError(
intl.formatMessage(messages.errorExists, { applicationName })
);
} else {
setError(intl.formatMessage(messages.errorUnknown));
}
} else {
setError(intl.formatMessage(messages.errorUnknown));
onSave();
}
} catch (e) {
setError(intl.formatMessage(messages.errorUnknown));
}
}}
>

View File

@@ -8,7 +8,6 @@ import PageTitle from '@app/components/Common/PageTitle';
import useSettings from '@app/hooks/useSettings';
import { Permission, UserType, useUser } from '@app/hooks/useUser';
import globalMessages from '@app/i18n/globalMessages';
import { RequestError } from '@app/types/error';
import defineMessages from '@app/utils/defineMessages';
import PlexOAuth from '@app/utils/plex';
import { TrashIcon } from '@heroicons/react/24/solid';
@@ -100,18 +99,18 @@ const UserLinkedAccountsSettings = () => {
}
);
if (!res.ok) {
throw new RequestError(res);
}
await revalidateUser();
} catch (e) {
if (e instanceof RequestError && e.status === 401) {
setError(intl.formatMessage(messages.plexErrorUnauthorized));
} else if (e instanceof RequestError && e.status === 422) {
setError(intl.formatMessage(messages.plexErrorExists));
if (res.status === 401) {
setError(intl.formatMessage(messages.plexErrorUnauthorized));
} else if (res.status === 422) {
setError(intl.formatMessage(messages.plexErrorExists));
} else {
setError(intl.formatMessage(messages.errorUnknown));
}
} else {
setError(intl.formatMessage(messages.errorServer));
await revalidateUser();
}
} catch (e) {
setError(intl.formatMessage(messages.errorUnknown));
}
};
@@ -169,7 +168,7 @@ const UserLinkedAccountsSettings = () => {
</h3>
</div>
<Alert
title={intl.formatMessage(messages.nopermissionDescription)}
title={intl.formatMessage(messages.noPermissionDescription)}
type="error"
/>
</>

View File

@@ -1,11 +0,0 @@
export class RequestError extends Error {
status: number;
res: Response;
constructor(res: Response) {
const status = res.status;
super(`Request failed with status code ${status}`);
this.status = status;
this.res = res;
}
}