Log errors if ErrorResponse panics

We have to do this because otherwise the errors never actually end up in
the ResponseData.
This commit is contained in:
Ben Visness 2021-09-05 19:00:25 -05:00
parent 59f5243839
commit 6f7237f656
6 changed files with 24 additions and 33 deletions

View File

@ -1,26 +0,0 @@
#!/bin/bash
set -eou pipefail
# This script is for use in local development only. It wipes the existing db,
# creates a new empty one, runs the initial migration to create the schema,
# and then imports actual db content on top of that.
# TODO(opensource): We should adapt Asaf's seedfile command and then delete this.
THIS_PATH=$(pwd)
BETA_PATH='/mnt/c/Users/bvisn/Developer/handmade/handmade-beta'
# BETA_PATH='/Users/benvisness/Developer/handmade/handmade-beta'
cd $BETA_PATH
docker-compose down -v
docker-compose up -d postgres
sleep 3
./scripts/db_import -d -n hmn_two -c
cd $THIS_PATH
go run src/main.go migrate 2021-03-10T05:16:21Z
cd $BETA_PATH
./scripts/db_import -d -n hmn_two -a ./dbdumps/hmn_pg_dump_2021-04-26
# ./scripts/db_import -d -n hmn_two -a ./dbdumps/hmn_pg_dump_2021-04-25

View File

@ -82,6 +82,12 @@ type EpisodeGuide struct {
Projects map[string]string // NOTE(asaf): Maps from slugs to default episode guide topic
}
func init() {
if Config.EpisodeGuide.Projects == nil {
Config.EpisodeGuide.Projects = make(map[string]string)
}
}
func (info PostgresConfig) DSN() string {
return fmt.Sprintf("user=%s password=%s host=%s port=%d dbname=%s", info.User, info.Password, info.Hostname, info.Port, info.DbName)
}

View File

@ -194,7 +194,7 @@ func LogPanicValue(logger *zerolog.Logger, val interface{}, msg string) {
}
if err, ok := val.(error); ok {
logger.Error().Stack().Err(err).Msg(msg)
logger.Error().Err(err).Msg(msg)
} else {
logger.Error().
Interface("recovered", val).

View File

@ -276,6 +276,13 @@ func (c *RequestContext) Redirect(dest string, code int) ResponseData {
}
func (c *RequestContext) ErrorResponse(status int, errs ...error) ResponseData {
defer func() {
if r := recover(); r != nil {
LogContextErrors(c, errs...)
panic(r)
}
}()
res := ResponseData{
StatusCode: status,
Errors: errs,

View File

@ -37,7 +37,7 @@ func NewWebsiteRoutes(longRequestContext context.Context, conn *pgxpool.Pool, pe
logPerf := TrackRequestPerf(c, perfCollector)
defer logPerf()
defer LogContextErrors(c, &res)
defer LogContextErrorsFromResponse(c, &res)
defer MiddlewarePanicCatcher(c, &res)
return h(c)
@ -53,7 +53,7 @@ func NewWebsiteRoutes(longRequestContext context.Context, conn *pgxpool.Pool, pe
logPerf := TrackRequestPerf(c, perfCollector)
defer logPerf()
defer LogContextErrors(c, &res)
defer LogContextErrorsFromResponse(c, &res)
defer MiddlewarePanicCatcher(c, &res)
defer storeNoticesInCookie(c, &res)
@ -75,7 +75,7 @@ func NewWebsiteRoutes(longRequestContext context.Context, conn *pgxpool.Pool, pe
logPerf := TrackRequestPerf(c, perfCollector)
defer logPerf()
defer LogContextErrors(c, &res)
defer LogContextErrorsFromResponse(c, &res)
defer MiddlewarePanicCatcher(c, &res)
defer storeNoticesInCookie(c, &res)
@ -558,12 +558,16 @@ func TrackRequestPerf(c *RequestContext, perfCollector *perf.PerfCollector) (aft
}
}
func LogContextErrors(c *RequestContext, res *ResponseData) {
for _, err := range res.Errors {
func LogContextErrors(c *RequestContext, errs ...error) {
for _, err := range errs {
c.Logger.Error().Timestamp().Stack().Str("Requested", c.FullUrl()).Err(err).Msg("error occurred during request")
}
}
func LogContextErrorsFromResponse(c *RequestContext, res *ResponseData) {
LogContextErrors(c, res.Errors...)
}
func MiddlewarePanicCatcher(c *RequestContext, res *ResponseData) {
if recovered := recover(); recovered != nil {
maybeError, ok := recovered.(*error)

View File

@ -28,7 +28,7 @@ func TestLogContextErrors(t *testing.T) {
Middleware: func(h Handler) Handler {
return func(c *RequestContext) (res ResponseData) {
c.Logger = &logger
defer LogContextErrors(c, &res)
defer LogContextErrorsFromResponse(c, &res)
return h(c)
}
},