add 404 response when room not found.

This commit is contained in:
Miroslav Šedivý
2023-10-03 19:57:33 +02:00
parent 112e57539d
commit 177483c20c
4 changed files with 59 additions and 8 deletions
+18
View File
@@ -93,6 +93,8 @@ paths:
$ref: '#/components/schemas/RoomEntry'
'400':
description: Bad request
'404':
description: Room not found
'500':
description: Internal server error
delete:
@@ -109,6 +111,8 @@ paths:
responses:
'204':
description: OK
'404':
description: Room not found
'500':
description: Internal server error
/api/rooms/{roomName}/by-name:
@@ -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:
@@ -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:
@@ -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:
@@ -193,6 +203,8 @@ paths:
responses:
'204':
description: OK
'404':
description: Room not found
'500':
description: Internal server error
/api/rooms/{roomId}/start:
@@ -210,6 +222,8 @@ paths:
responses:
'204':
description: OK
'404':
description: Room not found
'500':
description: Internal server error
/api/rooms/{roomId}/restart:
@@ -227,6 +241,8 @@ paths:
responses:
'204':
description: OK
'404':
description: Room not found
'500':
description: Internal server error
/api/rooms/{roomId}/recreate:
@@ -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:
+31 -7
View File
@@ -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
}
@@ -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
}
@@ -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
}
@@ -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
}
@@ -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
}
@@ -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
}
+8 -1
View File
@@ -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"
)
@@ -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]
@@ -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
}
@@ -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
}
+2
View File
@@ -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)