Skip to content

Commit

Permalink
feat: use custom error codes in status
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Apr 29, 2024
1 parent f3c59b3 commit 92999d3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/GZCTF/ClientApp/src/components/TeamRank.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { useTranslation } from 'react-i18next'
import { useNavigate, useParams } from 'react-router-dom'
import { useIsMobile } from '@Utils/ThemeOverride'
import api from '@Api'
import { ErrorCodes } from '@Utils/Shared'

const useStyle = createStyles((theme) => ({
number: {
Expand All @@ -47,7 +48,7 @@ const TeamRank: FC<PaperProps> = (props) => {
const solved = (data?.rank?.solvedCount ?? 0) / (data?.rank?.challenges?.length ?? 1)

useEffect(() => {
if (error?.status === 204) {
if (error?.status === ErrorCodes.GameEnded) {
navigate(`/games/${numId}`)
showNotification({
color: 'yellow',
Expand Down
13 changes: 13 additions & 0 deletions src/GZCTF/ClientApp/src/utils/Shared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,16 @@ export const HunamizeSize = (size: number) => {
return `${(size / 1024 / 1024 / 1024).toFixed(2)} GiB`
}
}

/** 系统错误信息 */
export const enum ErrorCodes {
/**
* 比赛未开始
*/
GameNotStarted = 10001,

/**
* 比赛已结束
*/
GameEnded = 10002,
}
9 changes: 3 additions & 6 deletions src/GZCTF/Controllers/GameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1166,14 +1166,11 @@ public async Task<IActionResult> SubmitWriteup([FromRoute] int id, IFormFile fil

if (DateTimeOffset.UtcNow < res.Game.StartTimeUtc)
return res.WithResult(
BadRequest(new RequestResponse(localizer[nameof(Resources.Program.Game_NotStarted)])));
BadRequest(new RequestResponse(localizer[nameof(Resources.Program.Game_NotStarted), ErrorCodes.GameNotStarted])));

if (denyAfterEnded && !res.Game.PracticeMode && res.Game.EndTimeUtc < DateTimeOffset.UtcNow)
return res.WithResult(new JsonResult(new RequestResponse(localizer[nameof(Resources.Program.Game_End)]))
{
// use 204 to indicate the game has ended
StatusCode = StatusCodes.Status204NoContent
});
return res.WithResult(
BadRequest(new RequestResponse(localizer[nameof(Resources.Program.Game_End)], ErrorCodes.GameEnded)));

if (challengeId <= 0)
return res;
Expand Down
16 changes: 16 additions & 0 deletions src/GZCTF/Utils/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,19 @@ result switch
_ => "??"
};
}

/// <summary>
/// 系统错误信息,从 10000 开始
/// </summary>
public static class ErrorCodes
{
/// <summary>
/// 比赛未开始
/// </summary>
public const int GameNotStarted = 10001;

/// <summary>
/// 比赛已结束
/// </summary>
public const int GameEnded = 10002;
}

0 comments on commit 92999d3

Please sign in to comment.