Browse Source

fix: handle exception error response in similar way

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5444/head
Pranav C 1 year ago
parent
commit
b3775b5ed3
  1. 2
      packages/nocodb-nest/src/app.module.ts
  2. 29
      packages/nocodb-nest/src/filters/global-exception/global-exception.filter.ts
  3. 12
      packages/nocodb-nest/src/modules/attachments/attachments.controller.ts

2
packages/nocodb-nest/src/app.module.ts

@ -143,7 +143,7 @@ export const JwtStrategyProvider: Provider = {
}, },
// JwtStrategyProvider, // JwtStrategyProvider,
LocalStrategy, LocalStrategy,
ExtractProjectIdMiddleware, // ExtractProjectIdMiddleware,
ClientService, ClientService,
AuthTokenStrategy, AuthTokenStrategy,
BaseViewStrategy, BaseViewStrategy,

29
packages/nocodb-nest/src/filters/global-exception/global-exception.filter.ts

@ -15,8 +15,6 @@ import type { Response } from 'express';
@Catch() @Catch()
export class GlobalExceptionFilter implements ExceptionFilter { export class GlobalExceptionFilter implements ExceptionFilter {
catch(exception: any, host: ArgumentsHost) { catch(exception: any, host: ArgumentsHost) {
console.log(exception)
const ctx = host.switchToHttp(); const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>(); const response = ctx.getResponse<Response>();
@ -28,17 +26,32 @@ export class GlobalExceptionFilter implements ExceptionFilter {
return response.status(400).json(dbError); return response.status(400).json(dbError);
} }
if (exception instanceof BadRequest) { if (exception instanceof BadRequest || exception.getStatus?.() === 400) {
return response.status(400).json({ msg: exception.message }); return response.status(400).json({ msg: exception.message });
} else if (exception instanceof Unauthorized) { } else if (
exception instanceof Unauthorized ||
exception.getStatus?.() === 401
) {
return response.status(401).json({ msg: exception.message }); return response.status(401).json({ msg: exception.message });
} else if (exception instanceof Forbidden) { } else if (
exception instanceof Forbidden ||
exception.getStatus?.() === 403
) {
return response.status(403).json({ msg: exception.message }); return response.status(403).json({ msg: exception.message });
} else if (exception instanceof NotFound) { } else if (
exception instanceof NotFound ||
exception.getStatus?.() === 404
) {
return response.status(404).json({ msg: exception.message }); return response.status(404).json({ msg: exception.message });
} else if (exception instanceof InternalServerError) { } else if (
exception instanceof InternalServerError ||
exception.getStatus?.() === 500
) {
return response.status(500).json({ msg: exception.message }); return response.status(500).json({ msg: exception.message });
} else if (exception instanceof NotImplemented) { } else if (
exception instanceof NotImplemented ||
exception.getStatus?.() === 501
) {
return response.status(501).json({ msg: exception.message }); return response.status(501).json({ msg: exception.message });
} else if (exception instanceof AjvError) { } else if (exception instanceof AjvError) {
return response return response

12
packages/nocodb-nest/src/modules/attachments/attachments.controller.ts

@ -13,22 +13,18 @@ import {
UseInterceptors, UseInterceptors,
} from '@nestjs/common' } from '@nestjs/common'
import multer from 'multer'; import multer from 'multer';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express'; import { FilesInterceptor } from '@nestjs/platform-express';
import { OrgUserRoles, ProjectRoles } from 'nocodb-sdk';
import { AuthGuard } from '@nestjs/passport'; import { AuthGuard } from '@nestjs/passport';
import { NC_ATTACHMENT_FIELD_SIZE } from '../../constants'; import { NC_ATTACHMENT_FIELD_SIZE } from '../../constants';
import { NcError } from '../../helpers/catchError';
import { UploadAllowedInterceptor } from '../../interceptors/is-upload-allowed/is-upload-allowed.interceptor'; import { UploadAllowedInterceptor } from '../../interceptors/is-upload-allowed/is-upload-allowed.interceptor';
import { ExtractProjectIdMiddleware } from '../../middlewares/extract-project-id/extract-project-id.middleware'; import { ExtractProjectIdMiddleware } from '../../middlewares/extract-project-id/extract-project-id.middleware'
import Noco from '../../Noco';
import { MetaTable } from '../../utils/globals';
import { AttachmentsService } from './attachments.service'; import { AttachmentsService } from './attachments.service';
@Controller() @Controller()
export class AttachmentsController { export class AttachmentsController {
constructor(private readonly attachmentsService: AttachmentsService) {} constructor(private readonly attachmentsService: AttachmentsService) {}
@UseGuards(ExtractProjectIdMiddleware, AuthGuard('jwt')) @UseGuards(AuthGuard('jwt'))
@Post( @Post(
'/api/v1/db/storage/upload', '/api/v1/db/storage/upload',
// multer({ // multer({
@ -80,7 +76,7 @@ export class AttachmentsController {
// catchError(uploadViaURL), // catchError(uploadViaURL),
// ] // ]
// ); // );
@UseGuards(ExtractProjectIdMiddleware, AuthGuard('jwt')) @UseGuards(AuthGuard('jwt'))
async uploadViaURL(@Body() body: any, @Query('path') path: string) { async uploadViaURL(@Body() body: any, @Query('path') path: string) {
const attachments = await this.attachmentsService.uploadViaURL({ const attachments = await this.attachmentsService.uploadViaURL({
urls: body, urls: body,

Loading…
Cancel
Save