From 7b9617f2e73df0bebe360b23714b1acebf2c5183 Mon Sep 17 00:00:00 2001 From: Alessandro Ros Date: Thu, 18 Jan 2024 23:28:56 +0100 Subject: [PATCH] api: return 404 when an entity is not found (#2582) (#2920) --- apidocs/openapi.yaml | 120 +++++++++++++++++++++++++++++ internal/api/api.go | 123 +++++++++++++++++++++++++----- internal/conf/conf.go | 10 ++- internal/core/api_test.go | 8 +- internal/core/path_manager.go | 2 +- internal/servers/hls/server.go | 6 +- internal/servers/rtmp/server.go | 8 +- internal/servers/rtsp/server.go | 13 +++- internal/servers/srt/server.go | 8 +- internal/servers/webrtc/server.go | 12 ++- 10 files changed, 269 insertions(+), 41 deletions(-) diff --git a/apidocs/openapi.yaml b/apidocs/openapi.yaml index ce96d11b..965476e7 100644 --- a/apidocs/openapi.yaml +++ b/apidocs/openapi.yaml @@ -808,6 +808,12 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + '404': + description: path not found. + content: + application/json: + schema: + $ref: '#/components/schemas/Error' '500': description: server error. content: @@ -876,6 +882,12 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + '404': + description: path not found. + content: + application/json: + schema: + $ref: '#/components/schemas/Error' '500': description: server error. content: @@ -910,6 +922,12 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + '404': + description: path not found. + content: + application/json: + schema: + $ref: '#/components/schemas/Error' '500': description: server error. content: @@ -938,6 +956,12 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + '404': + description: path not found. + content: + application/json: + schema: + $ref: '#/components/schemas/Error' '500': description: server error. content: @@ -1008,6 +1032,12 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + '404': + description: muxer not found. + content: + application/json: + schema: + $ref: '#/components/schemas/Error' '500': description: server error. content: @@ -1078,6 +1108,12 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + '404': + description: path not found. + content: + application/json: + schema: + $ref: '#/components/schemas/Error' '500': description: server error. content: @@ -1148,6 +1184,12 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + '404': + description: connection not found. + content: + application/json: + schema: + $ref: '#/components/schemas/Error' '500': description: server error. content: @@ -1218,6 +1260,12 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + '404': + description: session not found. + content: + application/json: + schema: + $ref: '#/components/schemas/Error' '500': description: server error. content: @@ -1246,6 +1294,12 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + '404': + description: session not found. + content: + application/json: + schema: + $ref: '#/components/schemas/Error' '500': description: server error. content: @@ -1316,6 +1370,12 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + '404': + description: connection not found. + content: + application/json: + schema: + $ref: '#/components/schemas/Error' '500': description: server error. content: @@ -1386,6 +1446,12 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + '404': + description: session not found. + content: + application/json: + schema: + $ref: '#/components/schemas/Error' '500': description: server error. content: @@ -1414,6 +1480,12 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + '404': + description: session not found. + content: + application/json: + schema: + $ref: '#/components/schemas/Error' '500': description: server error. content: @@ -1484,6 +1556,12 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + '404': + description: connection not found. + content: + application/json: + schema: + $ref: '#/components/schemas/Error' '500': description: server error. content: @@ -1512,6 +1590,12 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + '404': + description: connection not found. + content: + application/json: + schema: + $ref: '#/components/schemas/Error' '500': description: server error. content: @@ -1582,6 +1666,12 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + '404': + description: connection not found. + content: + application/json: + schema: + $ref: '#/components/schemas/Error' '500': description: server error. content: @@ -1610,6 +1700,12 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + '404': + description: connection not found. + content: + application/json: + schema: + $ref: '#/components/schemas/Error' '500': description: server error. content: @@ -1680,6 +1776,12 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + '404': + description: connection not found. + content: + application/json: + schema: + $ref: '#/components/schemas/Error' '500': description: server error. content: @@ -1708,6 +1810,12 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + '404': + description: connection not found. + content: + application/json: + schema: + $ref: '#/components/schemas/Error' '500': description: server error. content: @@ -1778,6 +1886,12 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + '404': + description: session not found. + content: + application/json: + schema: + $ref: '#/components/schemas/Error' '500': description: server error. content: @@ -1806,6 +1920,12 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + '404': + description: session not found. + content: + application/json: + schema: + $ref: '#/components/schemas/Error' '500': description: server error. content: diff --git a/internal/api/api.go b/internal/api/api.go index 60c3f808..44bb6320 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -3,6 +3,7 @@ package api import ( "encoding/json" + "errors" "fmt" "net/http" "reflect" @@ -19,6 +20,11 @@ import ( "github.com/bluenviron/mediamtx/internal/logger" "github.com/bluenviron/mediamtx/internal/protocols/httpserv" "github.com/bluenviron/mediamtx/internal/restrictnetwork" + "github.com/bluenviron/mediamtx/internal/servers/hls" + "github.com/bluenviron/mediamtx/internal/servers/rtmp" + "github.com/bluenviron/mediamtx/internal/servers/rtsp" + "github.com/bluenviron/mediamtx/internal/servers/srt" + "github.com/bluenviron/mediamtx/internal/servers/webrtc" ) func interfaceIsEmpty(i interface{}) bool { @@ -264,7 +270,6 @@ func (a *API) Log(level logger.Level, format string, args ...interface{}) { a.Parent.Log(level, "[API] "+format, args...) } -// error coming from something the user inserted into the request. func (a *API) writeError(ctx *gin.Context, status int, err error) { // show error in logs a.Log(logger.Error, err.Error()) @@ -385,7 +390,7 @@ func (a *API) onConfigPathsGet(ctx *gin.Context) { p, ok := c.Paths[name] if !ok { - a.writeError(ctx, http.StatusInternalServerError, fmt.Errorf("path configuration not found")) + a.writeError(ctx, http.StatusNotFound, fmt.Errorf("path configuration not found")) return } @@ -450,7 +455,11 @@ func (a *API) onConfigPathsPatch(ctx *gin.Context) { //nolint:dupl err = newConf.PatchPath(name, &p) if err != nil { - a.writeError(ctx, http.StatusBadRequest, err) + if errors.Is(err, conf.ErrPathNotFound) { + a.writeError(ctx, http.StatusNotFound, err) + } else { + a.writeError(ctx, http.StatusBadRequest, err) + } return } @@ -487,7 +496,11 @@ func (a *API) onConfigPathsReplace(ctx *gin.Context) { //nolint:dupl err = newConf.ReplacePath(name, &p) if err != nil { - a.writeError(ctx, http.StatusBadRequest, err) + if errors.Is(err, conf.ErrPathNotFound) { + a.writeError(ctx, http.StatusNotFound, err) + } else { + a.writeError(ctx, http.StatusBadRequest, err) + } return } @@ -517,7 +530,11 @@ func (a *API) onConfigPathsDelete(ctx *gin.Context) { err := newConf.RemovePath(name) if err != nil { - a.writeError(ctx, http.StatusBadRequest, err) + if errors.Is(err, conf.ErrPathNotFound) { + a.writeError(ctx, http.StatusNotFound, err) + } else { + a.writeError(ctx, http.StatusBadRequest, err) + } return } @@ -560,7 +577,11 @@ func (a *API) onPathsGet(ctx *gin.Context) { data, err := a.PathManager.APIPathsGet(name) if err != nil { - a.writeError(ctx, http.StatusInternalServerError, err) + if errors.Is(err, conf.ErrPathNotFound) { + a.writeError(ctx, http.StatusNotFound, err) + } else { + a.writeError(ctx, http.StatusInternalServerError, err) + } return } @@ -594,7 +615,11 @@ func (a *API) onRTSPConnsGet(ctx *gin.Context) { data, err := a.RTSPServer.APIConnsGet(uuid) if err != nil { - a.writeError(ctx, http.StatusInternalServerError, err) + if errors.Is(err, rtsp.ErrConnNotFound) { + a.writeError(ctx, http.StatusNotFound, err) + } else { + a.writeError(ctx, http.StatusInternalServerError, err) + } return } @@ -628,7 +653,11 @@ func (a *API) onRTSPSessionsGet(ctx *gin.Context) { data, err := a.RTSPServer.APISessionsGet(uuid) if err != nil { - a.writeError(ctx, http.StatusInternalServerError, err) + if errors.Is(err, rtsp.ErrSessionNotFound) { + a.writeError(ctx, http.StatusNotFound, err) + } else { + a.writeError(ctx, http.StatusInternalServerError, err) + } return } @@ -644,7 +673,11 @@ func (a *API) onRTSPSessionsKick(ctx *gin.Context) { err = a.RTSPServer.APISessionsKick(uuid) if err != nil { - a.writeError(ctx, http.StatusInternalServerError, err) + if errors.Is(err, rtsp.ErrSessionNotFound) { + a.writeError(ctx, http.StatusNotFound, err) + } else { + a.writeError(ctx, http.StatusInternalServerError, err) + } return } @@ -678,7 +711,11 @@ func (a *API) onRTSPSConnsGet(ctx *gin.Context) { data, err := a.RTSPSServer.APIConnsGet(uuid) if err != nil { - a.writeError(ctx, http.StatusInternalServerError, err) + if errors.Is(err, rtsp.ErrConnNotFound) { + a.writeError(ctx, http.StatusNotFound, err) + } else { + a.writeError(ctx, http.StatusInternalServerError, err) + } return } @@ -712,7 +749,11 @@ func (a *API) onRTSPSSessionsGet(ctx *gin.Context) { data, err := a.RTSPSServer.APISessionsGet(uuid) if err != nil { - a.writeError(ctx, http.StatusInternalServerError, err) + if errors.Is(err, rtsp.ErrSessionNotFound) { + a.writeError(ctx, http.StatusNotFound, err) + } else { + a.writeError(ctx, http.StatusInternalServerError, err) + } return } @@ -728,7 +769,11 @@ func (a *API) onRTSPSSessionsKick(ctx *gin.Context) { err = a.RTSPSServer.APISessionsKick(uuid) if err != nil { - a.writeError(ctx, http.StatusInternalServerError, err) + if errors.Is(err, rtsp.ErrSessionNotFound) { + a.writeError(ctx, http.StatusNotFound, err) + } else { + a.writeError(ctx, http.StatusInternalServerError, err) + } return } @@ -762,7 +807,11 @@ func (a *API) onRTMPConnsGet(ctx *gin.Context) { data, err := a.RTMPServer.APIConnsGet(uuid) if err != nil { - a.writeError(ctx, http.StatusInternalServerError, err) + if errors.Is(err, rtmp.ErrConnNotFound) { + a.writeError(ctx, http.StatusNotFound, err) + } else { + a.writeError(ctx, http.StatusInternalServerError, err) + } return } @@ -778,7 +827,11 @@ func (a *API) onRTMPConnsKick(ctx *gin.Context) { err = a.RTMPServer.APIConnsKick(uuid) if err != nil { - a.writeError(ctx, http.StatusInternalServerError, err) + if errors.Is(err, rtmp.ErrConnNotFound) { + a.writeError(ctx, http.StatusNotFound, err) + } else { + a.writeError(ctx, http.StatusInternalServerError, err) + } return } @@ -812,7 +865,11 @@ func (a *API) onRTMPSConnsGet(ctx *gin.Context) { data, err := a.RTMPSServer.APIConnsGet(uuid) if err != nil { - a.writeError(ctx, http.StatusInternalServerError, err) + if errors.Is(err, rtmp.ErrConnNotFound) { + a.writeError(ctx, http.StatusNotFound, err) + } else { + a.writeError(ctx, http.StatusInternalServerError, err) + } return } @@ -828,7 +885,11 @@ func (a *API) onRTMPSConnsKick(ctx *gin.Context) { err = a.RTMPSServer.APIConnsKick(uuid) if err != nil { - a.writeError(ctx, http.StatusInternalServerError, err) + if errors.Is(err, rtmp.ErrConnNotFound) { + a.writeError(ctx, http.StatusNotFound, err) + } else { + a.writeError(ctx, http.StatusInternalServerError, err) + } return } @@ -862,7 +923,11 @@ func (a *API) onHLSMuxersGet(ctx *gin.Context) { data, err := a.HLSServer.APIMuxersGet(name) if err != nil { - a.writeError(ctx, http.StatusInternalServerError, err) + if errors.Is(err, hls.ErrMuxerNotFound) { + a.writeError(ctx, http.StatusNotFound, err) + } else { + a.writeError(ctx, http.StatusInternalServerError, err) + } return } @@ -896,7 +961,11 @@ func (a *API) onWebRTCSessionsGet(ctx *gin.Context) { data, err := a.WebRTCServer.APISessionsGet(uuid) if err != nil { - a.writeError(ctx, http.StatusInternalServerError, err) + if errors.Is(err, webrtc.ErrSessionNotFound) { + a.writeError(ctx, http.StatusNotFound, err) + } else { + a.writeError(ctx, http.StatusInternalServerError, err) + } return } @@ -912,7 +981,11 @@ func (a *API) onWebRTCSessionsKick(ctx *gin.Context) { err = a.WebRTCServer.APISessionsKick(uuid) if err != nil { - a.writeError(ctx, http.StatusInternalServerError, err) + if errors.Is(err, webrtc.ErrSessionNotFound) { + a.writeError(ctx, http.StatusNotFound, err) + } else { + a.writeError(ctx, http.StatusInternalServerError, err) + } return } @@ -946,7 +1019,11 @@ func (a *API) onSRTConnsGet(ctx *gin.Context) { data, err := a.SRTServer.APIConnsGet(uuid) if err != nil { - a.writeError(ctx, http.StatusInternalServerError, err) + if errors.Is(err, srt.ErrConnNotFound) { + a.writeError(ctx, http.StatusNotFound, err) + } else { + a.writeError(ctx, http.StatusInternalServerError, err) + } return } @@ -962,7 +1039,11 @@ func (a *API) onSRTConnsKick(ctx *gin.Context) { err = a.SRTServer.APIConnsKick(uuid) if err != nil { - a.writeError(ctx, http.StatusInternalServerError, err) + if errors.Is(err, srt.ErrConnNotFound) { + a.writeError(ctx, http.StatusNotFound, err) + } else { + a.writeError(ctx, http.StatusInternalServerError, err) + } return } diff --git a/internal/conf/conf.go b/internal/conf/conf.go index cff8ffa6..f8226bdd 100644 --- a/internal/conf/conf.go +++ b/internal/conf/conf.go @@ -4,6 +4,7 @@ package conf import ( "bytes" "encoding/json" + "errors" "fmt" "os" "reflect" @@ -21,6 +22,9 @@ import ( "github.com/bluenviron/mediamtx/internal/logger" ) +// ErrPathNotFound is returned when a path is not found. +var ErrPathNotFound = errors.New("path not found") + func sortedKeys(paths map[string]*OptionalPath) []string { ret := make([]string, len(paths)) i := 0 @@ -531,7 +535,7 @@ func (conf *Conf) AddPath(name string, p *OptionalPath) error { func (conf *Conf) PatchPath(name string, optional2 *OptionalPath) error { optional, ok := conf.OptionalPaths[name] if !ok { - return fmt.Errorf("path not found") + return ErrPathNotFound } copyStructFields(optional.Values, optional2.Values) @@ -542,7 +546,7 @@ func (conf *Conf) PatchPath(name string, optional2 *OptionalPath) error { func (conf *Conf) ReplacePath(name string, optional2 *OptionalPath) error { _, ok := conf.OptionalPaths[name] if !ok { - return fmt.Errorf("path not found") + return ErrPathNotFound } conf.OptionalPaths[name] = optional2 @@ -552,7 +556,7 @@ func (conf *Conf) ReplacePath(name string, optional2 *OptionalPath) error { // RemovePath removes a path. func (conf *Conf) RemovePath(name string) error { if _, ok := conf.OptionalPaths[name]; !ok { - return fmt.Errorf("path not found") + return ErrPathNotFound } delete(conf.OptionalPaths, name) diff --git a/internal/core/api_test.go b/internal/core/api_test.go index 0ab8499f..f0d4f425 100644 --- a/internal/core/api_test.go +++ b/internal/core/api_test.go @@ -374,7 +374,7 @@ func TestAPIConfigPathsDelete(t *testing.T) { require.NoError(t, err) defer res.Body.Close() - require.Equal(t, http.StatusInternalServerError, res.StatusCode) + require.Equal(t, http.StatusNotFound, res.StatusCode) checkError(t, "path configuration not found", res.Body) }() } @@ -630,7 +630,7 @@ func TestAPIPathsGet(t *testing.T) { require.NoError(t, err) defer res.Body.Close() - require.Equal(t, http.StatusInternalServerError, res.StatusCode) + require.Equal(t, http.StatusNotFound, res.StatusCode) checkError(t, "path not found", res.Body) } }) @@ -1317,7 +1317,7 @@ func TestAPIProtocolGetNotFound(t *testing.T) { require.NoError(t, err) defer res.Body.Close() - require.Equal(t, http.StatusInternalServerError, res.StatusCode) + require.Equal(t, http.StatusNotFound, res.StatusCode) switch ca { case "rtsp conns", "rtsps conns", "rtmp", "rtmps", "srt": @@ -1541,7 +1541,7 @@ func TestAPIProtocolKickNotFound(t *testing.T) { require.NoError(t, err) defer res.Body.Close() - require.Equal(t, http.StatusInternalServerError, res.StatusCode) + require.Equal(t, http.StatusNotFound, res.StatusCode) switch ca { case "rtsp conns", "rtsps conns", "rtmp", "rtmps", "srt": diff --git a/internal/core/path_manager.go b/internal/core/path_manager.go index a42e2c50..944e1090 100644 --- a/internal/core/path_manager.go +++ b/internal/core/path_manager.go @@ -388,7 +388,7 @@ func (pm *pathManager) doAPIPathsList(req pathAPIPathsListReq) { func (pm *pathManager) doAPIPathsGet(req pathAPIPathsGetReq) { path, ok := pm.paths[req.name] if !ok { - req.res <- pathAPIPathsGetRes{err: fmt.Errorf("path not found")} + req.res <- pathAPIPathsGetRes{err: conf.ErrPathNotFound} return } diff --git a/internal/servers/hls/server.go b/internal/servers/hls/server.go index d76e309e..0abb3564 100644 --- a/internal/servers/hls/server.go +++ b/internal/servers/hls/server.go @@ -3,6 +3,7 @@ package hls import ( "context" + "errors" "fmt" "sort" "sync" @@ -12,6 +13,9 @@ import ( "github.com/bluenviron/mediamtx/internal/logger" ) +// ErrMuxerNotFound is returned when a muxer is not found. +var ErrMuxerNotFound = errors.New("muxer not found") + type serverAPIMuxersListRes struct { data *defs.APIHLSMuxerList err error @@ -179,7 +183,7 @@ outer: case req := <-s.chAPIMuxerGet: muxer, ok := s.muxers[req.name] if !ok { - req.res <- serverAPIMuxersGetRes{err: fmt.Errorf("muxer not found")} + req.res <- serverAPIMuxersGetRes{err: ErrMuxerNotFound} continue } diff --git a/internal/servers/rtmp/server.go b/internal/servers/rtmp/server.go index dce8f3cc..40ec8027 100644 --- a/internal/servers/rtmp/server.go +++ b/internal/servers/rtmp/server.go @@ -4,6 +4,7 @@ package rtmp import ( "context" "crypto/tls" + "errors" "fmt" "net" "sort" @@ -18,6 +19,9 @@ import ( "github.com/bluenviron/mediamtx/internal/restrictnetwork" ) +// ErrConnNotFound is returned when a connection is not found. +var ErrConnNotFound = errors.New("connection not found") + type serverAPIConnsListRes struct { data *defs.APIRTMPConnList err error @@ -196,7 +200,7 @@ outer: case req := <-s.chAPIConnsGet: c := s.findConnByUUID(req.uuid) if c == nil { - req.res <- serverAPIConnsGetRes{err: fmt.Errorf("connection not found")} + req.res <- serverAPIConnsGetRes{err: ErrConnNotFound} continue } @@ -205,7 +209,7 @@ outer: case req := <-s.chAPIConnsKick: c := s.findConnByUUID(req.uuid) if c == nil { - req.res <- serverAPIConnsKickRes{err: fmt.Errorf("connection not found")} + req.res <- serverAPIConnsKickRes{err: ErrConnNotFound} continue } diff --git a/internal/servers/rtsp/server.go b/internal/servers/rtsp/server.go index d22a8526..1fd1e501 100644 --- a/internal/servers/rtsp/server.go +++ b/internal/servers/rtsp/server.go @@ -4,6 +4,7 @@ package rtsp import ( "context" "crypto/tls" + "errors" "fmt" "sort" "strings" @@ -22,6 +23,12 @@ import ( "github.com/bluenviron/mediamtx/internal/logger" ) +// ErrConnNotFound is returned when a connection is not found. +var ErrConnNotFound = errors.New("connection not found") + +// ErrSessionNotFound is returned when a session is not found. +var ErrSessionNotFound = errors.New("session not found") + func printAddresses(srv *gortsplib.Server) string { var ret []string @@ -356,7 +363,7 @@ func (s *Server) APIConnsGet(uuid uuid.UUID) (*defs.APIRTSPConn, error) { conn := s.findConnByUUID(uuid) if conn == nil { - return nil, fmt.Errorf("connection not found") + return nil, ErrConnNotFound } return conn.apiItem(), nil @@ -401,7 +408,7 @@ func (s *Server) APISessionsGet(uuid uuid.UUID) (*defs.APIRTSPSession, error) { _, sx := s.findSessionByUUID(uuid) if sx == nil { - return nil, fmt.Errorf("session not found") + return nil, ErrSessionNotFound } return sx.apiItem(), nil @@ -420,7 +427,7 @@ func (s *Server) APISessionsKick(uuid uuid.UUID) error { key, sx := s.findSessionByUUID(uuid) if sx == nil { - return fmt.Errorf("session not found") + return ErrSessionNotFound } sx.Close() diff --git a/internal/servers/srt/server.go b/internal/servers/srt/server.go index 046a26b9..cc42541c 100644 --- a/internal/servers/srt/server.go +++ b/internal/servers/srt/server.go @@ -3,6 +3,7 @@ package srt import ( "context" + "errors" "fmt" "sort" "sync" @@ -17,6 +18,9 @@ import ( "github.com/bluenviron/mediamtx/internal/logger" ) +// ErrConnNotFound is returned when a connection is not found. +var ErrConnNotFound = errors.New("connection not found") + func srtMaxPayloadSize(u int) int { return ((u - 16) / 188) * 188 // 16 = SRT header, 188 = MPEG-TS packet } @@ -189,7 +193,7 @@ outer: case req := <-s.chAPIConnsGet: c := s.findConnByUUID(req.uuid) if c == nil { - req.res <- serverAPIConnsGetRes{err: fmt.Errorf("connection not found")} + req.res <- serverAPIConnsGetRes{err: ErrConnNotFound} continue } @@ -198,7 +202,7 @@ outer: case req := <-s.chAPIConnsKick: c := s.findConnByUUID(req.uuid) if c == nil { - req.res <- serverAPIConnsKickRes{err: fmt.Errorf("connection not found")} + req.res <- serverAPIConnsKickRes{err: ErrConnNotFound} continue } diff --git a/internal/servers/webrtc/server.go b/internal/servers/webrtc/server.go index bcfc0bbe..5748d82b 100644 --- a/internal/servers/webrtc/server.go +++ b/internal/servers/webrtc/server.go @@ -7,6 +7,7 @@ import ( "crypto/rand" "crypto/sha1" "encoding/base64" + "errors" "fmt" "net" "net/http" @@ -33,6 +34,9 @@ const ( webrtcPayloadMaxSize = 1188 // 1200 - 12 (RTP header) ) +// ErrSessionNotFound is returned when a session is not found. +var ErrSessionNotFound = errors.New("session not found") + type nilWriter struct{} func (nilWriter) Write(p []byte) (int, error) { @@ -334,7 +338,7 @@ outer: case req := <-s.chAddSessionCandidates: sx, ok := s.sessionsBySecret[req.secret] if !ok { - req.res <- webRTCAddSessionCandidatesRes{err: fmt.Errorf("session not found")} + req.res <- webRTCAddSessionCandidatesRes{err: ErrSessionNotFound} continue } @@ -343,7 +347,7 @@ outer: case req := <-s.chDeleteSession: sx, ok := s.sessionsBySecret[req.secret] if !ok { - req.res <- webRTCDeleteSessionRes{err: fmt.Errorf("session not found")} + req.res <- webRTCDeleteSessionRes{err: ErrSessionNotFound} continue } @@ -371,7 +375,7 @@ outer: case req := <-s.chAPISessionsGet: sx := s.findSessionByUUID(req.uuid) if sx == nil { - req.res <- serverAPISessionsGetRes{err: fmt.Errorf("session not found")} + req.res <- serverAPISessionsGetRes{err: ErrSessionNotFound} continue } @@ -380,7 +384,7 @@ outer: case req := <-s.chAPIConnsKick: sx := s.findSessionByUUID(req.uuid) if sx == nil { - req.res <- serverAPISessionsKickRes{err: fmt.Errorf("session not found")} + req.res <- serverAPISessionsKickRes{err: ErrSessionNotFound} continue }