Browse Source

changed iferr to short forms

dependabot/npm_and_yarn/web/prismjs-1.21.0
Max Schmitt 8 years ago
parent
commit
761cbdca15
  1. 1
      handlers/auth.go
  2. 9
      handlers/handlers_test.go
  3. 3
      handlers/public.go
  4. 9
      main.go
  5. 6
      store/store.go
  6. 6
      store/store_test.go
  7. 3
      store/util.go

1
handlers/auth.go

@ -127,7 +127,6 @@ func (h *Handler) handleGoogleCallback(c *gin.Context) {
user.Email, user.Email,
}) })
// Sign and get the complete encoded token as a string using the secret
tokenString, err := token.SignedString(h.config.Secret) tokenString, err := token.SignedString(h.config.Secret)
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("could not sign token: %v", err)}) c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("could not sign token: %v", err)})

9
handlers/handlers_test.go

@ -87,8 +87,7 @@ func TestCreateEntry(t *testing.T) {
return return
} }
var parsed URLUtil var parsed URLUtil
err = json.Unmarshal(respBody, &parsed) if err = json.Unmarshal(respBody, &parsed); err != nil {
if err != nil {
t.Fatalf("could not unmarshal data: %v", err) t.Fatalf("could not unmarshal data: %v", err)
} }
t.Run("test if shorted URL is correct", func(t *testing.T) { t.Run("test if shorted URL is correct", func(t *testing.T) {
@ -114,8 +113,7 @@ func TestHandleInfo(t *testing.T) {
} }
respBody := createEntryWithJSON(t, reqBody, "application/json; charset=utf-8", http.StatusOK) respBody := createEntryWithJSON(t, reqBody, "application/json; charset=utf-8", http.StatusOK)
var parsed URLUtil var parsed URLUtil
err = json.Unmarshal(respBody, &parsed) if err = json.Unmarshal(respBody, &parsed); err != nil {
if err != nil {
t.Fatalf("could not unmarshal data: %v", err) t.Fatalf("could not unmarshal data: %v", err)
} }
body, err := json.Marshal(struct { body, err := json.Marshal(struct {
@ -134,8 +132,7 @@ func TestHandleInfo(t *testing.T) {
t.Errorf("expected status %d; got %d", http.StatusOK, resp.StatusCode) t.Errorf("expected status %d; got %d", http.StatusOK, resp.StatusCode)
} }
var entry store.Entry var entry store.Entry
err = json.NewDecoder(resp.Body).Decode(&entry) if err = json.NewDecoder(resp.Body).Decode(&entry); err != nil {
if err != nil {
t.Fatalf("could not unmarshal data: %v", err) t.Fatalf("could not unmarshal data: %v", err)
} }
if entry.URL != testURL { if entry.URL != testURL {

3
handlers/public.go

@ -54,8 +54,7 @@ func (h *Handler) handleAccess(c *gin.Context) {
// handleCreate handles requests to create an entry // handleCreate handles requests to create an entry
func (h *Handler) handleCreate(c *gin.Context) { func (h *Handler) handleCreate(c *gin.Context) {
var data URLUtil var data URLUtil
err := c.ShouldBind(&data) if err := c.ShouldBind(&data); err != nil {
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return return
} }

9
main.go

@ -24,8 +24,7 @@ func main() {
} }
func initShortener() (func(), error) { func initShortener() (func(), error) {
err := config.Preload() if err := config.Preload(); err != nil {
if err != nil {
return nil, errors.Wrap(err, "could not get config") return nil, errors.Wrap(err, "could not get config")
} }
conf := config.Get() conf := config.Get()
@ -38,14 +37,12 @@ func initShortener() (func(), error) {
return nil, errors.Wrap(err, "could not create handlers") return nil, errors.Wrap(err, "could not create handlers")
} }
go func() { go func() {
err := handler.Listen() if err := handler.Listen(); err != nil {
if err != nil {
log.Fatalf("could not listen to http handlers: %v", err) log.Fatalf("could not listen to http handlers: %v", err)
} }
}() }()
return func() { return func() {
err = handler.CloseStore() if err = handler.CloseStore(); err != nil {
if err != nil {
log.Printf("failed to stop the handlers: %v", err) log.Printf("failed to stop the handlers: %v", err)
} }
}, nil }, nil

6
store/store.go

@ -70,8 +70,7 @@ func (s *Store) GetEntryByID(id string) (*Entry, error) {
return nil, err return nil, err
} }
var entry *Entry var entry *Entry
err = json.Unmarshal(raw, &entry) return entry, json.Unmarshal(raw, &entry)
return entry, err
} }
// IncreaseVisitCounter increments the visit counter of an entry // IncreaseVisitCounter increments the visit counter of an entry
@ -88,8 +87,7 @@ func (s *Store) IncreaseVisitCounter(id string) error {
} }
err = s.db.Update(func(tx *bolt.Tx) error { err = s.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(s.bucketName) bucket := tx.Bucket(s.bucketName)
err := bucket.Put([]byte(id), raw) if err := bucket.Put([]byte(id), raw); err != nil {
if err != nil {
return errors.Wrap(err, "could not put data into bucket") return errors.Wrap(err, "could not put data into bucket")
} }
return nil return nil

6
store/store_test.go

@ -106,8 +106,7 @@ func TestIncreaseVisitCounter(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("could not get entry by id: %v", err) t.Fatalf("could not get entry by id: %v", err)
} }
err = store.IncreaseVisitCounter(id) if err = store.IncreaseVisitCounter(id); err != nil {
if err != nil {
t.Fatalf("could not increase visit counter %v", err) t.Fatalf("could not increase visit counter %v", err)
} }
entryAfterInc, err := store.GetEntryByID(id) entryAfterInc, err := store.GetEntryByID(id)
@ -117,8 +116,7 @@ func TestIncreaseVisitCounter(t *testing.T) {
if entryBeforeInc.VisitCount+1 != entryAfterInc.VisitCount { if entryBeforeInc.VisitCount+1 != entryAfterInc.VisitCount {
t.Fatalf("the increasement was not successful, the visit count is not correct") t.Fatalf("the increasement was not successful, the visit count is not correct")
} }
err = store.IncreaseVisitCounter("") if err = store.IncreaseVisitCounter(""); err != ErrIDIsEmpty {
if err != ErrIDIsEmpty {
t.Fatalf("could not get expected '%v' error: %v", ErrIDIsEmpty, err) t.Fatalf("could not get expected '%v' error: %v", ErrIDIsEmpty, err)
} }
} }

3
store/util.go

@ -30,8 +30,7 @@ func (s *Store) createEntryRaw(key, value []byte) error {
if raw != nil { if raw != nil {
return errors.New("entry value is not empty") return errors.New("entry value is not empty")
} }
err := bucket.Put(key, value) if err := bucket.Put(key, value); err != nil {
if err != nil {
return errors.Wrap(err, "could not put data into bucket") return errors.Wrap(err, "could not put data into bucket")
} }
return nil return nil

Loading…
Cancel
Save