Skip to content

Commit

Permalink
refactor(jellyfinapi): improve logging of jellyfin api
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Fallenbagel committed Nov 12, 2024
1 parent 694913c commit 17f2f43
Showing 1 changed file with 45 additions and 43 deletions.
88 changes: 45 additions & 43 deletions server/api/jellyfin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,39 +138,38 @@ class JellyfinAPI extends ExternalAPI {
try {
return await authenticate(true);
} catch (e) {
logger.debug(`Failed to authenticate with headers: ${e.message}`, {
logger.debug('Failed to authenticate with headers', {
label: 'Jellyfin API',
error: e.cause.message ?? e.cause.statusText,
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 {
return await authenticate(false);
} catch (e) {
const status = e.cause?.status;

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);
if (e.cause.status === 401) {
throw new ApiError(e.cause.status, ApiErrorCode.InvalidCredentials);
}

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

Expand Down Expand Up @@ -198,8 +197,8 @@ class JellyfinAPI extends ExternalAPI {
return serverResponse.ServerName;
} catch (e) {
logger.error(
`Something went wrong while getting the server name from the Jellyfin server: ${e.message}`,
{ label: 'Jellyfin API' }
'Something went wrong while getting the server name from the Jellyfin server',
{ label: 'Jellyfin API', error: e.cause.message ?? e.cause.statusText }
);

throw new ApiError(e.cause?.status, ApiErrorCode.Unknown);
Expand All @@ -213,8 +212,8 @@ class JellyfinAPI extends ExternalAPI {
return { users: userReponse };
} catch (e) {
logger.error(
`Something went wrong while getting the account from the Jellyfin server: ${e.message}`,
{ label: 'Jellyfin API' }
'Something went wrong while getting the account from the Jellyfin server',
{ label: 'Jellyfin API', error: e.cause.message ?? e.cause.statusText }
);

throw new ApiError(e.cause?.status, ApiErrorCode.InvalidAuthToken);
Expand All @@ -229,8 +228,8 @@ class JellyfinAPI extends ExternalAPI {
return userReponse;
} catch (e) {
logger.error(
`Something went wrong while getting the account from the Jellyfin server: ${e.message}`,
{ label: 'Jellyfin API' }
'Something went wrong while getting the account from the Jellyfin server',
{ label: 'Jellyfin API', error: e.cause.message ?? e.cause.statusText }
);

throw new ApiError(e.cause?.status, ApiErrorCode.InvalidAuthToken);
Expand All @@ -253,8 +252,11 @@ class JellyfinAPI extends ExternalAPI {
return this.mapLibraries(mediaFolderResponse.Items);
} catch (e) {
logger.error(
`Something went wrong while getting libraries from the Jellyfin server: ${e.message}`,
{ label: 'Jellyfin API' }
'Something went wrong while getting libraries from the Jellyfin server',
{
label: 'Jellyfin API',
error: e.cause.message ?? e.cause.statusText,
}
);

return [];
Expand Down Expand Up @@ -308,8 +310,8 @@ class JellyfinAPI extends ExternalAPI {
);
} catch (e) {
logger.error(
`Something went wrong while getting library content from the Jellyfin server: ${e.message}`,
{ label: 'Jellyfin API' }
'Something went wrong while getting library content from the Jellyfin server',
{ label: 'Jellyfin API', error: e.cause.message ?? e.cause.statusText }
);

throw new ApiError(e.cause?.status, ApiErrorCode.InvalidAuthToken);
Expand All @@ -329,8 +331,8 @@ class JellyfinAPI extends ExternalAPI {
return itemResponse;
} catch (e) {
logger.error(
`Something went wrong while getting library content from the Jellyfin server: ${e.message}`,
{ label: 'Jellyfin API' }
'Something went wrong while getting library content from the Jellyfin server',
{ label: 'Jellyfin API', error: e.cause.message ?? e.cause.statusText }
);

throw new ApiError(e.cause?.status, ApiErrorCode.InvalidAuthToken);
Expand All @@ -354,8 +356,8 @@ class JellyfinAPI extends ExternalAPI {
}

logger.error(
`Something went wrong while getting library content from the Jellyfin server: ${e.message}`,
{ label: 'Jellyfin API' }
'Something went wrong while getting library content from the Jellyfin server',
{ label: 'Jellyfin API', error: e.cause.message ?? e.cause.statusText }
);
throw new ApiError(e.cause?.status, ApiErrorCode.InvalidAuthToken);
}
Expand All @@ -368,8 +370,8 @@ class JellyfinAPI extends ExternalAPI {
return seasonResponse.Items;
} catch (e) {
logger.error(
`Something went wrong while getting the list of seasons from the Jellyfin server: ${e.message}`,
{ label: 'Jellyfin API' }
'Something went wrong while getting the list of seasons from the Jellyfin server',
{ label: 'Jellyfin API', error: e.cause.message ?? e.cause.statusText }
);

throw new ApiError(e.cause?.status, ApiErrorCode.InvalidAuthToken);
Expand All @@ -393,8 +395,8 @@ class JellyfinAPI extends ExternalAPI {
);
} catch (e) {
logger.error(
`Something went wrong while getting the list of episodes from the Jellyfin server: ${e.message}`,
{ label: 'Jellyfin API' }
'Something went wrong while getting the list of episodes from the Jellyfin server',
{ label: 'Jellyfin API', error: e.cause.message ?? e.cause.statusText }
);

throw new ApiError(e.cause?.status, ApiErrorCode.InvalidAuthToken);
Expand All @@ -410,8 +412,8 @@ class JellyfinAPI extends ExternalAPI {
).AccessToken;
} catch (e) {
logger.error(
`Something went wrong while creating an API key from the Jellyfin server: ${e.message}`,
{ label: 'Jellyfin API' }
'Something went wrong while creating an API key from the Jellyfin server',
{ label: 'Jellyfin API', error: e.cause.message ?? e.cause.statusText }
);

throw new ApiError(e.response?.status, ApiErrorCode.InvalidAuthToken);
Expand Down

0 comments on commit 17f2f43

Please sign in to comment.