From 465a6f71354e94f54f4545291ee747784a3e090b Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 23 Jan 2025 12:27:01 +0100 Subject: [PATCH] all: pass errors to panic, not strings GraphQL's recovery middleware can't handle strings so it just logs a very not useful --- auth/bearer.go | 2 +- email/worker.go | 14 +++++++------- redis/middleware.go | 3 ++- s3/middleware.go | 3 ++- valid/valid.go | 8 ++++---- webhooks/config.go | 6 +++--- webhooks/context.go | 2 +- webhooks/middleware.go | 5 +++-- 8 files changed, 23 insertions(+), 20 deletions(-) diff --git a/auth/bearer.go b/auth/bearer.go index a1bc435a4648bfd15c126d5c4e91865186088c97..1c738024c06d832029091ddf79c2b7869ebc95c8 100644 --- a/auth/bearer.go +++ b/auth/bearer.go @@ -141,7 +141,7 @@ func (g *Grants) Has(grant string, mode string) bool { } if mode != RO && mode != RW { - panic("Invalid access mode") + panic(fmt.Errorf("Invalid access mode")) } if g.ReadOnly && mode == RW { return false diff --git a/email/worker.go b/email/worker.go index 8fef81438a008da4ddf11c6aa2df0dca03f2a1ab..e2f48964697e3e04e61eb1b81775579550a07c48 100644 --- a/email/worker.go +++ b/email/worker.go @@ -184,7 +184,7 @@ func EnqueueStd(ctx context.Context, header mail.Header, func EnqueueRaw(ctx context.Context, data []byte, to []string) error { if len(to) == 0 { - panic("recipients list cannot be empty") + panic(fmt.Errorf("recipients list cannot be empty")) } queue := ForContext(ctx).queue return queue.Enqueue(NewTask(data, to)) @@ -214,19 +214,19 @@ func (q *Queue) CanPGPSign() bool { func NewQueue(conf ini.File) *Queue { smtpFrom, ok := conf.Get("mail", "smtp-from") if !ok { - panic("Expected [mail]smtp-from in config") + panic(fmt.Errorf("Expected [mail]smtp-from in config")) } ownerName, ok := conf.Get("sr.ht", "owner-name") if !ok { - panic("Expected [sr.ht]owner-name in config") + panic(fmt.Errorf("Expected [sr.ht]owner-name in config")) } ownerEmail, ok := conf.Get("sr.ht", "owner-email") if !ok { - panic("Expected [sr.ht]owner-email in config") + panic(fmt.Errorf("Expected [sr.ht]owner-email in config")) } addr, err := mail.ParseAddress(smtpFrom) if err != nil { - panic("Invalid [mail]smtp-from: " + err.Error()) + panic(fmt.Errorf("Invalid [mail]smtp-from: %s", err.Error())) } ownerAddr := &mail.Address{ Name: ownerName, @@ -246,11 +246,11 @@ func NewQueue(conf ini.File) *Queue { panic(fmt.Errorf("Failed to read PGP key ring from [mail]pgp-privkey: %v", err)) } if len(keyring) != 1 { - panic("Expected [mail]pgp-privkey to contain one key") + panic(fmt.Errorf("Expected [mail]pgp-privkey to contain one key")) } entity = keyring[0] if entity.PrivateKey == nil || entity.PrivateKey.Encrypted { - panic("Failed to load [mail]pgp-privkey for email signature") + panic(fmt.Errorf("Failed to load [mail]pgp-privkey for email signature")) } } diff --git a/redis/middleware.go b/redis/middleware.go index bc9da61fdaac0aa09edf56c8c63024e521007632..295cd6a4bd0d006e5876114b539c14fb0a65b25a 100644 --- a/redis/middleware.go +++ b/redis/middleware.go @@ -2,6 +2,7 @@ package redis import ( "context" + "fmt" "net/http" "github.com/go-redis/redis/v8" @@ -30,7 +31,7 @@ func Context(ctx context.Context, client redis.UniversalClient) context.Context func ForContext(ctx context.Context) redis.UniversalClient { raw, ok := ctx.Value(redisCtxKey).(redis.UniversalClient) if !ok { - panic("Invalid redis context") + panic(fmt.Errorf("Invalid redis context")) } return raw } diff --git a/s3/middleware.go b/s3/middleware.go index a4834050bbdd4e7814bd5824ac3fbd9296925ee4..217ee1fcfa2830ad6fd02def22ea28f73777a72c 100644 --- a/s3/middleware.go +++ b/s3/middleware.go @@ -3,6 +3,7 @@ package s3 import ( "context" "errors" + "fmt" "net/http" "github.com/minio/minio-go/v7" @@ -33,7 +34,7 @@ func Context(ctx context.Context, client *minio.Client) context.Context { func ForContext(ctx context.Context) *minio.Client { raw, ok := ctx.Value(minioCtxKey).(*minio.Client) if !ok { - panic("Invalid minio context") + panic(fmt.Errorf("Invalid minio context")) } return raw } diff --git a/valid/valid.go b/valid/valid.go index e352ef5585d789c435e8fc478a354739cddbf0ef..9a49711b00daa6da4882c71d7550df74012bc2ed 100644 --- a/valid/valid.go +++ b/valid/valid.go @@ -64,7 +64,7 @@ func (valid *Validation) Ok() bool { // validation with. func (valid *Validation) Optional(name string, fn func(i interface{})) { if valid.input == nil { - panic("Attempted to validate fields without input") + panic(fmt.Errorf("Attempted to validate fields without input")) } if o, ok := valid.input[name]; ok { if o == nil { @@ -80,7 +80,7 @@ func (valid *Validation) Optional(name string, fn func(i interface{})) { // called with the string for the user to conduct further validation with. func (valid *Validation) OptionalString(name string, fn func(s string)) { if valid.input == nil { - panic("Attempted to validate fields without input") + panic(fmt.Errorf("Attempted to validate fields without input")) } if o, ok := valid.input[name]; ok { if o == nil { @@ -109,7 +109,7 @@ func (valid *Validation) OptionalString(name string, fn func(s string)) { // validation with. func (valid *Validation) NullableString(name string, fn func(s *string)) { if valid.input == nil { - panic("Attempted to validate fields without input") + panic(fmt.Errorf("Attempted to validate fields without input")) } if o, ok := valid.input[name]; ok { var val *string @@ -136,7 +136,7 @@ func (valid *Validation) NullableString(name string, fn func(s *string)) { // called with the boolean for the user to conduct further validation with. func (valid *Validation) OptionalBool(name string, fn func(b bool)) { if valid.input == nil { - panic("Attempted to validate fields without input") + panic(fmt.Errorf("Attempted to validate fields without input")) } if o, ok := valid.input[name]; ok { if o == nil { diff --git a/webhooks/config.go b/webhooks/config.go index 73b41e679a9882c6a8ca645283dd1e5ab865818d..e85c3edc76658fca9a3074b7a1b1daa596dc094d 100644 --- a/webhooks/config.go +++ b/webhooks/config.go @@ -54,11 +54,11 @@ func NewAuthConfig(ctx context.Context) (AuthConfig, error) { return AuthConfig{}, fmt.Errorf("Native webhooks are not supported with web authentication") case auth.AUTH_INTERNAL: // TODO: Should this work? - panic("Internal webtoken auth is not supported") + panic(fmt.Errorf("Internal webtoken auth is not supported")) case auth.AUTH_WEBHOOK: - panic("Recursive webhook auth is not supported") + panic(fmt.Errorf("Recursive webhook auth is not supported")) } - panic("Unreachable") + panic(fmt.Errorf("Unreachable")) } // Returns an SQL expression to filter webhooks for the authenticated user. diff --git a/webhooks/context.go b/webhooks/context.go index 0ebc794484a9e2fa3bd2a4e8e340191b1eb6a19d..4c645dbcc16be739e568dac0558f15fe785ad246 100644 --- a/webhooks/context.go +++ b/webhooks/context.go @@ -51,7 +51,7 @@ func (webhook *WebhookContext) Exec(ctx context.Context, schema graphql.ExecutableSchema) ([]byte, error) { sub := webhook.Subscription if sub.AuthMethod != auth.AUTH_OAUTH2 { - panic("TODO") + panic(fmt.Errorf("TODO")) } tslice, err := hex.DecodeString(*sub.TokenHash) if err != nil { diff --git a/webhooks/middleware.go b/webhooks/middleware.go index d59508b73aab90c756b0f04bd59d5a3dd7a4aad5..de34b4436c6b3075e0ad2016041f458914d6e582 100644 --- a/webhooks/middleware.go +++ b/webhooks/middleware.go @@ -2,6 +2,7 @@ package webhooks import ( "context" + "fmt" "net/http" ) @@ -20,7 +21,7 @@ func Middleware(queue *WebhookQueue) func(next http.Handler) http.Handler { func ForContext(ctx context.Context) *WebhookQueue { queue, ok := ctx.Value(ctxKey).(*WebhookQueue) if !ok { - panic("No webhook queue for this context") + panic(fmt.Errorf("No webhook queue for this context")) } return queue } @@ -40,7 +41,7 @@ func LegacyMiddleware(queue *LegacyQueue) func(next http.Handler) http.Handler { func LegacyForContext(ctx context.Context) *LegacyQueue { queue, ok := ctx.Value(legacyCtxKey).(*LegacyQueue) if !ok { - panic("No legacy webhook queue for this context") + panic(fmt.Errorf("No legacy webhook queue for this context")) } return queue }