mirror of
https://github.com/immich-app/immich
synced 2025-11-14 17:36:12 +00:00
fix(server): auto-reconnect to database (#12320)
This commit is contained in:
parent
1783dfd393
commit
12b65e3c24
10 changed files with 130 additions and 46 deletions
|
|
@ -9,6 +9,7 @@ import {
|
|||
} from '@nestjs/common';
|
||||
import { Observable, catchError, throwError } from 'rxjs';
|
||||
import { ILoggerRepository } from 'src/interfaces/logger.interface';
|
||||
import { logGlobalError } from 'src/utils/logger';
|
||||
import { routeToErrorMessage } from 'src/utils/misc';
|
||||
|
||||
@Injectable()
|
||||
|
|
@ -25,9 +26,10 @@ export class ErrorInterceptor implements NestInterceptor {
|
|||
return error;
|
||||
}
|
||||
|
||||
const errorMessage = routeToErrorMessage(context.getHandler().name);
|
||||
this.logger.error(errorMessage, error, error?.errors, error?.stack);
|
||||
return new InternalServerErrorException(errorMessage);
|
||||
logGlobalError(this.logger, error);
|
||||
|
||||
const message = routeToErrorMessage(context.getHandler().name);
|
||||
return new InternalServerErrorException(message);
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
|
|
|||
47
server/src/middleware/global-exception.filter.ts
Normal file
47
server/src/middleware/global-exception.filter.ts
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import { ArgumentsHost, Catch, ExceptionFilter, HttpException, Inject } from '@nestjs/common';
|
||||
import { Response } from 'express';
|
||||
import { ClsService } from 'nestjs-cls';
|
||||
import { ILoggerRepository } from 'src/interfaces/logger.interface';
|
||||
import { logGlobalError } from 'src/utils/logger';
|
||||
|
||||
@Catch()
|
||||
export class GlobalExceptionFilter implements ExceptionFilter<Error> {
|
||||
constructor(
|
||||
@Inject(ILoggerRepository) private logger: ILoggerRepository,
|
||||
private cls: ClsService,
|
||||
) {
|
||||
this.logger.setContext(GlobalExceptionFilter.name);
|
||||
}
|
||||
|
||||
catch(error: Error, host: ArgumentsHost) {
|
||||
const ctx = host.switchToHttp();
|
||||
const response = ctx.getResponse<Response>();
|
||||
const { status, body } = this.fromError(error);
|
||||
if (!response.headersSent) {
|
||||
response.status(status).json({ ...body, statusCode: status, correlationId: this.cls.getId() });
|
||||
}
|
||||
}
|
||||
|
||||
private fromError(error: Error) {
|
||||
logGlobalError(this.logger, error);
|
||||
|
||||
if (error instanceof HttpException) {
|
||||
const status = error.getStatus();
|
||||
let body = error.getResponse();
|
||||
|
||||
// unclear what circumstances would return a string
|
||||
if (typeof body === 'string') {
|
||||
body = { message: body };
|
||||
}
|
||||
|
||||
return { status, body };
|
||||
}
|
||||
|
||||
return {
|
||||
status: 500,
|
||||
body: {
|
||||
message: 'Internal server error',
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
import { ArgumentsHost, Catch, ExceptionFilter, HttpException, Inject } from '@nestjs/common';
|
||||
import { Response } from 'express';
|
||||
import { ClsService } from 'nestjs-cls';
|
||||
import { ILoggerRepository } from 'src/interfaces/logger.interface';
|
||||
|
||||
@Catch(HttpException)
|
||||
export class HttpExceptionFilter implements ExceptionFilter {
|
||||
constructor(
|
||||
@Inject(ILoggerRepository) private logger: ILoggerRepository,
|
||||
private cls: ClsService,
|
||||
) {
|
||||
this.logger.setContext(HttpExceptionFilter.name);
|
||||
}
|
||||
|
||||
catch(exception: HttpException, host: ArgumentsHost) {
|
||||
const ctx = host.switchToHttp();
|
||||
const response = ctx.getResponse<Response>();
|
||||
const status = exception.getStatus();
|
||||
|
||||
this.logger.debug(`HttpException(${status}) ${JSON.stringify(exception.getResponse())}`);
|
||||
|
||||
let responseBody = exception.getResponse();
|
||||
// unclear what circumstances would return a string
|
||||
if (typeof responseBody === 'string') {
|
||||
responseBody = {
|
||||
error: 'Unknown',
|
||||
message: responseBody,
|
||||
statusCode: status,
|
||||
};
|
||||
}
|
||||
|
||||
if (!response.headersSent) {
|
||||
response.status(status).json({
|
||||
...responseBody,
|
||||
correlationId: this.cls.getId(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue