1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2025-11-23 04:53:36 +00:00

Add GatewayOpts.ErrorIsFatalClose

This commit is contained in:
diamondburned 2022-11-17 22:07:52 -08:00
parent 87c479a2dc
commit 78ad477b83
No known key found for this signature in database
GPG key ID: D78C4471CE776659

View file

@ -89,6 +89,23 @@ var DefaultGatewayOpts = GatewayOpts{
AlwaysCloseGracefully: true,
}
// ErrorIsFatalClose returns true if the error is a fatal close error. It uses
// opts.FatalCloseCodes to check for the codes.
func (opts GatewayOpts) ErrorIsFatalClose(err error) bool {
var closeErr *CloseEvent
if !errors.As(err, &closeErr) {
return false
}
for _, code := range opts.FatalCloseCodes {
if code == closeErr.Code {
return true
}
}
return false
}
// Gateway describes an instance that handles the Discord gateway. It is
// basically an abstracted concurrent event loop that the user could signal to
// start connecting to the Discord gateway server.
@ -306,14 +323,12 @@ func (g *Gateway) spin(ctx context.Context, h Handler) {
switch data := op.Data.(type) {
case *CloseEvent:
for _, code := range g.opts.FatalCloseCodes {
if code == data.Code {
// Don't wrap the error, but instead, just pipe it as-is
// through the channel.
g.outer.ch <- op
g.lastError = data
return
}
if g.opts.ErrorIsFatalClose(data) {
// Don't wrap the error, but instead, just pipe it as-is
// through the channel.
g.outer.ch <- op
g.lastError = data
return
}
}