Skip to content

Commit

Permalink
vendor kverror and show it off
Browse files Browse the repository at this point in the history
  • Loading branch information
sethgrid committed Dec 1, 2024
1 parent 2bca8d1 commit 62a6816
Show file tree
Hide file tree
Showing 366 changed files with 221 additions and 80,706 deletions.
21 changes: 2 additions & 19 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,50 +1,33 @@
module github.com/sethgrid/helloworld

go 1.21

toolchain go1.23.1
go 1.23.1

require (
github.com/a-h/templ v0.2.778
github.com/coreos/go-oidc/v3 v3.11.0
github.com/fatih/color v1.17.0
github.com/go-chi/chi/v5 v5.1.0
github.com/go-rod/rod v0.114.5
github.com/go-sql-driver/mysql v1.8.1
github.com/google/uuid v1.6.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/prometheus/client_golang v1.20.3
github.com/sendgrid/sendgrid-go v3.16.0+incompatible
github.com/stretchr/testify v1.9.0
golang.org/x/crypto v0.27.0
)

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/go-jose/go-jose/v4 v4.0.4 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.59.1 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/sendgrid/rest v2.6.9+incompatible // indirect
google.golang.org/protobuf v1.34.2 // indirect
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/ysmood/fetchup v0.2.3 // indirect
github.com/ysmood/goob v0.4.0 // indirect
github.com/ysmood/got v0.34.1 // indirect
github.com/ysmood/gson v0.7.3 // indirect
github.com/ysmood/leakless v0.8.0 // indirect
golang.org/x/oauth2 v0.23.0
github.com/sethgrid/kverr v0.0.0-20241118164339-2e0df7a392b4 // indirect
golang.org/x/sys v0.25.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
43 changes: 2 additions & 41 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/a-h/templ v0.2.778 h1:VzhOuvWECrwOec4790lcLlZpP4Iptt5Q4K9aFxQmtaM=
github.com/a-h/templ v0.2.778/go.mod h1:lq48JXoUvuQrU0VThrK31yFwdRjTCnIE5bcPCM9IP1w=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/coreos/go-oidc/v3 v3.11.0 h1:Ia3MxdwpSw702YW0xgfmP1GVCMA9aEFWu12XUZ3/OtI=
github.com/coreos/go-oidc/v3 v3.11.0/go.mod h1:gE3LgjOgFoHi9a4ce4/tJczr0Ai2/BoDhf0r5lltWI0=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-jose/go-jose/v4 v4.0.4 h1:VsjPI33J0SB9vQM6PLmNjoHqMQNGPiZ0rHL7Ni7Q6/E=
github.com/go-jose/go-jose/v4 v4.0.4/go.mod h1:NKb5HO1EZccyMpiZNbdUw/14tiXNyUJh188dfnMCAfc=
github.com/go-rod/rod v0.114.5 h1:1x6oqnslwFVuXJbJifgxspJUd3O4ntaGhRLHt+4Er9c=
github.com/go-rod/rod v0.114.5/go.mod h1:aiedSEFg5DwG/fnNbUOTPMTTWX3MRj6vIs/a684Mthw=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
Expand All @@ -35,11 +25,6 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -54,34 +39,10 @@ github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0leargg
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/sendgrid/rest v2.6.9+incompatible h1:1EyIcsNdn9KIisLW50MKwmSRSK+ekueiEMJ7NEoxJo0=
github.com/sendgrid/rest v2.6.9+incompatible/go.mod h1:kXX7q3jZtJXK5c5qK83bSGMdV6tsOE70KbHoqJls4lE=
github.com/sendgrid/sendgrid-go v3.16.0+incompatible h1:i8eE6IMkiCy7vusSdacHHSBUpXyTcTXy/Rl9N9aZ/Qw=
github.com/sendgrid/sendgrid-go v3.16.0+incompatible/go.mod h1:QRQt+LX/NmgVEvmdRw0VT/QgUn499+iza2FnDca9fg8=
github.com/sethgrid/kverr v0.0.0-20241118164339-2e0df7a392b4 h1:MHCqE94rEMy/N8AZUJeDM8t5bZXNwGPeyKXIg3tEsBc=
github.com/sethgrid/kverr v0.0.0-20241118164339-2e0df7a392b4/go.mod h1:AF7s3N8pefsxZ5IbOeaX5urMkDCSo0nqOYZPgcw/FCo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/ysmood/fetchup v0.2.3 h1:ulX+SonA0Vma5zUFXtv52Kzip/xe7aj4vqT5AJwQ+ZQ=
github.com/ysmood/fetchup v0.2.3/go.mod h1:xhibcRKziSvol0H1/pj33dnKrYyI2ebIvz5cOOkYGns=
github.com/ysmood/goob v0.4.0 h1:HsxXhyLBeGzWXnqVKtmT9qM7EuVs/XOgkX7T6r1o1AQ=
github.com/ysmood/goob v0.4.0/go.mod h1:u6yx7ZhS4Exf2MwciFr6nIM8knHQIE22lFpWHnfql18=
github.com/ysmood/gop v0.0.2 h1:VuWweTmXK+zedLqYufJdh3PlxDNBOfFHjIZlPT2T5nw=
github.com/ysmood/gop v0.0.2/go.mod h1:rr5z2z27oGEbyB787hpEcx4ab8cCiPnKxn0SUHt6xzk=
github.com/ysmood/got v0.34.1 h1:IrV2uWLs45VXNvZqhJ6g2nIhY+pgIG1CUoOcqfXFl1s=
github.com/ysmood/got v0.34.1/go.mod h1:yddyjq/PmAf08RMLSwDjPyCvHvYed+WjHnQxpH851LM=
github.com/ysmood/gotrace v0.6.0 h1:SyI1d4jclswLhg7SWTL6os3L1WOKeNn/ZtzVQF8QmdY=
github.com/ysmood/gotrace v0.6.0/go.mod h1:TzhIG7nHDry5//eYZDYcTzuJLYQIkykJzCRIo4/dzQM=
github.com/ysmood/gson v0.7.3 h1:QFkWbTH8MxyUTKPkVWAENJhxqdBa4lYTQWqZCiLG6kE=
github.com/ysmood/gson v0.7.3/go.mod h1:3Kzs5zDl21g5F/BlLTNcuAGAYLKt2lV5G8D1zF3RNmg=
github.com/ysmood/leakless v0.8.0 h1:BzLrVoiwxikpgEQR0Lk8NyBN5Cit2b1z+u0mgL4ZJak=
github.com/ysmood/leakless v0.8.0/go.mod h1:R8iAXPRaG97QJwqxs74RdwzcRHT1SWCGTNqY8q0JvMQ=
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs=
golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
Expand Down
11 changes: 4 additions & 7 deletions server/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

