1
0
Fork 0

Added better error handling

Dieser Commit ist enthalten in:
Sebastian Tobie 2022-11-19 11:28:27 +01:00
Ursprung 2326ceb32e
Commit 1d647c7d00
1 geänderte Dateien mit 5 neuen und 2 gelöschten Zeilen

Datei anzeigen

@ -29,7 +29,10 @@ var _ middleware.PostSetupMiddleware = &Middleware{}
// GetConnection is an simple helper function that returns an connection to the db // GetConnection is an simple helper function that returns an connection to the db
func GetConnection(c *gin.Context, db string) (*pgxpool.Conn, error) { func GetConnection(c *gin.Context, db string) (*pgxpool.Conn, error) {
if co, ok := c.Get(ContextKey); ok { 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. ") 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. // Gin is the Entrypoint for Gin.
func (m *Middleware) Gin(c *gin.Context) { 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 // Setup adds the connections from the configfile into the middleware