refactor: remove console logs and use getHostname and ApiErrorCodes

This commit is contained in:
fallenbagel
2024-05-26 19:24:01 +05:00
parent 135155cd85
commit e34881064a
6 changed files with 42 additions and 40 deletions

View File

@@ -645,8 +645,6 @@ class Settings {
const oldJellyfinSettings = JSON.parse(data).jellyfin;
// Migrate old settings
// TODO: Test this migration and the regex
console.log(oldJellyfinSettings);
if (oldJellyfinSettings && oldJellyfinSettings.hostname) {
// migrate old jellyfin hostname to ip and port and useSsl
@@ -683,8 +681,6 @@ class Settings {
delete oldJellyfinSettings.hostname;
console.log(this.data.jellyfin, oldJellyfinSettings.hostname);
this.data.jellyfin = Object.assign(
{},
this.data.jellyfin,

View File

@@ -443,9 +443,12 @@ authRoutes.post('/jellyfin', async (req, res, next) => {
label: 'Auth',
error: e.errorCode,
status: e.statusCode,
hostname: `${body.useSsl ? 'https' : 'http'}://${body.hostname}:${
body.port
}${body.urlBase}`,
hostname: getHostname({
useSsl: body.useSsl,
ip: body.hostname,
port: body.port,
urlBase: body.urlBase,
}),
}
);
return next({

View File

@@ -25,6 +25,7 @@ import { getSettings } from '@server/lib/settings';
import logger from '@server/logger';
import { isAuthenticated } from '@server/middleware/auth';
import discoverSettingRoutes from '@server/routes/settings/discover';
import { ApiError } from '@server/types/error';
import { appDataPath } from '@server/utils/appDataVolume';
import { getAppVersion } from '@server/utils/appVersion';
import { getHostname } from '@server/utils/getHostname';
@@ -275,11 +276,8 @@ settingsRoutes.post('/jellyfin', async (req, res, next) => {
const result = await jellyfinClient.getSystemInfo();
console.log(result);
// TODO: use the apiErrorCodes
if (!result?.data?.Id) {
throw new Error('Server not found');
throw new ApiError(result?.status, ApiErrorCode.InvalidUrl);
}
settings.jellyfin.serverId = result.Id;
@@ -364,12 +362,11 @@ settingsRoutes.get('/jellyfin/library', async (req, res, next) => {
});
settingsRoutes.get('/jellyfin/users', async (req, res) => {
const { ip, port, useSsl, urlBase, externalHostname } =
getSettings().jellyfin;
const { externalHostname } = getSettings().jellyfin;
const jellyfinHost =
externalHostname && externalHostname.length > 0
? externalHostname
: `${useSsl ? 'https' : 'http'}://${ip}:${port}${urlBase}`;
: getHostname();
const userRepository = getRepository(User);
const admin = await userRepository.findOneOrFail({

View File

@@ -20,6 +20,7 @@ import { hasPermission, Permission } from '@server/lib/permissions';
import { getSettings } from '@server/lib/settings';
import logger from '@server/logger';
import { isAuthenticated } from '@server/middleware/auth';
import { getHostname } from '@server/utils/getHostname';
import { Router } from 'express';
import gravatarUrl from 'gravatar-url';
import { findIndex, sortBy } from 'lodash';
@@ -503,9 +504,8 @@ router.post(
//const jellyfinUsersResponse = await jellyfinClient.getUsers();
const createdUsers: User[] = [];
const { ip, port, urlBase, useSsl, externalHostname } =
getSettings().jellyfin;
const hostname = `${useSsl ? 'https' : 'http'}://${ip}:${port}${urlBase}`;
const { externalHostname } = getSettings().jellyfin;
const hostname = getHostname();
const jellyfinHost =
externalHostname && externalHostname.length > 0

View File

@@ -126,7 +126,6 @@ const JellyfinLogin: React.FC<JellyfinLoginProps> = ({
email: values.email,
});
} catch (e) {
console.log(e);
let errorMessage = null;
switch (e.response?.data?.message) {
case ApiErrorCode.InvalidUrl:

View File

@@ -4,6 +4,7 @@ import LoadingSpinner from '@app/components/Common/LoadingSpinner';
import LibraryItem from '@app/components/Settings/LibraryItem';
import globalMessages from '@app/i18n/globalMessages';
import { ArrowDownOnSquareIcon } from '@heroicons/react/24/outline';
import { ApiErrorCode } from '@server/constants/error';
import type { JellyfinSettings } from '@server/lib/settings';
import axios from 'axios';
import { Field, Formik } from 'formik';
@@ -43,7 +44,7 @@ const messages = defineMessages({
'Custom authentication with Automatic Library Grouping not supported',
jellyfinSyncFailedGenericError:
'Something went wrong while syncing libraries',
invalidurlerror: 'Unable to connect to {mediaServerName} server.',
syncing: 'Syncing',
syncJellyfin: 'Sync Libraries',
manualscanJellyfin: 'Manual Library Scan',
@@ -148,15 +149,6 @@ const SettingsJellyfin: React.FC<SettingsJellyfinProps> = ({
intl.formatMessage(messages.validationUrlTrailingSlash),
(value) => !value || !value.endsWith('/')
),
// jellyfinInternalUrl: Yup.string().matches(
// /^(https?:\/\/)?(?:[\w-]+\.)*[\w-]+(?::\d{2,5})?(?:\/[\w-]+)*(?:\/)?$/gm,
// intl.formatMessage(messages.validationUrl)
// ),
// jellyfinForgotPasswordUrl: Yup.string().matches(
// /^(https?:\/\/)?(?:[\w-]+\.)*[\w-]+(?::\d{2,5})?(?:\/[\w-]+)*(?:\/)?$/gm,
// intl.formatMessage(messages.validationUrl)
// ),
});
const activeLibraries =
@@ -479,18 +471,33 @@ const SettingsJellyfin: React.FC<SettingsJellyfinProps> = ({
}
);
} catch (e) {
addToast(
intl.formatMessage(messages.jellyfinSettingsFailure, {
mediaServerName:
publicRuntimeConfig.JELLYFIN_TYPE == 'emby'
? 'Emby'
: 'Jellyfin',
}),
{
autoDismiss: true,
appearance: 'error',
}
);
if (e.response?.data?.message === ApiErrorCode.InvalidUrl) {
addToast(
intl.formatMessage(messages.invalidurlerror, {
mediaServerName:
publicRuntimeConfig.JELLYFIN_TYPE == 'emby'
? 'Emby'
: 'Jellyfin',
}),
{
autoDismiss: true,
appearance: 'error',
}
);
} else {
addToast(
intl.formatMessage(messages.jellyfinSettingsFailure, {
mediaServerName:
publicRuntimeConfig.JELLYFIN_TYPE == 'emby'
? 'Emby'
: 'Jellyfin',
}),
{
autoDismiss: true,
appearance: 'error',
}
);
}
} finally {
revalidate();
}