Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add error catch into some points #70

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions backend/src/friends/friends.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { Body, Controller, Delete, Get, Post, Put } from '@nestjs/common';
import {
Body,
Controller,
Delete,
Get,
HttpException,
HttpStatus,
Post,
Put,
} from '@nestjs/common';
import { UpdateFriendDto } from './dto/updateFriend.dto';
import { FriendsService } from './friends.service';
import { CreateFriendDto } from './dto/createFriend.dto';
Expand All @@ -10,7 +19,20 @@ export class FriendsController {

@Post()
async createFriend(@Body() createFriendDto: CreateFriendDto) {
return this.friendsService.createFriend(createFriendDto);
try {
return this.friendsService.createFriend(createFriendDto);
} catch (error) {
throw new HttpException(
{
status: HttpStatus.NOT_MODIFIED,
error: 'Friend not created',
},
HttpStatus.NOT_MODIFIED,
{
cause: error,
},
);
}
}

@Get()
Expand Down
48 changes: 42 additions & 6 deletions backend/src/lobby/lobby.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Body, Controller, Get, Param } from '@nestjs/common';
import {
Body,
Controller,
Get,
HttpException,
HttpStatus,
Param,
} from '@nestjs/common';
import { LobbyService } from './lobby.service';
import { GetLeaderboardDto } from './dto/getLeaderboard.dto';

Expand All @@ -8,12 +15,41 @@ export class LobbyController {

@Get('leaderboard')
getAllLeaderboard(@Body() getLeaderboardDto: GetLeaderboardDto) {
return this.lobbyService.getLeaderboard(getLeaderboardDto, -1);
try {
return this.lobbyService.getLeaderboard(getLeaderboardDto, -1);
} catch (error) {
throw new HttpException(
{
status: HttpStatus.FORBIDDEN,
error: 'No leaderboard found',
},
HttpStatus.FORBIDDEN,
{
cause: error,
},
);
}
}

@Get('leaderboard/:limit')
getSomeLeaderboard(@Body() getLeaderboardDto: GetLeaderboardDto, @Param('limit') limit: string) {
const parsedLimit = parseInt(limit, 10);
return this.lobbyService.getLeaderboard(getLeaderboardDto, parsedLimit);
@Get('leaderboard/:limit')
getSomeLeaderboard(
@Body() getLeaderboardDto: GetLeaderboardDto,
@Param('limit') limit: string,
) {
const parsedLimit = parseInt(limit, 10);
try {
return this.lobbyService.getLeaderboard(getLeaderboardDto, parsedLimit);
} catch (error) {
throw new HttpException(
{
status: HttpStatus.FORBIDDEN,
error: 'No leaderboard found',
},
HttpStatus.FORBIDDEN,
{
cause: error,
},
);
}
}
}
92 changes: 86 additions & 6 deletions backend/src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
Param,
Patch,
Post,
HttpException,
HttpStatus,
} from '@nestjs/common';
import { UsersService } from './users.service';
import { UpdateUserDto } from './dto/updateUser.dto';
Expand All @@ -17,31 +19,109 @@ export class UsersController {

@Get('me')
findMe(@Body() dto: any) {
return this.service.findMe(dto);
try {
return this.service.findMe(dto);
} catch (error) {
throw new HttpException(
{
status: HttpStatus.FORBIDDEN,
error: 'No user found',
},
HttpStatus.FORBIDDEN,
{
cause: error,
},
);
}
}

@Get()
findAll() {
return this.service.findAll();
try {
return this.service.findAll();
} catch (error) {
throw new HttpException(
{
status: HttpStatus.FORBIDDEN,
error: 'No users found',
},
HttpStatus.FORBIDDEN,
{
cause: error,
},
);
}
}

@Get(':login')
findOne(@Param('login') login: string) {
return this.service.findOne(login);
try {
return this.service.findOne(login);
} catch (error) {
throw new HttpException(
{
status: HttpStatus.FORBIDDEN,
error: 'No user found',
},
HttpStatus.FORBIDDEN,
{
cause: error,
},
);
}
}

@Post()
create(@Body() createUserDto: CreateUserDto) {
return this.service.create(createUserDto);
try {
return this.service.create(createUserDto);
} catch (error) {
throw new HttpException(
{
status: HttpStatus.FORBIDDEN,
error: 'User already exists',
},
HttpStatus.FORBIDDEN,
{
cause: error,
},
);
}
}

@Patch(':login')
update(@Param('login') login: string, @Body() updateUserDto: UpdateUserDto) {
return this.service.update(login, updateUserDto);
try {
return this.service.update(login, updateUserDto);
} catch (error) {
throw new HttpException(
{
status: HttpStatus.NOT_MODIFIED,
error: 'User not modified',
},
HttpStatus.NOT_MODIFIED,
{
cause: error,
},
);
}
}

@Delete(':login')
remove(@Param('login') login: string) {
return this.service.remove(login);
try {
return this.service.remove(login);
} catch (error) {
throw new HttpException(
{
status: HttpStatus.NOT_MODIFIED,
error: 'User not deleted',
},
HttpStatus.NOT_MODIFIED,
{
cause: error,
},
);
}
}
}
38 changes: 38 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hook-form": "^7.44.3",
"react-hot-toast": "^2.4.1",
"react-konva": "^18.2.9",
"react-popper": "^2.3.0",
"socket.io-client": "^4.6.2",
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/app/(private)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Chat from "@/components/Chat";
import Header from "@/components/Header";
import Providers from "../login/providers";
import { ChatProvider } from "@/contexts/ChatContext";
import { Toaster } from "react-hot-toast";

