Skip to content

Commit

Permalink
fix: avoid panic when sourcemap doc does not contain hit (#4619) (#4624)
Browse files Browse the repository at this point in the history
closes #4601
  • Loading branch information
simitt authored Jan 14, 2021
1 parent ed80912 commit 859dfc2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion sourcemap/es_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func parse(body io.ReadCloser, name, version, path string, logger *logp.Logger)
return "", err
}
hits := esSourcemapResponse.Hits.Total.Value
if hits == 0 {
if hits == 0 || len(esSourcemapResponse.Hits.Hits) == 0 {
return emptyResult, nil
}

Expand Down
5 changes: 3 additions & 2 deletions sourcemap/es_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ func Test_esFetcher_fetch(t *testing.T) {
client elasticsearch.Client
filePath string
}{
"no sourcemap found": {client: test.ESClientWithSourcemapNotFound(t)},
"valid sourcemap found": {client: test.ESClientWithValidSourcemap(t), filePath: "bundle.js"},
"no sourcemap found": {client: test.ESClientWithSourcemapNotFound(t)},
"sourcemap indicated but not found": {client: test.ESClientWithSourcemapIndicatedNotFound(t)},
"valid sourcemap found": {client: test.ESClientWithValidSourcemap(t), filePath: "bundle.js"},
} {
t.Run(name, func(t *testing.T) {
sourcemapStr, err := testESStore(tc.client).fetch(context.Background(), "abc", "1.0", "/tmp")
Expand Down
15 changes: 15 additions & 0 deletions sourcemap/test/es_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ func ESClientWithSourcemapNotFound(t *testing.T) elasticsearch.Client {
return client
}

// ESClientWithSourcemapIndicatedNotFound returns an elasticsearch client that will always return a result indicating
// a sourcemap exists but it actually doesn't contain it. This is an edge case that usually shouldn't happen.
func ESClientWithSourcemapIndicatedNotFound(t *testing.T) elasticsearch.Client {
client, err := estest.NewElasticsearchClient(estest.NewTransport(t, http.StatusOK, sourcemapIndicatedNotFoundFromES()))
require.NoError(t, err)
return client
}

func validSourcemapFromES() map[string]interface{} {
return map[string]interface{}{
"hits": map[string]interface{}{
Expand All @@ -123,6 +131,13 @@ func sourcemapNotFoundFromES() map[string]interface{} {
"total": map[string]interface{}{"value": 0}}}
}

func sourcemapIndicatedNotFoundFromES() map[string]interface{} {
return map[string]interface{}{
"hits": map[string]interface{}{
"total": map[string]interface{}{"value": 1},
"hits": []map[string]interface{}{}}}
}

func invalidSourcemapFromES() map[string]interface{} {
return map[string]interface{}{
"hits": map[string]interface{}{
Expand Down

0 comments on commit 859dfc2

Please sign in to comment.