Skip to content

Commit

Permalink
✨ handlerでzipファイル関係のエラーハンドリング
Browse files Browse the repository at this point in the history
  • Loading branch information
ikura-hamu committed Oct 27, 2024
1 parent 83d57d4 commit 5a75e28
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 178 deletions.
3 changes: 2 additions & 1 deletion docs/openapi/v2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ servers:
- url: /api/v2 #oapi-codegenでproxy後にもmatchできるようにするため必要
info:
description: 'traP Collection v2'
version: '2.3.0'
version: '2.4.0'
title: 'traP Collection v2'
contact:
name: traP
Expand Down Expand Up @@ -857,6 +857,7 @@ paths:
$ref: '#/components/schemas/Error'
description: |
リクエストが不正である場合に返されます。
エントリーポイントが存在しない、zipファイルでないなどです。
"401":
$ref: '#/components/responses/TraPUnauthorized'
"404":
Expand Down
8 changes: 7 additions & 1 deletion src/handler/v2/game_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@ func (gameFile GameFile) PostGameFile(c echo.Context, gameID openapi.GameIDInPat
var err error
savedFile, err = gameFile.gameFileService.SaveGameFile(c.Request().Context(), r, values.NewGameIDFromUUID(gameID), fileType, entryPoint)
if errors.Is(err, service.ErrInvalidGameID) {
return echo.NewHTTPError(http.StatusNotFound, "invalid gameID")
return echo.NewHTTPError(http.StatusNotFound, "gameID not found")
}
if errors.Is(err, service.ErrNotZipFile) {
return echo.NewHTTPError(http.StatusBadRequest, "only zip file is allowed")
}
if errors.Is(err, service.ErrInvalidEntryPoint) {
return echo.NewHTTPError(http.StatusBadRequest, "invalid entry point")
}
if err != nil {
log.Printf("error: failed to save game file: %v\n", err)
Expand Down
20 changes: 20 additions & 0 deletions src/handler/v2/game_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,26 @@ func TestPostGameFile(t *testing.T) {
isErr: true,
statusCode: http.StatusNotFound,
},
{
description: "SaveGameFileがErrNotZipFileなので404",
fileType: openapi.Jar,
gameID: uuid.UUID(values.NewGameID()),
reader: bytes.NewReader([]byte("test")),
executeSaveGameFile: true,
saveGameFileErr: service.ErrNotZipFile,
isErr: true,
statusCode: http.StatusBadRequest,
},
{
description: "SaveGameFileがErrInvalidEntryPointなので404",
fileType: openapi.Jar,
gameID: uuid.UUID(values.NewGameID()),
reader: bytes.NewReader([]byte("test")),
executeSaveGameFile: true,
saveGameFileErr: service.ErrInvalidEntryPoint,
isErr: true,
statusCode: http.StatusBadRequest,
},
{
description: "SaveGameFileがエラーなので500",
fileType: openapi.Jar,
Expand Down
Loading

0 comments on commit 5a75e28

Please sign in to comment.