From 54770ab0a24f8bf06f21f3bdbd34e1882490afbf Mon Sep 17 00:00:00 2001 From: wagslane Date: Sun, 10 Aug 2025 17:21:08 -0600 Subject: [PATCH] update to handle new response format --- client/lessons.go | 33 ++++++++++++++++++++++++++++----- render/render.go | 12 ++++++------ 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/client/lessons.go b/client/lessons.go index e6593e3..ce158f1 100644 --- a/client/lessons.go +++ b/client/lessons.go @@ -3,6 +3,7 @@ package api import ( "encoding/json" "fmt" + "strings" ) type Lesson struct { @@ -146,13 +147,21 @@ type lessonSubmissionCLI struct { CLIResults []CLIStepResult } -type StructuredErrCLI struct { +type verificationResult struct { + ResultSlug string + // user friendly message to put in the toast + ResultMessage string + // only present if the lesson is an CLI type + StructuredErrCLI *VerificationResultStructuredErrCLI +} + +type VerificationResultStructuredErrCLI struct { ErrorMessage string `json:"Error"` FailedStepIndex int `json:"FailedStepIndex"` FailedTestIndex int `json:"FailedTestIndex"` } -func SubmitCLILesson(uuid string, results []CLIStepResult) (*StructuredErrCLI, error) { +func SubmitCLILesson(uuid string, results []CLIStepResult) (*VerificationResultStructuredErrCLI, error) { bytes, err := json.Marshal(lessonSubmissionCLI{CLIResults: results}) if err != nil { return nil, err @@ -168,10 +177,24 @@ func SubmitCLILesson(uuid string, results []CLIStepResult) (*StructuredErrCLI, e if code != 200 { return nil, fmt.Errorf("failed to submit CLI lesson (code: %v): %s", code, string(resp)) } - var failure StructuredErrCLI + + if strings.Contains(string(resp), `"StructuredErrCLI"`) { + result := verificationResult{} + err = json.Unmarshal(resp, &result) + if err != nil { + return nil, err + } + return result.StructuredErrCLI, nil + } + // TODO: delete this, it's for backwards compatibility + // it used to be a top-object + var failure VerificationResultStructuredErrCLI err = json.Unmarshal(resp, &failure) - if err != nil || failure.ErrorMessage == "" { - // this is ok - it means we had success + if err != nil { + return nil, err + } + if failure.ErrorMessage == "" { + // no structured error, return nil, this is success return nil, nil } return &failure, nil diff --git a/render/render.go b/render/render.go index b7b9f0f..4f2733d 100644 --- a/render/render.go +++ b/render/render.go @@ -93,7 +93,7 @@ func renderTest(text string, spinner string, isFinished bool, isSubmit *bool, pa } type doneStepMsg struct { - failure *api.StructuredErrCLI + failure *api.VerificationResultStructuredErrCLI } type startStepMsg struct { @@ -121,7 +121,7 @@ type stepModel struct { type rootModel struct { steps []stepModel spinner spinner.Model - failure *api.StructuredErrCLI + failure *api.VerificationResultStructuredErrCLI isSubmit bool success bool finalized bool @@ -362,7 +362,7 @@ func RenderRun( func RenderSubmission( data api.CLIData, results []api.CLIStepResult, - failure *api.StructuredErrCLI, + failure *api.VerificationResultStructuredErrCLI, ) { renderer(data, results, failure, true) } @@ -370,7 +370,7 @@ func RenderSubmission( func renderer( data api.CLIData, results []api.CLIStepResult, - failure *api.StructuredErrCLI, + failure *api.VerificationResultStructuredErrCLI, isSubmit bool, ) { var wg sync.WaitGroup @@ -417,7 +417,7 @@ func renderer( func renderCLICommand( cmd api.CLIStepCLICommand, result api.CLICommandResult, - failure *api.StructuredErrCLI, + failure *api.VerificationResultStructuredErrCLI, isSubmit bool, ch chan tea.Msg, index int, @@ -485,7 +485,7 @@ func renderCLICommand( func renderHTTPRequest( req api.CLIStepHTTPRequest, result api.HTTPRequestResult, - failure *api.StructuredErrCLI, + failure *api.VerificationResultStructuredErrCLI, isSubmit bool, baseURLDefault string, ch chan tea.Msg,