From e157a69d86444723eddefeeb66f65d46d8f5d405 Mon Sep 17 00:00:00 2001 From: Michel Heusschen <59014050+michelheusschen@users.noreply.github.com> Date: Sun, 26 Feb 2023 17:50:18 +0100 Subject: [PATCH] fix(web): don't log unauthorized errors (#1871) * fix(web): don't log unauthorized errors * fix docker build error --- web/src/hooks.server.ts | 24 ++++++++++++++++++++---- web/src/routes/+layout.server.ts | 11 ++--------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/web/src/hooks.server.ts b/web/src/hooks.server.ts index 532e1ca813..65a4912e75 100644 --- a/web/src/hooks.server.ts +++ b/web/src/hooks.server.ts @@ -4,10 +4,26 @@ import { env } from '$env/dynamic/public'; import { ImmichApi } from './api/api'; export const handle = (async ({ event, resolve }) => { - event.locals.api = new ImmichApi({ - basePath: env.PUBLIC_IMMICH_SERVER_URL || 'http://immich-server:3001', - accessToken: event.cookies.get('immich_access_token') - }); + const basePath = env.PUBLIC_IMMICH_SERVER_URL || 'http://immich-server:3001'; + const accessToken = event.cookies.get('immich_access_token'); + const api = new ImmichApi({ basePath, accessToken }); + + // API instance that should be used for all server-side requests. + event.locals.api = api; + + if (accessToken) { + try { + const { data: user } = await api.userApi.getMyUserInfo(); + event.locals.user = user; + } catch (err) { + const apiError = err as AxiosError; + + // Ignore 401 unauthorized errors and log all others. + if (apiError.response?.status !== 401) { + console.error('[ERROR] hooks.server.ts [handle]:', err); + } + } + } const res = await resolve(event); diff --git a/web/src/routes/+layout.server.ts b/web/src/routes/+layout.server.ts index cb7b4006f3..08b7f2518c 100644 --- a/web/src/routes/+layout.server.ts +++ b/web/src/routes/+layout.server.ts @@ -1,12 +1,5 @@ import type { LayoutServerLoad } from './$types'; -export const load = (async ({ locals: { api } }) => { - try { - const { data: user } = await api.userApi.getMyUserInfo(); - - return { user }; - } catch (e) { - console.error('[ERROR] layout.server.ts [LayoutServerLoad]: '); - return { user: undefined }; - } +export const load = (async ({ locals: { user } }) => { + return { user }; }) satisfies LayoutServerLoad;