Remplacement des codes d'erreurs NotAcceptable par des codes plus adaptés

This commit is contained in:
Emmy D'Anello 2024-12-08 17:02:22 +01:00
parent 3af1e498ac
commit 83d3a573ca
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
6 changed files with 13 additions and 16 deletions

View File

@ -1,7 +1,7 @@
import { Controller, Get, Post, Body, Patch, Param, Delete, ParseIntPipe, HttpCode, UseGuards, Req, Query, NotFoundException } from '@nestjs/common' import { Controller, Get, Post, Body, Patch, Param, Delete, ParseIntPipe, HttpCode, UseGuards, Req, Query, NotFoundException } from '@nestjs/common'
import { ChallengeActionsService } from './challenge-actions.service' import { ChallengeActionsService } from './challenge-actions.service'
import { AuthenticatedRequest, JwtAuthGuard } from 'src/auth/jwt-auth.guard' import { AuthenticatedRequest, JwtAuthGuard } from 'src/auth/jwt-auth.guard'
import { ApiBearerAuth, ApiCreatedResponse, ApiNotFoundResponse, ApiOkResponse, ApiUnauthorizedResponse, ApiUnprocessableEntityResponse } from '@nestjs/swagger' import { ApiBearerAuth, ApiConflictResponse, ApiCreatedResponse, ApiNotFoundResponse, ApiOkResponse, ApiUnauthorizedResponse } from '@nestjs/swagger'
import { ChallengeActionEntity } from './entities/challenge-action.entity' import { ChallengeActionEntity } from './entities/challenge-action.entity'
import { CreateChallengeActionDto } from './dto/create-challenge-action.dto' import { CreateChallengeActionDto } from './dto/create-challenge-action.dto'
import { ApiOkResponsePaginated, paginateOutput } from 'src/common/utils/pagination.utils' import { ApiOkResponsePaginated, paginateOutput } from 'src/common/utils/pagination.utils'
@ -76,6 +76,7 @@ export class ChallengeActionsController {
@ApiOkResponse({ type: ChallengeActionEntity }) @ApiOkResponse({ type: ChallengeActionEntity })
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" }) @ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
@ApiNotFoundResponse({ description: "Objet non trouvé" }) @ApiNotFoundResponse({ description: "Objet non trouvé" })
@ApiConflictResponse({ description: "Aucun défi à terminer n'est en cours" })
async endCurrent(@Req() request: AuthenticatedRequest, @Body() { success }: EndChallengeActionDto): Promise<ChallengeActionEntity> { async endCurrent(@Req() request: AuthenticatedRequest, @Body() { success }: EndChallengeActionDto): Promise<ChallengeActionEntity> {
const challengeAction = await this.challengeActionsService.endCurrentChallenge(request.user, success) const challengeAction = await this.challengeActionsService.endCurrentChallenge(request.user, success)
return new ChallengeActionEntity(challengeAction) return new ChallengeActionEntity(challengeAction)

View File

@ -1,4 +1,4 @@
import { Injectable, NotAcceptableException } from '@nestjs/common' import { BadRequestException, Injectable, UnprocessableEntityException } from '@nestjs/common'
import { CreateChallengeActionDto } from './dto/create-challenge-action.dto' import { CreateChallengeActionDto } from './dto/create-challenge-action.dto'
import { UpdateChallengeActionDto } from './dto/update-challenge-action.dto' import { UpdateChallengeActionDto } from './dto/update-challenge-action.dto'
import { ChallengeAction, Player } from '@prisma/client' import { ChallengeAction, Player } from '@prisma/client'
@ -55,7 +55,7 @@ export class ChallengeActionsService {
} }
}) })
if (!challengeAction) if (!challengeAction)
throw new NotAcceptableException("Aucun défi n'est en cours") throw new BadRequestException("Aucun défi n'est en cours")
let data let data
const now = new Date() const now = new Date()
if (success) { if (success) {

View File

@ -3,7 +3,7 @@ import { ChallengesService } from './challenges.service'
import { CreateChallengeDto } from './dto/create-challenge.dto' import { CreateChallengeDto } from './dto/create-challenge.dto'
import { UpdateChallengeDto } from './dto/update-challenge.dto' import { UpdateChallengeDto } from './dto/update-challenge.dto'
import { AuthenticatedRequest, JwtAuthGuard } from 'src/auth/jwt-auth.guard' import { AuthenticatedRequest, JwtAuthGuard } from 'src/auth/jwt-auth.guard'
import { ApiBearerAuth, ApiCreatedResponse, ApiForbiddenResponse, ApiNotFoundResponse, ApiOkResponse, ApiUnauthorizedResponse } from '@nestjs/swagger' import { ApiBearerAuth, ApiConflictResponse, ApiCreatedResponse, ApiNotFoundResponse, ApiOkResponse, ApiUnauthorizedResponse } from '@nestjs/swagger'
import { ChallengeEntity } from './entities/challenge.entity' import { ChallengeEntity } from './entities/challenge.entity'
import { ApiOkResponsePaginated, paginateOutput } from 'src/common/utils/pagination.utils' import { ApiOkResponsePaginated, paginateOutput } from 'src/common/utils/pagination.utils'
import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto' import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto'
@ -19,7 +19,6 @@ export class ChallengesController {
@ApiBearerAuth() @ApiBearerAuth()
@ApiCreatedResponse({ type: ChallengeEntity, description: "Objet créé avec succès" }) @ApiCreatedResponse({ type: ChallengeEntity, description: "Objet créé avec succès" })
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" }) @ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
@ApiForbiddenResponse({ description: "Permission refusée" })
async create(@Body() createChallengeDto: CreateChallengeDto): Promise<ChallengeEntity> { async create(@Body() createChallengeDto: CreateChallengeDto): Promise<ChallengeEntity> {
const challenge = await this.challengesService.create(createChallengeDto) const challenge = await this.challengesService.create(createChallengeDto)
return new ChallengeEntity(challenge) return new ChallengeEntity(challenge)
@ -30,7 +29,6 @@ export class ChallengesController {
@ApiBearerAuth() @ApiBearerAuth()
@ApiOkResponsePaginated(ChallengeEntity) @ApiOkResponsePaginated(ChallengeEntity)
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" }) @ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
@ApiForbiddenResponse({ description: "Permission refusée" })
async findAll(@Query() queryPagination?: QueryPaginationDto): Promise<PaginateOutputDto<ChallengeEntity>> { async findAll(@Query() queryPagination?: QueryPaginationDto): Promise<PaginateOutputDto<ChallengeEntity>> {
const [challenges, total] = await this.challengesService.findAll(queryPagination) const [challenges, total] = await this.challengesService.findAll(queryPagination)
return paginateOutput<ChallengeEntity>(challenges.map(challenge => new ChallengeEntity(challenge)), total, queryPagination) return paginateOutput<ChallengeEntity>(challenges.map(challenge => new ChallengeEntity(challenge)), total, queryPagination)
@ -41,7 +39,6 @@ export class ChallengesController {
@ApiBearerAuth() @ApiBearerAuth()
@ApiOkResponse({ type: ChallengeEntity }) @ApiOkResponse({ type: ChallengeEntity })
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" }) @ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
@ApiForbiddenResponse({ description: "Permission refusée" })
@ApiNotFoundResponse({ description: "Objet non trouvé" }) @ApiNotFoundResponse({ description: "Objet non trouvé" })
async findOne(@Param('id', ParseIntPipe) id: number): Promise<ChallengeEntity> { async findOne(@Param('id', ParseIntPipe) id: number): Promise<ChallengeEntity> {
const challenge = await this.challengesService.findOne(id) const challenge = await this.challengesService.findOne(id)
@ -55,7 +52,6 @@ export class ChallengesController {
@ApiBearerAuth() @ApiBearerAuth()
@ApiOkResponse({ type: ChallengeEntity }) @ApiOkResponse({ type: ChallengeEntity })
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" }) @ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
@ApiForbiddenResponse({ description: "Permission refusée" })
@ApiNotFoundResponse({ description: "Objet non trouvé" }) @ApiNotFoundResponse({ description: "Objet non trouvé" })
async update(@Param('id', ParseIntPipe) id: number, @Body() updateChallengeDto: UpdateChallengeDto) { async update(@Param('id', ParseIntPipe) id: number, @Body() updateChallengeDto: UpdateChallengeDto) {
return await this.challengesService.update(id, updateChallengeDto) return await this.challengesService.update(id, updateChallengeDto)
@ -67,7 +63,6 @@ export class ChallengesController {
@ApiBearerAuth() @ApiBearerAuth()
@ApiOkResponse({ type: ChallengeEntity }) @ApiOkResponse({ type: ChallengeEntity })
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" }) @ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
@ApiForbiddenResponse({ description: "Permission refusée" })
@ApiNotFoundResponse({ description: "Objet non trouvé" }) @ApiNotFoundResponse({ description: "Objet non trouvé" })
async remove(@Param('id', ParseIntPipe) id: number) { async remove(@Param('id', ParseIntPipe) id: number) {
await this.challengesService.remove(id) await this.challengesService.remove(id)
@ -78,8 +73,8 @@ export class ChallengesController {
@ApiBearerAuth() @ApiBearerAuth()
@ApiOkResponse({ type: ChallengeEntity }) @ApiOkResponse({ type: ChallengeEntity })
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" }) @ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
@ApiForbiddenResponse({ description: "Permission refusée" })
@ApiNotFoundResponse({ description: "Objet non trouvé" }) @ApiNotFoundResponse({ description: "Objet non trouvé" })
@ApiConflictResponse({ description: "Un défi est déjà en cours d'accomplissement" })
async drawRandom(@Req() request: AuthenticatedRequest): Promise<ChallengeEntity> { async drawRandom(@Req() request: AuthenticatedRequest): Promise<ChallengeEntity> {
const challenge = await this.challengesService.drawRandom(request.user) const challenge = await this.challengesService.drawRandom(request.user)
return new ChallengeEntity(challenge) return new ChallengeEntity(challenge)

View File

@ -1,4 +1,4 @@
import { Injectable, NotAcceptableException, NotFoundException } from '@nestjs/common' import { ConflictException, Injectable, NotFoundException } from '@nestjs/common'
import { CreateChallengeDto } from './dto/create-challenge.dto' import { CreateChallengeDto } from './dto/create-challenge.dto'
import { UpdateChallengeDto } from './dto/update-challenge.dto' import { UpdateChallengeDto } from './dto/update-challenge.dto'
import { Challenge, Player } from '@prisma/client' import { Challenge, Player } from '@prisma/client'
@ -64,7 +64,7 @@ export class ChallengesService {
} }
}) })
if (currentChallengeAction) if (currentChallengeAction)
throw new NotAcceptableException("Un défi est déjà en cours d'accomplissement") throw new ConflictException("Un défi est déjà en cours d'accomplissement")
const remaningChallenges = await this.prisma.challenge.count({ const remaningChallenges = await this.prisma.challenge.count({
where: { where: {
action: null, action: null,

View File

@ -4,7 +4,7 @@ import { CreateTrainDto } from './dto/create-train.dto'
import { UpdateTrainDto } from './dto/update-train.dto' import { UpdateTrainDto } from './dto/update-train.dto'
import { TrainEntity } from './entities/train.entity' import { TrainEntity } from './entities/train.entity'
import { AuthenticatedRequest, JwtAuthGuard } from 'src/auth/jwt-auth.guard' import { AuthenticatedRequest, JwtAuthGuard } from 'src/auth/jwt-auth.guard'
import { ApiBearerAuth, ApiCreatedResponse, ApiNotFoundResponse, ApiOkResponse, ApiUnauthorizedResponse } from '@nestjs/swagger' import { ApiBearerAuth, ApiCreatedResponse, ApiNotFoundResponse, ApiOkResponse, ApiUnauthorizedResponse, ApiUnprocessableEntityResponse } from '@nestjs/swagger'
import { ApiOkResponsePaginated, paginateOutput } from 'src/common/utils/pagination.utils' import { ApiOkResponsePaginated, paginateOutput } from 'src/common/utils/pagination.utils'
import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto' import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto'
import { PaginateOutputDto } from 'src/common/dto/pagination-output.dto' import { PaginateOutputDto } from 'src/common/dto/pagination-output.dto'
@ -76,6 +76,7 @@ export class TrainsController {
@ApiBearerAuth() @ApiBearerAuth()
@ApiCreatedResponse({ type: TrainEntity, description: "Train importé avec succès" }) @ApiCreatedResponse({ type: TrainEntity, description: "Train importé avec succès" })
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" }) @ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
@ApiUnprocessableEntityResponse({ description: "Le voyage Interrail à importer contient plusieurs trajets ou plusieurs trains" })
async import(@Req() request: AuthenticatedRequest, @Body() importTrainDto: ImportTrainDto): Promise<TrainEntity> { async import(@Req() request: AuthenticatedRequest, @Body() importTrainDto: ImportTrainDto): Promise<TrainEntity> {
const train = await this.trainsService.import(request.user, importTrainDto) const train = await this.trainsService.import(request.user, importTrainDto)
return new TrainEntity(train) return new TrainEntity(train)

View File

@ -1,4 +1,4 @@
import { Injectable, NotAcceptableException } from '@nestjs/common' import { Injectable, UnprocessableEntityException } from '@nestjs/common'
import { CreateTrainDto } from './dto/create-train.dto' import { CreateTrainDto } from './dto/create-train.dto'
import { UpdateTrainDto } from './dto/update-train.dto' import { UpdateTrainDto } from './dto/update-train.dto'
import { PrismaService } from 'src/prisma/prisma.service' import { PrismaService } from 'src/prisma/prisma.service'
@ -53,10 +53,10 @@ export class TrainsService {
const interrailResult: InterrailJourney = await fetch(`https://3uiwjsimnh.execute-api.eu-central-1.amazonaws.com/Prod/journey-import?id=${trainId}`) const interrailResult: InterrailJourney = await fetch(`https://3uiwjsimnh.execute-api.eu-central-1.amazonaws.com/Prod/journey-import?id=${trainId}`)
.then(data => data.json()) .then(data => data.json())
if (interrailResult.data.travels.length !== 1) if (interrailResult.data.travels.length !== 1)
throw new NotAcceptableException(`Ce voyage contient ${interrailResult.data.travels.length} trajets. Merci d'ajouter les trajets un à un.`) throw new UnprocessableEntityException(`Ce voyage contient ${interrailResult.data.travels.length} trajets. Merci d'ajouter les trajets un à un.`)
const travel = interrailResult.data.travels[0] const travel = interrailResult.data.travels[0]
if (travel.legs.length !== 1) if (travel.legs.length !== 1)
throw new NotAcceptableException(`Ce trajet contient ${travel.legs.length} trains. Merci d'ajouter les trajets un à un.`) throw new UnprocessableEntityException(`Ce trajet contient ${travel.legs.length} trains. Merci d'ajouter les trajets un à un.`)
const leg = travel.legs[0] const leg = travel.legs[0]
const travelInfoJson: InterrailTravelInfo = JSON.parse(travel.infoJson) const travelInfoJson: InterrailTravelInfo = JSON.parse(travel.infoJson)