Improved error handling for negative lap times

ref #71
This commit is contained in:
Nicolai Ort 2021-01-03 17:21:53 +01:00
parent dcb791c9a2
commit 28c1b6d31d
5 changed files with 24 additions and 5 deletions

View File

@ -2,7 +2,7 @@ import { Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm'; import { getConnectionManager, Repository } from 'typeorm';
import { EntityFromBody } from 'typeorm-routing-controllers-extensions'; import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
import { TrackIdsNotMatchingError, TrackNotFoundError } from "../errors/TrackErrors"; import { TrackIdsNotMatchingError, TrackLapTimeCantBeNegativeError, TrackNotFoundError } from "../errors/TrackErrors";
import { CreateTrack } from '../models/actions/CreateTrack'; import { CreateTrack } from '../models/actions/CreateTrack';
import { Track } from '../models/entities/Track'; import { Track } from '../models/entities/Track';
import { ResponseEmpty } from '../models/responses/ResponseEmpty'; import { ResponseEmpty } from '../models/responses/ResponseEmpty';
@ -48,6 +48,7 @@ export class TrackController {
@Post() @Post()
@Authorized("TRACK:CREATE") @Authorized("TRACK:CREATE")
@ResponseSchema(ResponseTrack) @ResponseSchema(ResponseTrack)
@ResponseSchema(TrackLapTimeCantBeNegativeError, { statusCode: 406 })
@OpenAPI({ description: "Create a new track. <br> Please remember that the track\'s distance must be greater than 0." }) @OpenAPI({ description: "Create a new track. <br> Please remember that the track\'s distance must be greater than 0." })
async post( async post(
@Body({ validate: true }) @Body({ validate: true })
@ -61,6 +62,7 @@ export class TrackController {
@ResponseSchema(ResponseTrack) @ResponseSchema(ResponseTrack)
@ResponseSchema(TrackNotFoundError, { statusCode: 404 }) @ResponseSchema(TrackNotFoundError, { statusCode: 404 })
@ResponseSchema(TrackIdsNotMatchingError, { statusCode: 406 }) @ResponseSchema(TrackIdsNotMatchingError, { statusCode: 406 })
@ResponseSchema(TrackLapTimeCantBeNegativeError, { statusCode: 406 })
@OpenAPI({ description: "Update the track whose id you provided. <br> Please remember that ids can't be changed." }) @OpenAPI({ description: "Update the track whose id you provided. <br> Please remember that ids can't be changed." })
async put(@Param('id') id: number, @EntityFromBody() track: Track) { async put(@Param('id') id: number, @EntityFromBody() track: Track) {
let oldTrack = await this.trackRepository.findOne({ id: id }); let oldTrack = await this.trackRepository.findOne({ id: id });

View File

@ -23,3 +23,14 @@ export class TrackIdsNotMatchingError extends NotAcceptableError {
@IsString() @IsString()
message = "The ids don't match! \n And if you wanted to change a track's id: This isn't allowed" message = "The ids don't match! \n And if you wanted to change a track's id: This isn't allowed"
} }
/**
* Error to throw when a track's lap time is set to a negative value.
*/
export class TrackLapTimeCantBeNegativeError extends NotAcceptableError {
@IsString()
name = "TrackLapTimeCantBeNegativeError"
@IsString()
message = "The minimum lap time you provided is negative - That isn't possible. \n If you wanted to disable it: Just set it to 0/null."
}

View File

@ -1,4 +1,5 @@
import { IsInt, IsNotEmpty, IsOptional, IsPositive, IsString } from 'class-validator'; import { IsInt, IsNotEmpty, IsOptional, IsPositive, IsString } from 'class-validator';
import { TrackLapTimeCantBeNegativeError } from '../../errors/TrackErrors';
import { Track } from '../entities/Track'; import { Track } from '../entities/Track';
/** /**
@ -25,8 +26,7 @@ export class CreateTrack {
*/ */
@IsInt() @IsInt()
@IsOptional() @IsOptional()
@IsPositive() minimumLapTime: number;
minimumLapTime?: number;
/** /**
* Creates a new Track entity from this. * Creates a new Track entity from this.
@ -37,6 +37,9 @@ export class CreateTrack {
newTrack.name = this.name; newTrack.name = this.name;
newTrack.distance = this.distance; newTrack.distance = this.distance;
newTrack.minimumLapTime = this.minimumLapTime; newTrack.minimumLapTime = this.minimumLapTime;
if (this.minimumLapTime < 0) {
throw new TrackLapTimeCantBeNegativeError();
}
return newTrack; return newTrack;
} }

View File

@ -43,10 +43,9 @@ export class Track {
* The minimum time a runner should take to run a lap on this track. * The minimum time a runner should take to run a lap on this track.
* Will be used for fraud detection. * Will be used for fraud detection.
*/ */
@Column() @Column({ nullable: true })
@IsInt() @IsInt()
@IsOptional() @IsOptional()
@IsPositive()
minimumLapTime?: number; minimumLapTime?: number;
/** /**

View File

@ -1,4 +1,5 @@
import { IsInt, IsOptional, IsString } from "class-validator"; import { IsInt, IsOptional, IsString } from "class-validator";
import { TrackLapTimeCantBeNegativeError } from '../../errors/TrackErrors';
import { Track } from '../entities/Track'; import { Track } from '../entities/Track';
/** /**
@ -40,5 +41,8 @@ export class ResponseTrack {
this.name = track.name; this.name = track.name;
this.distance = track.distance; this.distance = track.distance;
this.minimumLapTime = track.minimumLapTime; this.minimumLapTime = track.minimumLapTime;
if (this.minimumLapTime < 0) {
throw new TrackLapTimeCantBeNegativeError();
}
} }
} }