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,
})
// Sign and get the complete encoded token as a string using the secret
tokenString, err := token.SignedString(h.config.Secret)
if err != nil {
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
}
var parsed URLUtil
err = json.Unmarshal(respBody, &parsed)
if err != nil {
if err = json.Unmarshal(respBody, &parsed); err != nil {
t.Fatalf("could not unmarshal data: %v", err)
}
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)
var parsed URLUtil
err = json.Unmarshal(respBody, &parsed)
if err != nil {
if err = json.Unmarshal(respBody, &parsed); err != nil {
t.Fatalf("could not unmarshal data: %v", err)
}
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)
}
var entry store.Entry
err = json.NewDecoder(resp.Body).Decode(&entry)
if err != nil {
if err = json.NewDecoder(resp.Body).Decode(&entry); err != nil {
t.Fatalf("could not unmarshal data: %v", err)
}
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
func (h *Handler) handleCreate(c *gin.Context) {
var data URLUtil
err := c.ShouldBind(&data)
if err != nil {
if err := c.ShouldBind(&data); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

9
main.go

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

6
store/store.go

@ -70,8 +70,7 @@ func (s *Store) GetEntryByID(id string) (*Entry, error) {
return nil, err
}
var entry *Entry
err = json.Unmarshal(raw, &entry)
return entry, err
return entry, json.Unmarshal(raw, &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 {
bucket := tx.Bucket(s.bucketName)
err := bucket.Put([]byte(id), raw)
if err != nil {
if err := bucket.Put([]byte(id), raw); err != nil {
return errors.Wrap(err, "could not put data into bucket")
}
return nil

6
store/store_test.go

@ -106,8 +106,7 @@ func TestIncreaseVisitCounter(t *testing.T) {
if err != nil {
t.Fatalf("could not get entry by id: %v", err)
}
err = store.IncreaseVisitCounter(id)
if err != nil {
if err = store.IncreaseVisitCounter(id); err != nil {
t.Fatalf("could not increase visit counter %v", err)
}
entryAfterInc, err := store.GetEntryByID(id)
@ -117,8 +116,7 @@ func TestIncreaseVisitCounter(t *testing.T) {
if entryBeforeInc.VisitCount+1 != entryAfterInc.VisitCount {
t.Fatalf("the increasement was not successful, the visit count is not correct")
}
err = store.IncreaseVisitCounter("")
if err != ErrIDIsEmpty {
if err = store.IncreaseVisitCounter(""); err != ErrIDIsEmpty {
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 {
return errors.New("entry value is not empty")
}
err := bucket.Put(key, value)
if err != nil {
if err := bucket.Put(key, value); err != nil {
return errors.Wrap(err, "could not put data into bucket")
}
return nil

Loading…
Cancel
Save