refactor(jellyfinapi): improve logging of jellyfin api (#1089)

This commit improves the logging of jellyfin api. This will also fix the wrong logging of throwing
an invalid credentials when jellyfin/emby is unreachable.

re #1053
This commit is contained in:
Fallenbagel
2024-11-13 05:26:56 +08:00
committed by GitHub
parent 694913c767
commit 5a13226877

View File

@@ -138,39 +138,38 @@ class JellyfinAPI extends ExternalAPI {
try { try {
return await authenticate(true); return await authenticate(true);
} catch (e) { } catch (e) {
logger.debug(`Failed to authenticate with headers: ${e.message}`, { logger.debug('Failed to authenticate with headers', {
label: 'Jellyfin API', label: 'Jellyfin API',
error: e.cause.message ?? e.cause.statusText,
ip: ClientIP, ip: ClientIP,
}); });
if (!e.cause.status) {
throw new ApiError(404, ApiErrorCode.InvalidUrl);
}
if (e.cause.status === 401) {
throw new ApiError(e.cause.status, ApiErrorCode.InvalidCredentials);
}
} }
try { try {
return await authenticate(false); return await authenticate(false);
} catch (e) { } catch (e) {
const status = e.cause?.status; if (e.cause.status === 401) {
throw new ApiError(e.cause.status, ApiErrorCode.InvalidCredentials);
const networkErrorCodes = new Set([
'ECONNREFUSED',
'EHOSTUNREACH',
'ENOTFOUND',
'ETIMEDOUT',
'ECONNRESET',
'EADDRINUSE',
'ENETDOWN',
'ENETUNREACH',
'EPIPE',
'ECONNABORTED',
'EPROTO',
'EHOSTDOWN',
'EAI_AGAIN',
'ERR_INVALID_URL',
]);
if (networkErrorCodes.has(e.code) || status === 404) {
throw new ApiError(status, ApiErrorCode.InvalidUrl);
} }
throw new ApiError(status, ApiErrorCode.InvalidCredentials); logger.error(
'Something went wrong while authenticating with the Jellyfin server',
{
label: 'Jellyfin API',
error: e.cause.message ?? e.cause.statusText,
ip: ClientIP,
}
);
throw new ApiError(e.cause.status, ApiErrorCode.Unknown);
} }
} }
@@ -198,8 +197,8 @@ class JellyfinAPI extends ExternalAPI {
return serverResponse.ServerName; return serverResponse.ServerName;
} catch (e) { } catch (e) {
logger.error( logger.error(
`Something went wrong while getting the server name from the Jellyfin server: ${e.message}`, 'Something went wrong while getting the server name from the Jellyfin server',
{ label: 'Jellyfin API' } { label: 'Jellyfin API', error: e.cause.message ?? e.cause.statusText }
); );
throw new ApiError(e.cause?.status, ApiErrorCode.Unknown); throw new ApiError(e.cause?.status, ApiErrorCode.Unknown);
@@ -213,8 +212,8 @@ class JellyfinAPI extends ExternalAPI {
return { users: userReponse }; return { users: userReponse };
} catch (e) { } catch (e) {
logger.error( logger.error(
`Something went wrong while getting the account from the Jellyfin server: ${e.message}`, 'Something went wrong while getting the account from the Jellyfin server',
{ label: 'Jellyfin API' } { label: 'Jellyfin API', error: e.cause.message ?? e.cause.statusText }
); );
throw new ApiError(e.cause?.status, ApiErrorCode.InvalidAuthToken); throw new ApiError(e.cause?.status, ApiErrorCode.InvalidAuthToken);
@@ -229,8 +228,8 @@ class JellyfinAPI extends ExternalAPI {
return userReponse; return userReponse;
} catch (e) { } catch (e) {
logger.error( logger.error(
`Something went wrong while getting the account from the Jellyfin server: ${e.message}`, 'Something went wrong while getting the account from the Jellyfin server',
{ label: 'Jellyfin API' } { label: 'Jellyfin API', error: e.cause.message ?? e.cause.statusText }
); );
throw new ApiError(e.cause?.status, ApiErrorCode.InvalidAuthToken); throw new ApiError(e.cause?.status, ApiErrorCode.InvalidAuthToken);
@@ -253,8 +252,11 @@ class JellyfinAPI extends ExternalAPI {
return this.mapLibraries(mediaFolderResponse.Items); return this.mapLibraries(mediaFolderResponse.Items);
} catch (e) { } catch (e) {
logger.error( logger.error(
`Something went wrong while getting libraries from the Jellyfin server: ${e.message}`, 'Something went wrong while getting libraries from the Jellyfin server',
{ label: 'Jellyfin API' } {
label: 'Jellyfin API',
error: e.cause.message ?? e.cause.statusText,
}
); );
return []; return [];
@@ -308,8 +310,8 @@ class JellyfinAPI extends ExternalAPI {
); );
} catch (e) { } catch (e) {
logger.error( logger.error(
`Something went wrong while getting library content from the Jellyfin server: ${e.message}`, 'Something went wrong while getting library content from the Jellyfin server',
{ label: 'Jellyfin API' } { label: 'Jellyfin API', error: e.cause.message ?? e.cause.statusText }
); );
throw new ApiError(e.cause?.status, ApiErrorCode.InvalidAuthToken); throw new ApiError(e.cause?.status, ApiErrorCode.InvalidAuthToken);
@@ -329,8 +331,8 @@ class JellyfinAPI extends ExternalAPI {
return itemResponse; return itemResponse;
} catch (e) { } catch (e) {
logger.error( logger.error(
`Something went wrong while getting library content from the Jellyfin server: ${e.message}`, 'Something went wrong while getting library content from the Jellyfin server',
{ label: 'Jellyfin API' } { label: 'Jellyfin API', error: e.cause.message ?? e.cause.statusText }
); );
throw new ApiError(e.cause?.status, ApiErrorCode.InvalidAuthToken); throw new ApiError(e.cause?.status, ApiErrorCode.InvalidAuthToken);
@@ -354,8 +356,8 @@ class JellyfinAPI extends ExternalAPI {
} }
logger.error( logger.error(
`Something went wrong while getting library content from the Jellyfin server: ${e.message}`, 'Something went wrong while getting library content from the Jellyfin server',
{ label: 'Jellyfin API' } { label: 'Jellyfin API', error: e.cause.message ?? e.cause.statusText }
); );
throw new ApiError(e.cause?.status, ApiErrorCode.InvalidAuthToken); throw new ApiError(e.cause?.status, ApiErrorCode.InvalidAuthToken);
} }
@@ -368,8 +370,8 @@ class JellyfinAPI extends ExternalAPI {
return seasonResponse.Items; return seasonResponse.Items;
} catch (e) { } catch (e) {
logger.error( logger.error(
`Something went wrong while getting the list of seasons from the Jellyfin server: ${e.message}`, 'Something went wrong while getting the list of seasons from the Jellyfin server',
{ label: 'Jellyfin API' } { label: 'Jellyfin API', error: e.cause.message ?? e.cause.statusText }
); );
throw new ApiError(e.cause?.status, ApiErrorCode.InvalidAuthToken); throw new ApiError(e.cause?.status, ApiErrorCode.InvalidAuthToken);
@@ -393,8 +395,8 @@ class JellyfinAPI extends ExternalAPI {
); );
} catch (e) { } catch (e) {
logger.error( logger.error(
`Something went wrong while getting the list of episodes from the Jellyfin server: ${e.message}`, 'Something went wrong while getting the list of episodes from the Jellyfin server',
{ label: 'Jellyfin API' } { label: 'Jellyfin API', error: e.cause.message ?? e.cause.statusText }
); );
throw new ApiError(e.cause?.status, ApiErrorCode.InvalidAuthToken); throw new ApiError(e.cause?.status, ApiErrorCode.InvalidAuthToken);
@@ -410,8 +412,8 @@ class JellyfinAPI extends ExternalAPI {
).AccessToken; ).AccessToken;
} catch (e) { } catch (e) {
logger.error( logger.error(
`Something went wrong while creating an API key from the Jellyfin server: ${e.message}`, 'Something went wrong while creating an API key from the Jellyfin server',
{ label: 'Jellyfin API' } { label: 'Jellyfin API', error: e.cause.message ?? e.cause.statusText }
); );
throw new ApiError(e.response?.status, ApiErrorCode.InvalidAuthToken); throw new ApiError(e.response?.status, ApiErrorCode.InvalidAuthToken);