osbuildexecutor: show full osbuild exector on json decode errors
This is a short term workaround to get better visibility why the osbuild executor sometimes sends non-json data. When reading the result from the executor the entire output is now read and if the json parsing goes wrong it will use the entire body in the error message for better debug visibility.
This commit is contained in:
parent
0a68fe3005
commit
86b1143923
2 changed files with 22 additions and 4 deletions
|
|
@ -149,14 +149,14 @@ func handleBuild(inputArchive, host string) (*osbuild.Result, error) {
|
|||
|
||||
var osbuildResult osbuild.Result
|
||||
|
||||
err = json.NewDecoder(resp.Body).Decode(&osbuildResult)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Unable to decode response body into osbuild result: %w", err)
|
||||
return nil, fmt.Errorf("Unable to read response body: %w", err)
|
||||
}
|
||||
|
||||
_, err = io.ReadAll(resp.Body)
|
||||
err = json.Unmarshal(body, &osbuildResult)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Unable to wait for executor to close connection: %w", err)
|
||||
return nil, fmt.Errorf("Unable to decode response body %q into osbuild result: %w", body, err)
|
||||
}
|
||||
|
||||
return &osbuildResult, nil
|
||||
|
|
|
|||
|
|
@ -91,6 +91,24 @@ func TestHandleBuild(t *testing.T) {
|
|||
require.True(t, osbuildResult.Success)
|
||||
}
|
||||
|
||||
func TestHandleBuildNoJSON(t *testing.T) {
|
||||
buildServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, err := io.ReadAll(r.Body)
|
||||
require.NoError(t, err)
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
_, err = w.Write([]byte("bad non-json text"))
|
||||
require.NoError(t, err)
|
||||
}))
|
||||
|
||||
cacheDir := t.TempDir()
|
||||
inputArchive := filepath.Join(cacheDir, "test.tar")
|
||||
require.NoError(t, os.WriteFile(inputArchive, []byte("test"), 0600))
|
||||
|
||||
_, err := osbuildexecutor.HandleBuild(inputArchive, buildServer.URL)
|
||||
require.ErrorContains(t, err, `Unable to decode response body "bad non-json text" into osbuild result:`)
|
||||
}
|
||||
|
||||
func TestHandleOutputArchive(t *testing.T) {
|
||||
serverDir := t.TempDir()
|
||||
serverOutputDir := filepath.Join(serverDir, "output")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue