diff --git a/backend/src/config/late/info.config.service.ts b/backend/src/config/late/info.config.service.ts index fea9b2c..891aadc 100644 --- a/backend/src/config/late/info.config.service.ts +++ b/backend/src/config/late/info.config.service.ts @@ -14,7 +14,7 @@ export class InfoConfigService { SysPreference.HostOverride, ); if (HasFailed(hostname)) { - this.logger.warn(hostname.print()); + hostname.print(this.logger); return undefined; } diff --git a/backend/src/decorators/multipart/postfiles.pipe.ts b/backend/src/decorators/multipart/postfiles.pipe.ts index 3a3f18f..dd80b39 100644 --- a/backend/src/decorators/multipart/postfiles.pipe.ts +++ b/backend/src/decorators/multipart/postfiles.pipe.ts @@ -26,7 +26,7 @@ export class MultiPartPipe implements PipeTransform { ) { const filesLimit = typeof data === 'number' ? data : undefined; - if (!request.isMultipart()) throw Fail(FT.UsrValidation, 'Invalid file'); + if (!request.isMultipart()) throw Fail(FT.UsrValidation, 'Invalid files'); const files = request.files({ limits: this.multipartConfigService.getLimits(filesLimit), diff --git a/backend/src/layers/exception/exception.filter.ts b/backend/src/layers/exception/exception.filter.ts index a7de68a..8ea90b3 100644 --- a/backend/src/layers/exception/exception.filter.ts +++ b/backend/src/layers/exception/exception.filter.ts @@ -6,7 +6,7 @@ import { Logger, MethodNotAllowedException, NotFoundException, - UnauthorizedException, + UnauthorizedException } from '@nestjs/common'; import { FastifyReply, FastifyRequest } from 'fastify'; import { ApiErrorResponse } from 'picsur-shared/dist/dto/api/api.dto'; @@ -14,7 +14,7 @@ import { Fail, Failure, FT, - IsFailure, + IsFailure } from 'picsur-shared/dist/types/failable'; // This will catch any exception that is made in any request @@ -39,23 +39,7 @@ export class MainExceptionFilter implements ExceptionFilter { const status = exception.getCode(); const type = exception.getType(); - const message = exception.getReason(); - const logmessage = - message + - (exception.getDebugMessage() ? ' - ' + exception.getDebugMessage() : ''); - - if (exception.isImportant()) { - MainExceptionFilter.logger.error( - `${traceString} ${exception.getName()}: ${logmessage}`, - ); - if (exception.getStack()) { - MainExceptionFilter.logger.debug(exception.getStack()); - } - } else { - MainExceptionFilter.logger.warn( - `${traceString} ${exception.getName()}: ${logmessage}`, - ); - } + exception.print(MainExceptionFilter.logger, { prefix: traceString }); const toSend: ApiErrorResponse = { success: false, @@ -65,7 +49,7 @@ export class MainExceptionFilter implements ExceptionFilter { data: { type, - message, + message: exception.getReason(), }, }; diff --git a/backend/src/main.ts b/backend/src/main.ts index 815c771..10864e2 100644 --- a/backend/src/main.ts +++ b/backend/src/main.ts @@ -4,7 +4,7 @@ import fastifyReplyFrom from '@fastify/reply-from'; import { NestFactory } from '@nestjs/core'; import { FastifyAdapter, - NestFastifyApplication, + NestFastifyApplication } from '@nestjs/platform-fastify'; import { AppModule } from './app.module'; import { HostConfigService } from './config/early/host.config.service'; @@ -39,14 +39,17 @@ async function bootstrap() { fastifyAdapter, { bufferLogs: isProduction, + autoFlushLogs: true, }, ); // Configure logger - app.useLogger(app.get(PicsurLoggerService)); - + const logger = app.get(PicsurLoggerService) + app.useLogger(logger); app.flushLogs(); + console.log(logger); + app.useGlobalFilters(app.get(MainExceptionFilter)); app.useGlobalInterceptors(app.get(SuccessInterceptor)); app.useGlobalPipes(app.get(ZodValidationPipe)); diff --git a/backend/src/managers/image/image-converter.service.ts b/backend/src/managers/image/image-converter.service.ts index f814d6a..db810e5 100644 --- a/backend/src/managers/image/image-converter.service.ts +++ b/backend/src/managers/image/image-converter.service.ts @@ -3,7 +3,7 @@ import ms from 'ms'; import { ImageRequestParams } from 'picsur-shared/dist/dto/api/image.dto'; import { FileType, - SupportedFileTypeCategory, + SupportedFileTypeCategory } from 'picsur-shared/dist/dto/mimes.dto'; import { SysPreference } from 'picsur-shared/dist/dto/sys-preferences.enum'; import { AsyncFailable, Fail, FT, HasFailed } from 'picsur-shared/dist/types'; @@ -122,16 +122,4 @@ export class ImageConverterService { filetype: targetFiletype.identifier, }; } - - private async convertAnimation( - image: Buffer, - targetFiletype: FileType, - options: ImageRequestParams, - ): AsyncFailable { - // Apng and gif are stored as is for now - return { - image: image, - filetype: targetFiletype.identifier, - }; - } } diff --git a/backend/src/managers/image/image-processor.service.ts b/backend/src/managers/image/image-processor.service.ts index ee451d0..045b69f 100644 --- a/backend/src/managers/image/image-processor.service.ts +++ b/backend/src/managers/image/image-processor.service.ts @@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common'; import { FileType, ImageFileType, - SupportedFileTypeCategory, + SupportedFileTypeCategory } from 'picsur-shared/dist/dto/mimes.dto'; import { AsyncFailable, Fail, FT, HasFailed } from 'picsur-shared/dist/types'; diff --git a/backend/src/managers/image/image.module.ts b/backend/src/managers/image/image.module.ts index ef4001c..f90cc62 100644 --- a/backend/src/managers/image/image.module.ts +++ b/backend/src/managers/image/image.module.ts @@ -19,7 +19,7 @@ import { ImageManagerService } from './image.service'; ImageProcessorService, ImageConverterService, ], - exports: [ImageManagerService], + exports: [ImageManagerService, ImageConverterService], }) export class ImageManagerModule implements OnModuleInit { private readonly logger = new Logger(ImageManagerModule.name); @@ -31,7 +31,7 @@ export class ImageManagerModule implements OnModuleInit { ) {} async onModuleInit() { - await this.imageManagerCron() + await this.imageManagerCron(); } @Interval(1000 * 60) @@ -57,7 +57,7 @@ export class ImageManagerModule implements OnModuleInit { const result = await this.imageFileDB.cleanupDerivatives(after_ms / 1000); if (HasFailed(result)) { - this.logger.warn(result.print()); + result.print(this.logger); } if (result > 0) this.logger.log(`Cleaned up ${result} derivatives`); @@ -67,7 +67,8 @@ export class ImageManagerModule implements OnModuleInit { const cleanedUp = await this.imageDB.cleanupExpired(); if (HasFailed(cleanedUp)) { - this.logger.warn(cleanedUp.print()); + cleanedUp.print(this.logger); + return; } if (cleanedUp > 0) diff --git a/frontend/src/app/routes/settings/sharex/settings-sharex.component.ts b/frontend/src/app/routes/settings/sharex/settings-sharex.component.ts index cf8992e..2e106e8 100644 --- a/frontend/src/app/routes/settings/sharex/settings-sharex.component.ts +++ b/frontend/src/app/routes/settings/sharex/settings-sharex.component.ts @@ -64,7 +64,7 @@ export class SettingsShareXComponent implements OnInit { const ext = FileType2Ext(this.selectedFormat); if (HasFailed(ext)) { - this.logger.error(ext.print()); + ext.print(this.logger); } const sharexConfig = BuildShareX( diff --git a/frontend/src/app/util/error-manager/error.service.ts b/frontend/src/app/util/error-manager/error.service.ts index b6bbcb9..93e3e26 100644 --- a/frontend/src/app/util/error-manager/error.service.ts +++ b/frontend/src/app/util/error-manager/error.service.ts @@ -15,11 +15,7 @@ export class ErrorService { ) {} public showFailure(error: Failure, logger: Logger): void { - if (error.isImportant()) { - logger.error(error.print()); - } else { - logger.warn(error.print()); - } + error.print(logger); this.snackbar.showSnackBar( error.getReason(), diff --git a/shared/src/types/failable.ts b/shared/src/types/failable.ts index 1260876..eb521a6 100644 --- a/shared/src/types/failable.ts +++ b/shared/src/types/failable.ts @@ -21,6 +21,12 @@ export enum FT { Network = 'network', } +interface ILogger { + error: (message: string) => void; + warn: (message: string) => void; + debug: (message: string) => void; +} + interface FTProp { important: boolean; code: number; @@ -142,10 +148,42 @@ export class Failure { return FTProps[this.type].important; } - print(): string { - return `${this.getName()}: ${this.getReason()}\n(${ - this.debugMessage - })\n${this.getStack()}`; + print( + logger: ILogger, + options?: { + notImportant?: boolean; + prefix?: string; + }, + ): void { + const message = this.getReason(); + const logmessage = + message + (this.getDebugMessage() ? ' - ' + this.getDebugMessage() : ''); + + const prefix = options?.prefix ? options.prefix + ' ' : ''; + const logline = `${prefix}${this.getName()}: ${logmessage}`; + + if (this.isImportant() && options?.notImportant !== true) { + logger.error(logline); + const stack = this.getStack(); + if (stack) { + logger.debug(stack); + } + } else { + logger.warn(logline); + } + } + + toString(): string { + return ( + `${this.getName()}: ${this.getReason()} - (${this.debugMessage})` + + (this.isImportant() ? '\n' + this.stack : '') + ); + } + + toError(): Error { + const error = new Error(); + (error as any).message = this; + return error; } static deserialize(data: any): Failure { @@ -251,10 +289,10 @@ export function ThrowIfFailed(failable: Failable): V { export function FallbackIfFailed( failable: Failable, fallback: V, - logger?: { warn: (...args: any) => any }, + logger?: ILogger, ): V { if (HasFailed(failable)) { - if (logger) logger.warn(failable.print()); + if (logger) failable.print(logger, { notImportant: true }); return fallback; }