Prevent logging an error on lazy static 404

This commit is contained in:
Chocobozzz 2022-07-27 14:38:07 +02:00
parent e3d6c6434f
commit eb7b48ce84
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
3 changed files with 29 additions and 25 deletions

View File

@ -7,7 +7,7 @@ import { logger } from '../helpers/logger'
import { ACTOR_IMAGES_SIZE, LAZY_STATIC_PATHS, STATIC_MAX_AGE } from '../initializers/constants' import { ACTOR_IMAGES_SIZE, LAZY_STATIC_PATHS, STATIC_MAX_AGE } from '../initializers/constants'
import { VideosCaptionCache, VideosPreviewCache } from '../lib/files-cache' import { VideosCaptionCache, VideosPreviewCache } from '../lib/files-cache'
import { actorImagePathUnsafeCache, downloadActorImageFromWorker } from '../lib/local-actor' import { actorImagePathUnsafeCache, downloadActorImageFromWorker } from '../lib/local-actor'
import { asyncMiddleware } from '../middlewares' import { asyncMiddleware, handleStaticError } from '../middlewares'
import { ActorImageModel } from '../models/actor/actor-image' import { ActorImageModel } from '../models/actor/actor-image'
const lazyStaticRouter = express.Router() const lazyStaticRouter = express.Router()
@ -16,27 +16,32 @@ lazyStaticRouter.use(cors())
lazyStaticRouter.use( lazyStaticRouter.use(
LAZY_STATIC_PATHS.AVATARS + ':filename', LAZY_STATIC_PATHS.AVATARS + ':filename',
asyncMiddleware(getActorImage) asyncMiddleware(getActorImage),
handleStaticError
) )
lazyStaticRouter.use( lazyStaticRouter.use(
LAZY_STATIC_PATHS.BANNERS + ':filename', LAZY_STATIC_PATHS.BANNERS + ':filename',
asyncMiddleware(getActorImage) asyncMiddleware(getActorImage),
handleStaticError
) )
lazyStaticRouter.use( lazyStaticRouter.use(
LAZY_STATIC_PATHS.PREVIEWS + ':filename', LAZY_STATIC_PATHS.PREVIEWS + ':filename',
asyncMiddleware(getPreview) asyncMiddleware(getPreview),
handleStaticError
) )
lazyStaticRouter.use( lazyStaticRouter.use(
LAZY_STATIC_PATHS.VIDEO_CAPTIONS + ':filename', LAZY_STATIC_PATHS.VIDEO_CAPTIONS + ':filename',
asyncMiddleware(getVideoCaption) asyncMiddleware(getVideoCaption),
handleStaticError
) )
lazyStaticRouter.use( lazyStaticRouter.use(
LAZY_STATIC_PATHS.TORRENTS + ':filename', LAZY_STATIC_PATHS.TORRENTS + ':filename',
asyncMiddleware(getTorrent) asyncMiddleware(getTorrent),
handleStaticError
) )
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------

View File

@ -1,6 +1,6 @@
import { HttpStatusCode } from '@shared/models'
import cors from 'cors' import cors from 'cors'
import express from 'express' import express from 'express'
import { handleStaticError } from '@server/middlewares'
import { CONFIG } from '../initializers/config' import { CONFIG } from '../initializers/config'
import { HLS_STREAMING_PLAYLIST_DIRECTORY, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers/constants' import { HLS_STREAMING_PLAYLIST_DIRECTORY, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers/constants'
@ -41,19 +41,3 @@ staticRouter.use(
export { export {
staticRouter staticRouter
} }
// ---------------------------------------------------------------------------
function handleStaticError (err: any, req: express.Request, res: express.Response, next: express.NextFunction) {
const message = err.message || ''
if (message.includes('ENOENT')) {
return res.fail({
status: err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500,
message: err.message,
type: err.name
})
}
return next(err)
}

View File

@ -40,6 +40,21 @@ function apiFailMiddleware (req: express.Request, res: express.Response, next: e
if (next) next() if (next) next()
} }
export { function handleStaticError (err: any, req: express.Request, res: express.Response, next: express.NextFunction) {
apiFailMiddleware const message = err.message || ''
if (message.includes('ENOENT')) {
return res.fail({
status: err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500,
message: err.message,
type: err.name
})
}
return next(err)
}
export {
apiFailMiddleware,
handleStaticError
} }