Skip to content

Commit

Permalink
add 404 response when room not found.
Browse files Browse the repository at this point in the history
  • Loading branch information
m1k1o committed Oct 3, 2023
1 parent 112e575 commit 177483c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
18 changes: 18 additions & 0 deletions OpenApi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ paths:
$ref: '#/components/schemas/RoomEntry'
'400':
description: Bad request
'404':
description: Room not found
'500':
description: Internal server error
delete:
Expand All @@ -109,6 +111,8 @@ paths:
responses:
'204':
description: OK
'404':
description: Room not found
'500':
description: Internal server error
/api/rooms/{roomName}/by-name:
Expand All @@ -132,6 +136,8 @@ paths:
$ref: '#/components/schemas/RoomEntry'
'400':
description: Bad request
'404':
description: Room not found
'500':
description: Internal server error
/api/rooms/{roomId}/settings:
Expand All @@ -155,6 +161,8 @@ paths:
$ref: '#/components/schemas/RoomSettings'
'400':
description: Bad request
'404':
description: Room not found
'500':
description: Internal server error
/api/rooms/{roomId}/stats:
Expand All @@ -176,6 +184,8 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/RoomStats'
'404':
description: Room not found
'500':
description: Internal server error
/api/rooms/{roomId}/stop:
Expand All @@ -193,6 +203,8 @@ paths:
responses:
'204':
description: OK
'404':
description: Room not found
'500':
description: Internal server error
/api/rooms/{roomId}/start:
Expand All @@ -210,6 +222,8 @@ paths:
responses:
'204':
description: OK
'404':
description: Room not found
'500':
description: Internal server error
/api/rooms/{roomId}/restart:
Expand All @@ -227,6 +241,8 @@ paths:
responses:
'204':
description: OK
'404':
description: Room not found
'500':
description: Internal server error
/api/rooms/{roomId}/recreate:
Expand All @@ -253,6 +269,8 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/RoomEntry'
'404':
description: Room not found
'500':
description: Internal server error
/api/docker-compose.yaml:
Expand Down
38 changes: 31 additions & 7 deletions internal/api/rooms.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ func (manager *ApiManagerCtx) roomRecreate(w http.ResponseWriter, r *http.Reques

entry, err := manager.rooms.GetEntry(roomId)
if err != nil {
manager.logger.Error().Err(err).Msg("recreate: failed to get room entry")
http.Error(w, err.Error(), 500)
if errors.Is(err, types.ErrRoomNotFound) {
http.Error(w, err.Error(), 404)
} else {
manager.logger.Error().Err(err).Msg("recreate: failed to get room entry")
http.Error(w, err.Error(), 500)
}
return
}

Expand Down Expand Up @@ -134,7 +138,11 @@ func (manager *ApiManagerCtx) roomGetEntry(w http.ResponseWriter, r *http.Reques

response, err := manager.rooms.GetEntry(roomId)
if err != nil {
http.Error(w, err.Error(), 500)
if errors.Is(err, types.ErrRoomNotFound) {
http.Error(w, err.Error(), 404)
} else {
http.Error(w, err.Error(), 500)
}
return
}

Expand All @@ -148,7 +156,11 @@ func (manager *ApiManagerCtx) roomGetEntryByName(w http.ResponseWriter, r *http.

response, err := manager.rooms.GetEntryByName(roomName)
if err != nil {
http.Error(w, err.Error(), 500)
if errors.Is(err, types.ErrRoomNotFound) {
http.Error(w, err.Error(), 404)
} else {
http.Error(w, err.Error(), 500)
}
return
}

Expand All @@ -161,7 +173,11 @@ func (manager *ApiManagerCtx) roomGetSettings(w http.ResponseWriter, r *http.Req

response, err := manager.rooms.GetSettings(roomId)
if err != nil {
http.Error(w, err.Error(), 500)
if errors.Is(err, types.ErrRoomNotFound) {
http.Error(w, err.Error(), 404)
} else {
http.Error(w, err.Error(), 500)
}
return
}

Expand All @@ -174,7 +190,11 @@ func (manager *ApiManagerCtx) roomGetStats(w http.ResponseWriter, r *http.Reques

response, err := manager.rooms.GetStats(roomId)
if err != nil {
http.Error(w, err.Error(), 500)
if errors.Is(err, types.ErrRoomNotFound) {
http.Error(w, err.Error(), 404)
} else {
http.Error(w, err.Error(), 500)
}
return
}

Expand All @@ -188,7 +208,11 @@ func (manager *ApiManagerCtx) roomGenericAction(action func(id string) error) fu

err := action(roomId)
if err != nil {
http.Error(w, err.Error(), 500)
if errors.Is(err, types.ErrRoomNotFound) {
http.Error(w, err.Error(), 404)
} else {
http.Error(w, err.Error(), 500)
}
return
}

Expand Down
9 changes: 8 additions & 1 deletion internal/room/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

dockerTypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
dockerClient "github.com/docker/docker/client"

"github.com/m1k1o/neko-rooms/internal/types"
)
Expand Down Expand Up @@ -66,7 +67,7 @@ func (manager *RoomManagerCtx) containerFilter(args filters.Args) (*dockerTypes.
}

if len(containers) == 0 {
return nil, fmt.Errorf("container not found")
return nil, types.ErrRoomNotFound
}

container := containers[0]
Expand All @@ -88,6 +89,9 @@ func (manager *RoomManagerCtx) containerByName(name string) (*dockerTypes.Contai
func (manager *RoomManagerCtx) inspectContainer(id string) (*dockerTypes.ContainerJSON, error) {
container, err := manager.client.ContainerInspect(context.Background(), id)
if err != nil {
if dockerClient.IsErrNotFound(err) {
return nil, types.ErrRoomNotFound
}
return nil, err
}

Expand All @@ -109,6 +113,9 @@ func (manager *RoomManagerCtx) containerExec(id string, cmd []string) (string, e
Detach: false,
})
if err != nil {
if dockerClient.IsErrNotFound(err) {
return "", types.ErrRoomNotFound
}
return "", err
}

Expand Down
2 changes: 2 additions & 0 deletions internal/types/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ type RoomMember struct {
Muted bool `json:"muted"`
}

var ErrRoomNotFound = fmt.Errorf("room not found")

type RoomManager interface {
Config() RoomsConfig
List(labels map[string]string) ([]RoomEntry, error)
Expand Down

0 comments on commit 177483c

Please sign in to comment.