Skip to content

Commit

Permalink
Added few error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
hetjagani committed Nov 27, 2020
1 parent 5cf55d3 commit 6a45927
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
27 changes: 26 additions & 1 deletion errorx/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package errorx

import (
"fmt"
"net/http"

"github.com/factly/x/renderx"
Expand All @@ -16,7 +17,7 @@ type Message struct {
// InvalidID error
func InvalidID() Message {
return Message{
Code: http.StatusNotFound,
Code: http.StatusBadRequest,
Message: "Invalid ID",
}
}
Expand Down Expand Up @@ -69,6 +70,30 @@ func CannotSaveChanges() Message {
}
}

//Unauthorized - errors for unauthorized response
func Unauthorized() Message {
return Message{
Code: http.StatusUnauthorized,
Message: "Not allowed",
}
}

//SameNameExist - errors for same name exists
func SameNameExist() Message {
return Message{
Code: http.StatusUnprocessableEntity,
Message: "Entity with same name exists",
}
}

//CannotDelete - errors for cannot delete associations
func CannotDelete(entity, association string) Message {
return Message{
Code: http.StatusUnprocessableEntity,
Message: fmt.Sprint(entity, " is associated with some ", association),
}
}

// GetMessage - get errorx.Message object
func GetMessage(msg string, code int) Message {
return Message{
Expand Down
38 changes: 37 additions & 1 deletion errorx/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestErrorx(t *testing.T) {
t.Run("get InvalidID error message", func(t *testing.T) {
err := InvalidID()

if err.Code != http.StatusNotFound {
if err.Code != http.StatusBadRequest {
t.Errorf("Returned wrong code expected %v, got %v", http.StatusNotFound, err.Code)
}

Expand Down Expand Up @@ -116,4 +116,40 @@ func TestErrorx(t *testing.T) {
t.Errorf("Returned wrong message expected %v, got %v", "Can not save changes", err.Message)
}
})

t.Run("get Unauthorized error message", func(t *testing.T) {
err := Unauthorized()

if err.Code != http.StatusUnauthorized {
t.Errorf("Returned wrong code expected %v, got %v", http.StatusUnauthorized, err.Code)
}

if err.Message != "Not allowed" {
t.Errorf("Returned wrong message expected %v, got %v", "Not allowed", err.Message)
}
})

t.Run("get SameNameExist error message", func(t *testing.T) {
err := SameNameExist()

if err.Code != http.StatusUnprocessableEntity {
t.Errorf("Returned wrong code expected %v, got %v", http.StatusUnprocessableEntity, err.Code)
}

if err.Message != "Entity with same name exists" {
t.Errorf("Returned wrong message expected %v, got %v", "Entity with same name exists", err.Message)
}
})

t.Run("get CannotDelete error message", func(t *testing.T) {
err := CannotDelete("tag", "post")

if err.Code != http.StatusUnprocessableEntity {
t.Errorf("Returned wrong code expected %v, got %v", http.StatusUnprocessableEntity, err.Code)
}

if err.Message != "tag is associated with some post" {
t.Errorf("Returned wrong message expected %v, got %v", "tag is associated with some post", err.Message)
}
})
}

0 comments on commit 6a45927

Please sign in to comment.