Skip to content

Commit

Permalink
Add cleaner handling for when entry is not found (#158)
Browse files Browse the repository at this point in the history
* Add cleaner handling for when entry is not found

* Fix unit tests
  • Loading branch information
mtlynch authored Aug 15, 2022
1 parent b390576 commit 20a9758
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
9 changes: 7 additions & 2 deletions handlers/paste.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/gorilla/mux"

"github.com/mtlynch/logpaste/random"
"github.com/mtlynch/logpaste/store"
)

const MaxPasteCharacters = 2 * 1000 * 1000
Expand All @@ -28,8 +29,12 @@ func (s defaultServer) pasteGet() http.HandlerFunc {
w.Header().Set("Content-Type", "text/plain; charset=UTF-8")
contents, err := s.store.GetEntry(id)
if err != nil {
log.Printf("Error retrieving entry with id %s: %v", id, err)
http.Error(w, "entry not found", http.StatusNotFound)
if _, ok := err.(store.EntryNotFoundError); ok {
http.Error(w, "entry not found", http.StatusNotFound)
return
}
log.Printf("failed to retrieve entry %s from datastore: %v", id, err)
http.Error(w, "failed to retrieve entry", http.StatusInternalServerError)
return
}
io.WriteString(w, contents)
Expand Down
4 changes: 2 additions & 2 deletions handlers/paste_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package handlers

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -11,6 +10,7 @@ import (
"testing"

"github.com/gorilla/mux"
"github.com/mtlynch/logpaste/store"
)

type mockStore struct {
Expand All @@ -21,7 +21,7 @@ func (ds mockStore) GetEntry(id string) (string, error) {
if contents, ok := ds.entries[id]; ok {
return contents, nil
}
return "", errors.New("not found")
return "", store.EntryNotFoundError{ID: id}
}

func (ds *mockStore) InsertEntry(id string, contents string) error {
Expand Down
3 changes: 3 additions & 0 deletions store/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ func New() store.Store {
func (d db) GetEntry(id string) (string, error) {
var contents string
if err := d.ctx.QueryRow("SELECT contents FROM entries WHERE id=?", id).Scan(&contents); err != nil {
if err == sql.ErrNoRows {
return "", store.EntryNotFoundError{ID: id}
}
return "", err
}
return contents, nil
Expand Down
11 changes: 11 additions & 0 deletions store/store.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
package store

import "fmt"

type Store interface {
GetEntry(id string) (string, error)
InsertEntry(id string, contents string) error
}

// EntryNotFoundError occurs when no entry exists with the given ID.
type EntryNotFoundError struct {
ID string
}

func (f EntryNotFoundError) Error() string {
return fmt.Sprintf("could not find entry with ID=%v", f.ID)
}

0 comments on commit 20a9758

Please sign in to comment.