Skip to content

Commit

Permalink
Merge pull request #2 from marconi1992/bug/handling-result-error
Browse files Browse the repository at this point in the history
Handler job error and add html comment in response
  • Loading branch information
marconi1992 authored May 26, 2019
2 parents ddf750a + 1700be0 commit 3ada5a9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.json
20 changes: 17 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ import (
"github.com/PuerkitoBio/goquery"
)

// ViewJobError is an error happened during and after a view is requesting.
type ViewJobError struct {
Name string `json:"name"`
Message string `json:"message"`
}

type HypernovaResult struct {
Success bool
Html string
Name string
Error ViewJobError
}

type HypernovaResponse struct {
Expand Down Expand Up @@ -77,6 +84,10 @@ func modifyBody(html string) string {
batch[uuid]["data"] = data
})

if len(batch) == 0 {
return html
}

b, encodeErr := json.Marshal(batch)

if encodeErr != nil {
Expand All @@ -88,7 +99,8 @@ func modifyBody(html string) string {
resp, reqErr := http.Post(os.Getenv("HYPERNOVA_BATCH"), "application/json", strings.NewReader(payload))

if reqErr != nil {
log.Fatal(reqErr)
log.Println(reqErr)
return html
}

defer resp.Body.Close()
Expand All @@ -104,14 +116,16 @@ func modifyBody(html string) string {
json.Unmarshal(body, &hypernovaResponse)

for uuid, result := range hypernovaResponse.Results {
divQuery := createQuery("div", uuid, result.Name)

if !result.Success {
break
doc.Find(divQuery).PrependHtml("<!-- Proxy Error: " + result.Error.Name + " -->")
continue
}

scriptQuery := createQuery("script", uuid, result.Name)
doc.Find(scriptQuery).Remove()

divQuery := createQuery("div", uuid, result.Name)
doc.Find(divQuery).ReplaceWithHtml(result.Html)
}

Expand Down

0 comments on commit 3ada5a9

Please sign in to comment.