Skip to content

Commit fdd5107

Browse files
authored
update to handle new response format (#101)
1 parent 7422048 commit fdd5107

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

client/lessons.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package api
33
import (
44
"encoding/json"
55
"fmt"
6+
"strings"
67
)
78

89
type Lesson struct {
@@ -146,13 +147,21 @@ type lessonSubmissionCLI struct {
146147
CLIResults []CLIStepResult
147148
}
148149

149-
type StructuredErrCLI struct {
150+
type verificationResult struct {
151+
ResultSlug string
152+
// user friendly message to put in the toast
153+
ResultMessage string
154+
// only present if the lesson is an CLI type
155+
StructuredErrCLI *VerificationResultStructuredErrCLI
156+
}
157+
158+
type VerificationResultStructuredErrCLI struct {
150159
ErrorMessage string `json:"Error"`
151160
FailedStepIndex int `json:"FailedStepIndex"`
152161
FailedTestIndex int `json:"FailedTestIndex"`
153162
}
154163

155-
func SubmitCLILesson(uuid string, results []CLIStepResult) (*StructuredErrCLI, error) {
164+
func SubmitCLILesson(uuid string, results []CLIStepResult) (*VerificationResultStructuredErrCLI, error) {
156165
bytes, err := json.Marshal(lessonSubmissionCLI{CLIResults: results})
157166
if err != nil {
158167
return nil, err
@@ -168,10 +177,24 @@ func SubmitCLILesson(uuid string, results []CLIStepResult) (*StructuredErrCLI, e
168177
if code != 200 {
169178
return nil, fmt.Errorf("failed to submit CLI lesson (code: %v): %s", code, string(resp))
170179
}
171-
var failure StructuredErrCLI
180+
181+
if strings.Contains(string(resp), `"StructuredErrCLI"`) {
182+
result := verificationResult{}
183+
err = json.Unmarshal(resp, &result)
184+
if err != nil {
185+
return nil, err
186+
}
187+
return result.StructuredErrCLI, nil
188+
}
189+
// TODO: delete this, it's for backwards compatibility
190+
// it used to be a top-object
191+
var failure VerificationResultStructuredErrCLI
172192
err = json.Unmarshal(resp, &failure)
173-
if err != nil || failure.ErrorMessage == "" {
174-
// this is ok - it means we had success
193+
if err != nil {
194+
return nil, err
195+
}
196+
if failure.ErrorMessage == "" {
197+
// no structured error, return nil, this is success
175198
return nil, nil
176199
}
177200
return &failure, nil

render/render.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func renderTest(text string, spinner string, isFinished bool, isSubmit *bool, pa
9393
}
9494

9595
type doneStepMsg struct {
96-
failure *api.StructuredErrCLI
96+
failure *api.VerificationResultStructuredErrCLI
9797
}
9898

9999
type startStepMsg struct {
@@ -121,7 +121,7 @@ type stepModel struct {
121121
type rootModel struct {
122122
steps []stepModel
123123
spinner spinner.Model
124-
failure *api.StructuredErrCLI
124+
failure *api.VerificationResultStructuredErrCLI
125125
isSubmit bool
126126
success bool
127127
finalized bool
@@ -362,15 +362,15 @@ func RenderRun(
362362
func RenderSubmission(
363363
data api.CLIData,
364364
results []api.CLIStepResult,
365-
failure *api.StructuredErrCLI,
365+
failure *api.VerificationResultStructuredErrCLI,
366366
) {
367367
renderer(data, results, failure, true)
368368
}
369369

370370
func renderer(
371371
data api.CLIData,
372372
results []api.CLIStepResult,
373-
failure *api.StructuredErrCLI,
373+
failure *api.VerificationResultStructuredErrCLI,
374374
isSubmit bool,
375375
) {
376376
var wg sync.WaitGroup
@@ -417,7 +417,7 @@ func renderer(
417417
func renderCLICommand(
418418
cmd api.CLIStepCLICommand,
419419
result api.CLICommandResult,
420-
failure *api.StructuredErrCLI,
420+
failure *api.VerificationResultStructuredErrCLI,
421421
isSubmit bool,
422422
ch chan tea.Msg,
423423
index int,
@@ -485,7 +485,7 @@ func renderCLICommand(
485485
func renderHTTPRequest(
486486
req api.CLIStepHTTPRequest,
487487
result api.HTTPRequestResult,
488-
failure *api.StructuredErrCLI,
488+
failure *api.VerificationResultStructuredErrCLI,
489489
isSubmit bool,
490490
baseURLDefault string,
491491
ch chan tea.Msg,

0 commit comments

Comments
 (0)