Skip to content

Commit

Permalink
[core] Send 404 if file does not exist
Browse files Browse the repository at this point in the history
fix #134
  • Loading branch information
allburov committed Nov 25, 2023
1 parent aff3b01 commit eb08739
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
28 changes: 27 additions & 1 deletion src/api/exception.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,42 @@ export function serializeError(err: unknown) {

@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
catch(exception: HttpException | Error, host: ArgumentsHost): void {
catch(exception: any | Error, host: ArgumentsHost): void {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>();

/**
* If file not found - we get weird 500 error
* So we convert that exception manually to 404 and send appropriate JSON response
* @issue https://github.com/devlikeapro/whatsapp-http-api/issues/134
* @solution https://github.com/nestjs/serve-static/issues/139#issuecomment-612429557
*/
if (exception.code === 'ENOENT') {
response.status(HttpStatus.NOT_FOUND).json({
error: {
code: 404,
key: 'FILE_NOT_FOUND',
message: 'File not found',
details: 'File not found or no longer available',
},
});
response.send();
return;
}

/**
* If it's HttpException - pass it as is
*/
if (exception instanceof HttpException) {
response.status(exception.getStatus()).json(exception.getResponse());
return;
}

/**
* If it's not HttpException - pass it as 500 error
* And send JSON response with error details
*/
const httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
response.status(httpStatus).json({
statusCode: httpStatus,
Expand Down
1 change: 0 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { AppModuleCore } from './core/app.module.core';
import { SwaggerModuleCore } from './core/swagger.module.core';
import { getWAHAVersion, VERSION, WAHAVersion } from './version';
import { WAHA_WEBHOOKS } from './structures/webhooks.dto';
import { LogLevel } from '@nestjs/common';
import { getLogLevels } from './helpers';

async function loadModules(): Promise<
Expand Down

0 comments on commit eb08739

Please sign in to comment.