-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
report error to coralogix initial push
- Loading branch information
1 parent
74da120
commit 01e9766
Showing
4 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package tests | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient" | ||
"github.com/jfrog/jfrog-client-go/xsc/services" | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
const errorMessageContentForTest = "THIS IS NOT A REAL ERROR! This Error is posted as part of TestXscSendLogErrorEvent test" | ||
|
||
func TestXscSendLogErrorEvent(t *testing.T) { | ||
initXscTest(t) | ||
mockServer, logErrorService := createXscMockServer(t) | ||
defer mockServer.Close() | ||
|
||
event := &services.ExternalErrorLog{ | ||
Log_level: "error", | ||
Source: "cli", | ||
Message: errorMessageContentForTest, | ||
} | ||
|
||
assert.NoError(t, logErrorService.SendLogErrorEvent(event)) | ||
} | ||
|
||
func createXscMockServer(t *testing.T) (mockServer *httptest.Server, logErrorService *services.LogErrorEventService) { | ||
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.RequestURI == "/xsc/api/v1/event/logMessage" && r.Method == http.MethodPost { | ||
var reqBody services.ExternalErrorLog | ||
decoder := json.NewDecoder(r.Body) | ||
err := decoder.Decode(&reqBody) | ||
assert.NoError(t, err, "Invalid JSON request body") | ||
if err != nil { | ||
return | ||
} | ||
|
||
assert.Equal(t, "error", reqBody.Log_level) | ||
assert.Equal(t, "cli", reqBody.Source) | ||
assert.Equal(t, errorMessageContentForTest, reqBody.Message) | ||
w.WriteHeader(http.StatusCreated) | ||
return | ||
} | ||
assert.Fail(t, "received an unexpected request") | ||
})) | ||
|
||
xscDetails := GetXscDetails() | ||
xscDetails.SetUrl(mockServer.URL + "/xsc") | ||
|
||
client, err := jfroghttpclient.JfrogClientBuilder().Build() | ||
assert.NoError(t, err) | ||
|
||
logErrorService = services.NewLogErrorEventService(client) | ||
logErrorService.XscDetails = xscDetails | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package services | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/jfrog/jfrog-client-go/auth" | ||
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient" | ||
"github.com/jfrog/jfrog-client-go/utils" | ||
"github.com/jfrog/jfrog-client-go/utils/errorutils" | ||
"net/http" | ||
) | ||
|
||
const postLogErrorAPI = "api/v1/event/logMessage" | ||
|
||
type LogErrorEventService struct { | ||
client *jfroghttpclient.JfrogHttpClient | ||
XscDetails auth.ServiceDetails | ||
} | ||
|
||
type ExternalErrorLog struct { | ||
Log_level string `json:"log_level"` | ||
Source string `json:"source"` | ||
Message string `json:"message"` | ||
} | ||
|
||
func NewLogErrorEventService(client *jfroghttpclient.JfrogHttpClient) *LogErrorEventService { | ||
return &LogErrorEventService{client: client} | ||
} | ||
|
||
func (les *LogErrorEventService) SendLogErrorEvent(errorLog *ExternalErrorLog) error { | ||
httpClientDetails := les.XscDetails.CreateHttpClientDetails() | ||
requestContent, err := json.Marshal(errorLog) | ||
if err != nil { | ||
return fmt.Errorf("failed to convert POST request body's struct into JSON: %q", err) | ||
} | ||
url := utils.AddTrailingSlashIfNeeded(les.XscDetails.GetUrl()) + postLogErrorAPI | ||
response, body, err := les.client.SendPost(url, requestContent, &httpClientDetails) | ||
if err != nil { | ||
return fmt.Errorf("failed to send POST query to '%s': %s", url, err.Error()) | ||
} | ||
return errorutils.CheckResponseStatusWithBody(response, body, http.StatusCreated) | ||
} |