export const metadata: Metadata = {
title: "Game | 42 Transcendence",
Expand All @@ -21,6 +22,7 @@ export default function RootPrivateLayout({
return (
<html lang="en">
<body className="bg-black42-100 flex">
<Toaster />
<Providers>
<div className="flex flex-col md:flex-row">
<Sidebar />
Expand Down
30 changes: 30 additions & 0 deletions frontend/src/app/error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use client"; // Error components must be Client Components

import { useEffect } from "react";

export default function Error({
error,
reset,
}: {
error: Error;
reset: () => void;
}) {
useEffect(() => {
// Log the error to an error reporting service
console.error(error);
}, [error]);

return (
<div>
<h2>Something went wrong!</h2>
<button
onClick={
// Attempt to recover by trying to re-render the segment
() => reset()
}
>
Try again
</button>
</div>
);
}
2 changes: 2 additions & 0 deletions frontend/src/app/login/auth/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use client";
import { useRouter } from "next/navigation";
import { useState } from "react";
import { toast } from "react-hot-toast";

// TODO: refactor states and input to be a client component, and use the client component here, also remove use client from top

Expand All @@ -22,6 +23,7 @@ export default function AuthPage() {

const handleSubmit = () => {
// Handle submission logic here
toast.success("Código validado com sucesso!");
console.log("Submitted code:", code);
router.push("/game");
};
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/app/login/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Metadata } from "next";
import Providers from "./providers";
import { Toaster } from "react-hot-toast";

export const metadata: Metadata = {
title: "42 Transcendence",
Expand All @@ -14,6 +15,7 @@ export default function RootLoginPublicLayout({
return (
<html lang="en">
<body>
<Toaster />
<Providers>{children}</Providers>
</body>
</html>
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Image from "next/image";
import { Toaster } from "react-hot-toast";

export default function Page() {
return (
<div className="max-w-screen-xl m-auto">
<Toaster />
<div className="flex flex-col h-screen md:flex-row">
<div className="flex flex-col md:flex-1">
<div className="flex flex-col flex-1 justify-center items-center md:items-start mx-4">
Expand Down
Loading
Loading