"github.com/sethgrid/helloworld/logger"
"github.com/sethgrid/kverr"
)

type errorResp struct {
Expand All @@ -13,13 +14,9 @@ type errorResp struct {

// ErrorJSON prepares the user message for json format. In the event an err is present, an ERROR level log will be emitted, else INFO
func (s *Server) ErrorJSON(w http.ResponseWriter, r *http.Request, statusCode int, userMsg string, err error) {
logger := logger.FromRequest(r).With("status_code", statusCode)
if err != nil {
defer logger.Error(userMsg, "error", err.Error())
} else {
// the main use case for this is to suppress login or other user input errors being reported as errors
defer logger.Info(userMsg)
}
// NOTE: if this was a kverr, those key:value pairs will be pulled out and attached to our error log here
logger := logger.FromRequest(r).With("status_code", statusCode).With(kverr.YoinkArgs(err)...)
defer logger.Error(userMsg, "error", err.Error())

w.Header().Set("content-type", "application/json")
w.WriteHeader(statusCode)
Expand Down
21 changes: 21 additions & 0 deletions server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,40 @@ package server

import (
"encoding/json"
"errors"
"math/rand"
"net/http"

"github.com/sethgrid/helloworld/logger"

"github.com/sethgrid/kverr"
)

type helloworldResp struct {
Hello string `json:"hello"`
}

func (s *Server) helloworldHandler(w http.ResponseWriter, r *http.Request) {

// simulate some work
if err := RandomFailure(); err != nil {
// NOTE: we don't have to tell other services that a kverr is being passed in
s.ErrorJSON(w, r, http.StatusInternalServerError, "random failure", err)
return
}

resp := helloworldResp{Hello: "World!"}

if err := json.NewEncoder(w).Encode(resp); err != nil {
logger.FromRequest(r).Error("unable to encode json", "error", err.Error())
}
}

func RandomFailure() error {
val := rand.Intn(3)
if rand.Intn(2) == 0 { // Generate a random integer: 0 or 1
// NOTE: the key value pair "val":val will be available to the server error logs
return kverr.New(errors.New("operation failed"), "val", val)
}
return nil
}
8 changes: 5 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/x509"
"database/sql"
"fmt"
"log"
"log/slog"
"net"
"net/http"
Expand Down Expand Up @@ -78,17 +79,18 @@ func New(conf Config) (*Server, error) {
} else {
rootLogger.Error("secure cookies and tls to the db are turned off")
}
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?timeout=5s&parseTime=true%s",
// TODO: after db bootstrap, include db name before timeout and other options
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/?timeout=5s&parseTime=true%s",
conf.DBUser,
conf.DBPass,
conf.DBHost,
strings.TrimPrefix(conf.DBPort, ":"),
conf.DBName,

customTLS,
)

// helpful if you are having trouble reaching the db; warning: prints password
// log.Println("dsn: ", dsn)
log.Println("dsn: ", dsn)

db, err := sql.Open("mysql", dsn)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion taskqueue/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"log/slog"
"sync"
"time"

"github.com/sethgrid/kverr"
)

type InMemoryTaskQueue struct {
Expand Down Expand Up @@ -71,7 +73,7 @@ func (m *InMemoryTaskQueue) MarkTaskComplete(taskID int) error {

task, exists := m.tasks[taskID]
if !exists {
return fmt.Errorf("task not found")
return kverr.New(fmt.Errorf("task not found"), "task_id", task.ID)
}
task.mu.Lock()
defer task.mu.Unlock()
Expand Down
6 changes: 4 additions & 2 deletions taskqueue/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"log/slog"
"time"

"github.com/sethgrid/kverr"
)

type MySQLTaskQueue struct {
Expand Down Expand Up @@ -126,7 +128,7 @@ func (m *MySQLTaskQueue) MarkTaskComplete(taskID int) error {
FOR UPDATE
`, taskID).Scan(&task.ID, &task.UserID, &task.Status, &task.TaskType, &task.Attempts)
if err != nil {
return err
return kverr.New(err, "task_id", taskID)
}

// Step 2: Log the task details
Expand All @@ -139,7 +141,7 @@ func (m *MySQLTaskQueue) MarkTaskComplete(taskID int) error {
WHERE id = ?
`, taskID)
if err != nil {
return err
return kverr.New(err, "task_id", taskID, "user_id", task.UserID)
}

return nil
Expand Down
4 changes: 3 additions & 1 deletion taskqueue/taskqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"log/slog"
"sync"
"time"

"github.com/sethgrid/kverr"
)

type Task struct {
Expand Down Expand Up @@ -162,7 +164,7 @@ func (tq *Runner) processTask(task Task) {

err := tq.TaskStore.MarkTaskComplete(task.ID)
if err != nil {
logger.Error("unable to mark task complete")
logger.Error("unable to mark task complete", kverr.YoinkArgs(err)...)

}
}
3 changes: 0 additions & 3 deletions vendor/github.com/a-h/templ/.dockerignore

This file was deleted.

1 change: 0 additions & 1 deletion vendor/github.com/a-h/templ/.envrc

This file was deleted.

31 changes: 0 additions & 31 deletions vendor/github.com/a-h/templ/.gitignore

This file was deleted.

72 changes: 0 additions & 72 deletions vendor/github.com/a-h/templ/.goreleaser.yaml

This file was deleted.

Loading

0 comments on commit 62a6816

Please sign in to comment.