Skip to content

Commit

Permalink
fix: better error handling of run for studio (#912)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfbx9da4 authored Sep 4, 2024
1 parent 86ab07f commit 8e7cf7b
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 41 deletions.
15 changes: 0 additions & 15 deletions .vscode/launch.json

This file was deleted.

23 changes: 23 additions & 0 deletions .vscode/launch.template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"configurations": [
{
"name": "speakeasy run",
"type": "go",
"request": "launch",
"program": "main.go",
"cwd": "/tmp/studio-test-6840674974721771490",
"args": ["run", "--launch-studio"]
},
{
"name": "Debug CLI headless",
"type": "go",
"debugAdapter": "dlv-dap",
"request": "attach",
"mode": "remote",
"remotePath": "${workspaceFolder}",
"port": 2345,
"host": "127.0.0.1",
"preLaunchTask": "Run headless dlv"
}
]
}
23 changes: 15 additions & 8 deletions internal/run/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ func (w *Workflow) RunSource(ctx context.Context, parentStep *workflowTracking.W
Source: sourceID,
Diagnosis: suggestions.Diagnosis{},
}
defer func() {
w.OnSourceResult(sourceRes)
}()

rulesetToUse := "speakeasy-generation"
if source.Ruleset != nil {
Expand Down Expand Up @@ -226,6 +229,7 @@ func (w *Workflow) RunSource(ctx context.Context, parentStep *workflowTracking.W
}

currentDocument = overlayLocation
overlayStep.Succeed()
}

sourceRes.OutputPath = currentDocument
Expand All @@ -240,12 +244,6 @@ func (w *Workflow) RunSource(ctx context.Context, parentStep *workflowTracking.W
}
}

sourceRes.Diagnosis, err = suggest.Diagnose(ctx, currentDocument)
if err != nil {
w.OnSourceResult(sourceRes)
return "", sourceRes, err
}

// If the source has a previous tracked revision, compute changes against it
if w.lockfileOld != nil && !w.SkipChangeReport {
if targetLockOld, ok := w.lockfileOld.Targets[targetID]; ok && !utils.IsZeroTelemetryOrganization(ctx) {
Expand All @@ -256,6 +254,7 @@ func (w *Workflow) RunSource(ctx context.Context, parentStep *workflowTracking.W
}
}
}

if sourceRes.ChangeReport == nil {
// If we failed to compute changes, always generate the SDK
_ = versioning.AddVersionReport(ctx, versioning.VersionReport{
Expand All @@ -267,15 +266,21 @@ func (w *Workflow) RunSource(ctx context.Context, parentStep *workflowTracking.W

if !w.SkipLinting {
sourceRes.LintResult, err = w.validateDocument(ctx, rootStep, sourceID, currentDocument, rulesetToUse, w.ProjectDir)
w.OnSourceResult(sourceRes)
if err != nil {
return "", sourceRes, &LintingError{Err: err, Document: currentDocument}
}
}

step := rootStep.NewSubstep("Diagnosing OpenAPI")
sourceRes.Diagnosis, err = suggest.Diagnose(ctx, currentDocument)
if err != nil {
step.Fail()
return "", sourceRes, err
}
step.Succeed()

rootStep.SucceedWorkflow()

w.OnSourceResult(sourceRes)
return currentDocument, sourceRes, nil
}

Expand Down Expand Up @@ -367,6 +372,8 @@ func (w *Workflow) validateDocument(ctx context.Context, parentStep *workflowTra

w.validatedDocuments = append(w.validatedDocuments, schemaPath)

step.SucceedWorkflow()

return res, err
}

Expand Down
20 changes: 14 additions & 6 deletions internal/studio/launchStudio.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"os/signal"
"syscall"
"time"

"github.com/speakeasy-api/speakeasy-core/auth"

Expand Down Expand Up @@ -107,14 +108,20 @@ func authMiddleware(secret string, next http.Handler) http.Handler {

func handler(h func(context.Context, http.ResponseWriter, *http.Request) error) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
id := generateRequestID()
log.From(r.Context()).Info("handling request", zap.String("method", r.Method), zap.String("path", r.URL.Path), zap.String("request_id", id))
method := fmt.Sprintf("%-6s", r.Method) // Fixed width 6 characters
path := fmt.Sprintf("%-21s", r.URL.Path) // Fixed width 21 characters
base := fmt.Sprintf("%s %s %s", id, method, path)
log.From(r.Context()).Info(fmt.Sprintf("%s started", base))
ctx := r.Context()
if err := h(ctx, w, r); err != nil {
log.From(ctx).Error("error handling request", zap.String("method", r.Method), zap.String("path", r.URL.Path), zap.String("request_id", id), zap.Error(err))
log.From(ctx).Error(fmt.Sprintf("%s failed: %v", base, err))
respondJSONError(ctx, w, err)
return
}
log.From(ctx).Info("request handled", zap.String("method", r.Method), zap.String("path", r.URL.Path), zap.String("request_id", id))
duration := time.Since(start)
log.From(ctx).Info(fmt.Sprintf("%s completed in %s", base, duration))
}
}

Expand Down Expand Up @@ -198,8 +205,9 @@ func respondJSONError(ctx context.Context, w http.ResponseWriter, err error) {
}
}

var counter int

func generateRequestID() string {
bytes := make([]byte, 4)
rand.Read(bytes) // Generate random bytes
return hex.EncodeToString(bytes)
counter++
return fmt.Sprintf("%03d", counter)
}
6 changes: 3 additions & 3 deletions internal/studio/sdk/.speakeasy/workflow.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions internal/studio/studioHandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,14 @@ func (h *StudioHandlers) convertLastRunResult(ctx context.Context, step string)
WorkingDirectory: h.WorkflowRunner.ProjectDir,
Step: step,
IsPartial: step != "end",
Took: h.WorkflowRunner.Duration.Milliseconds(),
}

ret.Took = h.WorkflowRunner.Duration.Milliseconds()
wf, err := convertWorkflowToComponentsWorkflow(*h.WorkflowRunner.GetWorkflowFile())
if err != nil {
return &ret, fmt.Errorf("error converting workflow to components.Workflow: %w", err)
}
ret.Workflow = wf

if h.WorkflowRunner.Error != nil {
errStr := h.WorkflowRunner.Error.Error()
Expand Down Expand Up @@ -341,12 +346,6 @@ func (h *StudioHandlers) convertLastRunResult(ctx context.Context, step string)
ret.SourceResult = *sourceResponse
}

wf, err := convertWorkflowToComponentsWorkflow(*h.WorkflowRunner.GetWorkflowFile())
if err != nil {
return &ret, fmt.Errorf("error converting workflow to components.Workflow: %w", err)
}
ret.Workflow = wf

return &ret, nil
}

Expand Down
5 changes: 3 additions & 2 deletions internal/suggest/suggest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package suggest
import (
"context"
"fmt"
"github.com/speakeasy-api/speakeasy-core/suggestions"
"gopkg.in/yaml.v3"
"io"
"io/ioutil"
"net/http"
"strings"
"time"

"github.com/speakeasy-api/speakeasy-core/suggestions"
"gopkg.in/yaml.v3"

"github.com/charmbracelet/lipgloss"
"github.com/speakeasy-api/openapi-overlay/pkg/overlay"
"github.com/speakeasy-api/speakeasy-client-sdk-go/v3/pkg/models/shared"
Expand Down

0 comments on commit 8e7cf7b

Please sign in to comment.