From 1d647c7d001ad7a231c03d3a9adc5053d0e1c9a3 Mon Sep 17 00:00:00 2001 From: Sebastian Tobie Date: Sat, 19 Nov 2022 11:28:27 +0100 Subject: [PATCH] Added better error handling --- middleware/db/db.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/middleware/db/db.go b/middleware/db/db.go index 46d4c18..1ea44bc 100644 --- a/middleware/db/db.go +++ b/middleware/db/db.go @@ -29,7 +29,10 @@ var _ middleware.PostSetupMiddleware = &Middleware{} // GetConnection is an simple helper function that returns an connection to the db func GetConnection(c *gin.Context, db string) (*pgxpool.Conn, error) { if co, ok := c.Get(ContextKey); ok { - return co.(ConnGet)(db), nil + if cg, ok := co.(ConnGet); ok { + return cg(db), nil + } + return nil, fmt.Errorf("Failed to convert the method. %T != ConnGet", co) } return nil, errors.New("No db.Middleware set up. ") } @@ -101,7 +104,7 @@ func (m *Middleware) GetConn(name string) *pgxpool.Conn { // Gin is the Entrypoint for Gin. func (m *Middleware) Gin(c *gin.Context) { - c.Set(ContextKey, m.GetConn) + c.Set(ContextKey, ConnGet(m.GetConn)) } // Setup adds the connections from the configfile into the middleware