replace errors.As with modern errors.AsType (#5834)
This commit is contained in:
@@ -43,8 +43,7 @@ func (c *Cmd) runOSSpecific(cmdstr string, env []string) error {
|
||||
if err2 == nil {
|
||||
return 0
|
||||
}
|
||||
var ee *exec.ExitError
|
||||
if errors.As(err2, &ee) {
|
||||
if ee, ok := errors.AsType[*exec.ExitError](err2); ok {
|
||||
ee.ExitCode()
|
||||
}
|
||||
return 0
|
||||
|
||||
@@ -181,8 +181,7 @@ func (s *Server) onGet(ctx *gin.Context) {
|
||||
err = seekAndMux(pathConf.RecordFormat, segments, start, duration, m)
|
||||
if err != nil {
|
||||
// user aborted the download
|
||||
var neterr *net.OpError
|
||||
if errors.As(err, &neterr) {
|
||||
if _, ok := errors.AsType[*net.OpError](err); ok {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -215,8 +215,7 @@ func (s *httpServer) onRequest(ctx *gin.Context) {
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
var terr *auth.Error
|
||||
if errors.As(err, &terr) {
|
||||
if terr, ok := errors.AsType[*auth.Error](err); ok {
|
||||
if terr.AskCredentials {
|
||||
ctx.Header("WWW-Authenticate", `Basic realm="mediamtx"`)
|
||||
s.writeErrorNoLog(ctx, http.StatusUnauthorized, fmt.Errorf("authentication error"))
|
||||
@@ -271,8 +270,7 @@ func (s *httpServer) onRequest(ctx *gin.Context) {
|
||||
}
|
||||
err := sx.initialize(ctx)
|
||||
if err != nil {
|
||||
var terr2 *defs.PathNoStreamAvailableError
|
||||
if errors.As(err, &terr2) {
|
||||
if _, ok := errors.AsType[*defs.PathNoStreamAvailableError](err); ok {
|
||||
s.writeErrorNoLog(ctx, http.StatusNotFound, err)
|
||||
return
|
||||
}
|
||||
@@ -336,8 +334,7 @@ func (s *httpServer) onRequest(ctx *gin.Context) {
|
||||
}
|
||||
err := sx.initialize(ctx)
|
||||
if err != nil {
|
||||
var terr *auth.Error
|
||||
if errors.As(err, &terr) {
|
||||
if terr, ok := errors.AsType[*auth.Error](err); ok {
|
||||
if terr.AskCredentials {
|
||||
ctx.Header("WWW-Authenticate", `Basic realm="mediamtx"`)
|
||||
s.writeErrorNoLog(ctx, http.StatusUnauthorized, fmt.Errorf("authentication error"))
|
||||
@@ -353,8 +350,7 @@ func (s *httpServer) onRequest(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var terr2 *defs.PathNoStreamAvailableError
|
||||
if errors.As(err, &terr2) {
|
||||
if _, ok := errors.AsType[*defs.PathNoStreamAvailableError](err); ok {
|
||||
s.writeErrorNoLog(ctx, http.StatusNotFound, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -182,8 +182,7 @@ func (s *httpServer) checkAuthOutsideSession(ctx *gin.Context, pathName string,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
var terr *auth.Error
|
||||
if errors.As(err, &terr) {
|
||||
if terr, ok := errors.AsType[*auth.Error](err); ok {
|
||||
if terr.AskCredentials {
|
||||
ctx.Header("WWW-Authenticate", `Basic realm="mediamtx"`)
|
||||
s.writeErrorNoLog(ctx, http.StatusUnauthorized, fmt.Errorf("authentication error"))
|
||||
|
||||
@@ -171,8 +171,7 @@ func (c *conn) runRead() error {
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
var terr *auth.Error
|
||||
if errors.As(err, &terr) {
|
||||
if terr, ok := errors.AsType[*auth.Error](err); ok {
|
||||
// wait some seconds to delay brute force attacks
|
||||
<-time.After(auth.PauseAfterError)
|
||||
return terr
|
||||
@@ -266,8 +265,7 @@ func (c *conn) runPublish() error {
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
var terr *auth.Error
|
||||
if errors.As(err, &terr) {
|
||||
if terr, ok := errors.AsType[*auth.Error](err); ok {
|
||||
// wait some seconds to delay brute force attacks
|
||||
<-time.After(auth.PauseAfterError)
|
||||
return terr
|
||||
|
||||
@@ -164,14 +164,12 @@ func (c *conn) onDescribe(ctx *gortsplib.ServerHandlerOnDescribeCtx,
|
||||
})
|
||||
|
||||
if res.Err != nil {
|
||||
var terr *auth.Error
|
||||
if errors.As(res.Err, &terr) {
|
||||
if terr, ok := errors.AsType[*auth.Error](res.Err); ok {
|
||||
res, err2 := c.handleAuthError(terr)
|
||||
return res, nil, err2
|
||||
}
|
||||
|
||||
var terr2 *defs.PathNoStreamAvailableError
|
||||
if errors.As(res.Err, &terr2) {
|
||||
if _, ok := errors.AsType[*defs.PathNoStreamAvailableError](res.Err); ok {
|
||||
return &base.Response{
|
||||
StatusCode: base.StatusNotFound,
|
||||
}, nil, res.Err
|
||||
|
||||
@@ -214,8 +214,7 @@ func (s *session) onAnnounce(c *conn, ctx *gortsplib.ServerHandlerOnAnnounceCtx)
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
var terr *auth.Error
|
||||
if errors.As(err, &terr) {
|
||||
if terr, ok := errors.AsType[*auth.Error](err); ok {
|
||||
return c.handleAuthError(terr)
|
||||
}
|
||||
|
||||
@@ -295,14 +294,12 @@ func (s *session) onSetup(c *conn, ctx *gortsplib.ServerHandlerOnSetupCtx,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
var terr *auth.Error
|
||||
if errors.As(err, &terr) {
|
||||
if terr, ok := errors.AsType[*auth.Error](err); ok {
|
||||
res, err2 := c.handleAuthError(terr)
|
||||
return res, nil, err2
|
||||
}
|
||||
|
||||
var terr2 *defs.PathNoStreamAvailableError
|
||||
if errors.As(err, &terr2) {
|
||||
if _, ok := errors.AsType[*defs.PathNoStreamAvailableError](err); ok {
|
||||
return &base.Response{
|
||||
StatusCode: base.StatusNotFound,
|
||||
}, nil, err
|
||||
|
||||
@@ -148,8 +148,7 @@ func (c *conn) runPublish(streamID *streamID) error {
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
var terr *auth.Error
|
||||
if errors.As(err, &terr) {
|
||||
if terr, ok := errors.AsType[*auth.Error](err); ok {
|
||||
// wait some seconds to delay brute force attacks
|
||||
<-time.After(auth.PauseAfterError)
|
||||
c.connReq.Reject(srt.REJ_PEER)
|
||||
@@ -275,8 +274,7 @@ func (c *conn) runRead(streamID *streamID) error {
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
var terr *auth.Error
|
||||
if errors.As(err, &terr) {
|
||||
if terr, ok := errors.AsType[*auth.Error](err); ok {
|
||||
// wait some seconds to delay brute force attacks
|
||||
<-time.After(auth.PauseAfterError)
|
||||
c.connReq.Reject(srt.REJ_PEER)
|
||||
|
||||
@@ -149,8 +149,7 @@ func (s *httpServer) checkAuthOutsideSession(ctx *gin.Context, pathName string,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
var terr *auth.Error
|
||||
if errors.As(err, &terr) {
|
||||
if terr, ok := errors.AsType[*auth.Error](err); ok {
|
||||
if terr.AskCredentials {
|
||||
ctx.Header("WWW-Authenticate", `Basic realm="mediamtx"`)
|
||||
s.writeErrorNoLog(ctx, http.StatusUnauthorized, fmt.Errorf("authentication error"))
|
||||
@@ -218,8 +217,7 @@ func (s *httpServer) onWHIPPost(ctx *gin.Context, pathName string, publish bool)
|
||||
|
||||
res2 := res.sx.initialRequest(initialRequestReq{})
|
||||
if res2.err != nil {
|
||||
var terr *auth.Error
|
||||
if errors.As(res2.err, &terr) {
|
||||
if terr, ok := errors.AsType[*auth.Error](res2.err); ok {
|
||||
if terr.AskCredentials {
|
||||
ctx.Header("WWW-Authenticate", `Basic realm="mediamtx"`)
|
||||
s.writeErrorNoLog(ctx, http.StatusUnauthorized, fmt.Errorf("authentication error"))
|
||||
|
||||
@@ -493,8 +493,7 @@ func (s *session) runRead(req *initialRequestReq) (int, error) {
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
var terr2 *defs.PathNoStreamAvailableError
|
||||
if errors.As(err, &terr2) {
|
||||
if _, ok := errors.AsType[*defs.PathNoStreamAvailableError](err); ok {
|
||||
return http.StatusNotFound, err
|
||||
}
|
||||
|
||||
|
||||
@@ -342,8 +342,7 @@ func (s *Source) waitForPrimary(
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
var err2 *defs.PathNoStreamAvailableError
|
||||
if errors.As(err, &err2) {
|
||||
if _, ok := errors.AsType[*defs.PathNoStreamAvailableError](err); ok {
|
||||
select {
|
||||
case <-time.After(pauseBetweenErrors):
|
||||
case <-params.Context.Done():
|
||||
|
||||
@@ -128,8 +128,7 @@ func (ssf *subStreamFormat) writeUnitInner(u *unit.Unit) error {
|
||||
ssf.streamFormat.rtpEncoder, err = newRTPEncoder(ssf.streamFormat.format, ssf.streamFormat.rtpMaxPayloadSize,
|
||||
new(pkt.SSRC), new(pkt.SequenceNumber))
|
||||
if err != nil {
|
||||
var err2 rtpEncoderNotAvailableError
|
||||
if errors.As(err, &err2) {
|
||||
if _, ok := errors.AsType[rtpEncoderNotAvailableError](err); ok {
|
||||
return fmt.Errorf("RTP payload size (%d) is greater than maximum allowed (%d)",
|
||||
len(pkt.Payload), ssf.streamFormat.rtpMaxPayloadSize)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user