Skip to content

Commit

Permalink
Merge 9ea8348 into e74b5bd
Browse files Browse the repository at this point in the history
  • Loading branch information
denge90 committed Jul 29, 2016
2 parents e74b5bd + 9ea8348 commit a50a734
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 8 deletions.
5 changes: 4 additions & 1 deletion composition/composition_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ func (agg *CompositionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
mergeContext.AddContent(res)

} else if res.Def.Required {
logging.Application(r.Header).WithField("fetchResult", res).Errorf("error loading content from: %v", res.Def.URL)
// 404 Error already become logged in logger.go
if res.Content.HttpStatusCode() != 404 {
logging.Application(r.Header).WithField("fetchResult", res).Errorf("error loading content from: %v", res.Def.URL)
}
res.Def.ErrHandler.Handle(res.Err, res.Content.HttpStatusCode(), w, r)
return
} else {
Expand Down
12 changes: 8 additions & 4 deletions composition/content_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,14 @@ func (fetcher *ContentFetcher) AddFetchJob(d *FetchDefinition) {
}
}
} else {
logging.Logger.WithError(fetchResult.Err).
WithField("fetchDefinition", d).
WithField("correlation_id", logging.GetCorrelationId(definitionCopy.Header)).
Errorf("failed fetching %v", d.URL)
// 404 Error already become logged in logger.go
if fetchResult.Content.HttpStatusCode() != 404 {
logging.Logger.WithError(fetchResult.Err).
WithField("fetchDefinition", d).
WithField("correlation_id", logging.GetCorrelationId(definitionCopy.Header)).
Errorf("failed fetching %v", d.URL)
}

}
}()
}
Expand Down
22 changes: 21 additions & 1 deletion composition/content_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"io"
"strings"
// "fmt"
)

const (
Expand Down Expand Up @@ -42,7 +43,8 @@ func (cntx *ContentMerge) GetHtml() ([]byte, error) {
executeFragment = func(fragmentName string) error {
f, exist := cntx.Body[fragmentName]
if !exist {
return errors.New("Fragment does not exist: " + fragmentName)
missingFragmentString := generateMissingFragmentString(cntx.Body, fragmentName)
return errors.New(missingFragmentString)
}
return f.Execute(w, cntx.MetaJSON, executeFragment)
}
Expand Down Expand Up @@ -120,3 +122,21 @@ func urlToFragmentName(url string) string {
url = strings.Replace(url, `]§`, `\]\§`, -1)
return url
}

// Generates String for the missing Fragment error message. It adds all existing fragments from the body
func generateMissingFragmentString(body map[string]Fragment, fragmentName string) string {
text := "Fragment does not exist: " + fragmentName + ". Existing fragments: "
index := 0
for k, _ := range body {

if k != "" {
if index == 0 {
text += k
} else {
text += ", " + k
}
index++
}
}
return text
}
18 changes: 17 additions & 1 deletion composition/content_merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ func Test_ContentMerge_PositiveCase(t *testing.T) {
type MockPage1BodyFragment struct {
}

func Test_GenerateMissingFragmentString(t *testing.T) {
body := map[string]Fragment{
"footer": nil,
"header": nil,
"": nil,
}
fragmentName := "body"
fragmentString := generateMissingFragmentString(body, fragmentName)

a := assert.New(t)
a.Contains(fragmentString, "Fragment does not exist: body.")
a.Contains(fragmentString, "footer")
a.Contains(fragmentString, "header")

}

func (f MockPage1BodyFragment) Execute(w io.Writer, data map[string]interface{}, executeNestedFragment func(nestedFragmentName string) error) error {
w.Write([]byte("<page1-body-main>\n"))
if err := executeNestedFragment("page2-a"); err != nil {
Expand All @@ -86,7 +102,7 @@ func Test_ContentMerge_MainFragmentDoesNotExist(t *testing.T) {
cm := NewContentMerge(nil)
_, err := cm.GetHtml()
a.Error(err)
a.Equal("Fragment does not exist: ", err.Error())
a.Equal("Fragment does not exist: . Existing fragments: ", err.Error())
}

type closedWriterMock struct {
Expand Down
2 changes: 1 addition & 1 deletion composition/http_content_loader.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package composition

import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"errors"
"fmt"
"github.com/tarent/lib-compose/logging"
"github.com/tarent/lib-servicediscovery/servicediscovery"
"net/url"
Expand Down
1 change: 1 addition & 0 deletions logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func access(r *http.Request, start time.Time, statusCode int, err error) *logrus
"method": r.Method,
"proto": r.Proto,
"duration": time.Since(start).Nanoseconds() / 1000000,
"User_Agent": r.Header.Get("User-Agent"),
}

if statusCode != 0 {
Expand Down
5 changes: 5 additions & 0 deletions logging/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type logReccord struct {
Error string `json:"error"`
Message string `json:"message"`
Level string `json:"level"`
UserAgent string `json:"User_Agent"`
}

func Test_Logger_Set(t *testing.T) {
Expand Down Expand Up @@ -122,11 +123,14 @@ func Test_Logger_Access(t *testing.T) {
AccessLogCookiesBlacklist = []string{"ignore", "user_id"}
UserCorrelationCookie = "user_id"

// Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36

// and a request
r, _ := http.NewRequest("GET", "http://www.example.org/foo?q=bar", nil)
r.Header = http.Header{
CorrelationIdHeader: {"correlation-123"},
"Cookie": {"user_id=user-id-xyz; ignore=me; foo=bar;"},
"User-Agent": {"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36"},
}
r.RemoteAddr = "127.0.0.1"

Expand All @@ -153,6 +157,7 @@ func Test_Logger_Access(t *testing.T) {
a.Equal("access", data.Type)
a.Equal("/foo?q=bar", data.URL)
a.Equal("user-id-xyz", data.UserCorrelationId)
a.Equal("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36", data.UserAgent)
}

func Test_Logger_Access_ErrorCases(t *testing.T) {
Expand Down

0 comments on commit a50a734

Please sign in to comment.