-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pipeline/task.bridge: do not cache invalid external adapter response …
…objects. (#11725) * pipeline/task.bridge: check for external adapter status in response object * pipeline/task.bridge: external adapter response object tests * pipeline: move ea specific utilities into an internal package and add unit tests * pipeline/task.bridge: rebase fix for commonconfig * Update core/services/pipeline/internal/eautils/eautils.go Co-authored-by: Jordan Krage <[email protected]> * pipeline/task.bridge: ea status error can also be an object --------- Co-authored-by: Jordan Krage <[email protected]>
- Loading branch information
Showing
5 changed files
with
242 additions
and
13 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package eautils | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
type AdapterStatus struct { | ||
ErrorMessage *string `json:"errorMessage"` | ||
Error any `json:"error"` | ||
StatusCode *int `json:"statusCode"` | ||
ProviderStatusCode *int `json:"providerStatusCode"` | ||
} | ||
|
||
func BestEffortExtractEAStatus(responseBytes []byte) (code int, ok bool) { | ||
var status AdapterStatus | ||
err := json.Unmarshal(responseBytes, &status) | ||
if err != nil { | ||
return 0, false | ||
} | ||
|
||
if status.StatusCode == nil { | ||
return 0, false | ||
} | ||
|
||
if *status.StatusCode != http.StatusOK { | ||
return *status.StatusCode, true | ||
} | ||
|
||
if status.ProviderStatusCode != nil && *status.ProviderStatusCode != http.StatusOK { | ||
return *status.ProviderStatusCode, true | ||
} | ||
|
||
if status.Error != nil { | ||
return http.StatusInternalServerError, true | ||
} | ||
|
||
return *status.StatusCode, true | ||
} |
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,61 @@ | ||
package eautils | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBestEffortExtractEAStatus(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
arg []byte | ||
expectCode int | ||
expectOk bool | ||
}{ | ||
{ | ||
name: "invalid object", | ||
arg: []byte(`{"error": "invalid json object" `), | ||
expectCode: 0, | ||
expectOk: false, | ||
}, | ||
{ | ||
name: "no status code in object", | ||
arg: []byte(`{}`), | ||
expectCode: 0, | ||
expectOk: false, | ||
}, | ||
{ | ||
name: "invalid status code", | ||
arg: []byte(`{"statusCode":400}`), | ||
expectCode: http.StatusBadRequest, | ||
expectOk: true, | ||
}, | ||
{ | ||
name: "invalid provider status code", | ||
arg: []byte(`{"statusCode":200, "providerStatusCode":500}`), | ||
expectCode: http.StatusInternalServerError, | ||
expectOk: true, | ||
}, | ||
{ | ||
name: "valid statuses with error message", | ||
arg: []byte(`{"statusCode":200, "providerStatusCode":200, "error": "unexpected error"}`), | ||
expectCode: http.StatusInternalServerError, | ||
expectOk: true, | ||
}, | ||
{ | ||
name: "valid status code", | ||
arg: []byte(`{"statusCode":200}`), | ||
expectCode: http.StatusOK, | ||
expectOk: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
code, ok := BestEffortExtractEAStatus(tt.arg) | ||
assert.Equal(t, tt.expectCode, code) | ||
assert.Equal(t, tt.expectOk, ok) | ||
}) | ||
} | ||
} |
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
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