From 8d23867d2c6d3c6f0dd9681192b9b8642ceea254 Mon Sep 17 00:00:00 2001 From: Julio Capote Date: Mon, 2 Jan 2023 08:07:21 -0500 Subject: clean up error/response/not found handling --- models/persister.go | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'models') diff --git a/models/persister.go b/models/persister.go index 4381008..c6e0c72 100644 --- a/models/persister.go +++ b/models/persister.go @@ -82,14 +82,22 @@ func (p *Persister) Collect(model model) ([][]byte, error) { func (p *Persister) Find(model model) ([]byte, error) { var result []byte var item *badger.Item + var notFound bool err := p.db.View(func(txn *badger.Txn) error { var getErr error item, getErr = txn.Get([]byte(model.Key())) + if getErr == badger.ErrKeyNotFound { + notFound = true + return nil + } if getErr != nil { return getErr } return nil }) + if notFound { + return nil, nil // on key not found we want to return nil, so that gin returns 404 and not 500 + } if err != nil { return nil, err } -- cgit v1.2.3