Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions client/lessons.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"encoding/json"
"fmt"
"strings"
)

type Lesson struct {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -362,15 +362,15 @@ func RenderRun(
func RenderSubmission(
data api.CLIData,
results []api.CLIStepResult,
failure *api.StructuredErrCLI,
failure *api.VerificationResultStructuredErrCLI,
) {
renderer(data, results, failure, true)
}

func renderer(
data api.CLIData,
results []api.CLIStepResult,
failure *api.StructuredErrCLI,
failure *api.VerificationResultStructuredErrCLI,
isSubmit bool,
) {
var wg sync.WaitGroup
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down