Skip to content

Commit

Permalink
fix: error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
c27gc committed Jul 6, 2023
1 parent 89a2011 commit 4b6c2ad
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
27 changes: 23 additions & 4 deletions src/modules/private-share-folder/private-sharing.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {
BadRequestException,
Body,
Controller,
Get,
HttpStatus,
Logger,
Post,
Query,
Res,
} from '@nestjs/common';
import {
ApiBearerAuth,
Expand All @@ -14,12 +15,16 @@ import {
ApiQuery,
ApiTags,
} from '@nestjs/swagger';
import { PrivateSharingUseCase } from './private-sharing.usecase';
import {
InvalidOwnerError,
PrivateSharingUseCase,
} from './private-sharing.usecase';
import { User as UserDecorator } from '../auth/decorators/user.decorator';
import { Folder } from '../folder/folder.domain';
import { User } from '../user/user.domain';
import { OrderBy } from 'src/common/order.type';
import { Pagination } from 'src/lib/pagination';
import { Response } from 'express';
import { GrantPrivilegesDto } from './dto/grant-privileges.dto';

@ApiTags('Private Sharing')
Expand All @@ -36,7 +41,8 @@ export class PrivateSharingController {
async grantPrivileges(
@UserDecorator() user: User,
@Body() dto: GrantPrivilegesDto,
): Promise<Record<'message', string>> {
@Res({ passthrough: true }) res: Response,
) {
try {
await this.privateSharingUseCase.grantPrivileges(
user,
Expand All @@ -49,7 +55,20 @@ export class PrivateSharingController {
message: 'Privileges granted',
};
} catch (error) {
throw new BadRequestException(error.message);
let errorMessage = error.message;

if (error instanceof InvalidOwnerError) {
res.status(HttpStatus.BAD_REQUEST);
} else {
new Logger().error(
`[PRIVATESHARING/GRANTACCESS] ERROR: ${
(error as Error).message
}, BODY ${JSON.stringify(dto)}, STACK: ${(error as Error).stack}`,
);
res.status(HttpStatus.INTERNAL_SERVER_ERROR);
errorMessage = 'Internal Server Error';
}
return { error: errorMessage };
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/modules/private-share-folder/private-sharing.usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { SequelizePrivateSharingRepository } from './private-sharing.repository'
import { SequelizeUserRepository } from '../user/user.repository';
import { SequelizeFolderRepository } from '../folder/folder.repository';

export class InvalidOwnerError extends Error {
constructor() {
super('You are not the owner of this folder');
}
}
@Injectable()
export class PrivateSharingUseCase {
constructor(
Expand All @@ -26,7 +31,7 @@ export class PrivateSharingUseCase {
);

if (owner.id !== folder.userId) {
throw new Error('You are not the owner of this folder');
throw new InvalidOwnerError();
}

await this.privateSharingRespository.createPrivateFolderRole(
Expand Down

0 comments on commit 4b6c2ad

Please sign in to comment.