~bigbes/core-go

465a6f71354e94f54f4545291ee747784a3e090b — Drew DeVault 1 year, 6 months ago fea51c6
all: pass errors to panic, not strings

GraphQL's recovery middleware can't handle strings so it just logs a
very not useful <nil>
M auth/bearer.go => auth/bearer.go +1 -1
@@ 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

M email/worker.go => email/worker.go +7 -7
@@ 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"))
		}
	}


M redis/middleware.go => redis/middleware.go +2 -1
@@ 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
}

M s3/middleware.go => s3/middleware.go +2 -1
@@ 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
}

M valid/valid.go => valid/valid.go +4 -4
@@ 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 {

M webhooks/config.go => webhooks/config.go +3 -3
@@ 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.

M webhooks/context.go => webhooks/context.go +1 -1
@@ 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 {

M webhooks/middleware.go => webhooks/middleware.go +3 -2
@@ 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
}