diff --git a/resetdb.sh b/resetdb.sh deleted file mode 100755 index a79775a4..00000000 --- a/resetdb.sh +++ /dev/null @@ -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 diff --git a/src/config/types.go b/src/config/types.go index bd25fa69..397c65a4 100644 --- a/src/config/types.go +++ b/src/config/types.go @@ -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) } diff --git a/src/logging/logging.go b/src/logging/logging.go index 10c07595..74a51dba 100644 --- a/src/logging/logging.go +++ b/src/logging/logging.go @@ -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). diff --git a/src/website/requesthandling.go b/src/website/requesthandling.go index ec71dff5..208efd38 100644 --- a/src/website/requesthandling.go +++ b/src/website/requesthandling.go @@ -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, diff --git a/src/website/routes.go b/src/website/routes.go index eaed141f..ee74dbb1 100644 --- a/src/website/routes.go +++ b/src/website/routes.go @@ -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) diff --git a/src/website/routes_test.go b/src/website/routes_test.go index bbd822a9..a706c5e0 100644 --- a/src/website/routes_test.go +++ b/src/website/routes_test.go @@ -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) } },