Skip to content

Commit 846bbf6

Browse files
authored
Merge pull request #53 from dispatchrun/error-tweaks
run: improve error messages when local application can't be reached or misbehaves
2 parents 8219e1f + 4fa9e07 commit 846bbf6

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

cli/run.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,10 +459,11 @@ func invoke(ctx context.Context, client *http.Client, url, requestID string, bri
459459
endpointRes, err := client.Do(endpointReq)
460460
now := time.Now()
461461
if err != nil {
462+
err = fmt.Errorf("can't connect to %s: %v (check that -e,--endpoint is correct)", LocalEndpoint, tidyErr(err))
462463
if observer != nil {
463464
observer.ObserveResponse(now, &runRequest, err, nil, nil)
464465
}
465-
return fmt.Errorf("failed to contact local application endpoint (%s): %v. Please check that -e,--endpoint is correct.", LocalEndpoint, err)
466+
return err
466467
}
467468

468469
// Buffer the response body in memory.
@@ -473,10 +474,11 @@ func invoke(ctx context.Context, client *http.Client, url, requestID string, bri
473474
_, err = io.Copy(endpointResBody, endpointRes.Body)
474475
endpointRes.Body.Close()
475476
if err != nil {
477+
err = fmt.Errorf("read error from %s: %v", LocalEndpoint, tidyErr(err))
476478
if observer != nil {
477479
observer.ObserveResponse(now, &runRequest, err, endpointRes, nil)
478480
}
479-
return fmt.Errorf("failed to read response from local application endpoint (%s): %v", LocalEndpoint, err)
481+
return err
480482
}
481483
endpointRes.Body = io.NopCloser(endpointResBody)
482484
endpointRes.ContentLength = int64(endpointResBody.Len())
@@ -485,10 +487,11 @@ func invoke(ctx context.Context, client *http.Client, url, requestID string, bri
485487
if endpointRes.StatusCode == http.StatusOK && endpointRes.Header.Get("Content-Type") == "application/proto" {
486488
var runResponse sdkv1.RunResponse
487489
if err := proto.Unmarshal(endpointResBody.Bytes(), &runResponse); err != nil {
490+
err = fmt.Errorf("invalid response from %s: %v", LocalEndpoint, tidyErr(err))
488491
if observer != nil {
489492
observer.ObserveResponse(now, &runRequest, err, endpointRes, nil)
490493
}
491-
return fmt.Errorf("invalid response from local application endpoint (%s): %v", LocalEndpoint, err)
494+
return err
492495
}
493496
switch runResponse.Status {
494497
case sdkv1.Status_STATUS_OK:
@@ -638,3 +641,18 @@ func randomSessionID() string {
638641
i.SetBytes(b[:])
639642
return i.Text(62) // base62
640643
}
644+
645+
var (
646+
errConnectionRefused = errors.New("connection refused")
647+
)
648+
649+
func tidyErr(err error) error {
650+
var errno syscall.Errno
651+
if errors.As(err, &errno) {
652+
switch errno {
653+
case syscall.ECONNREFUSED:
654+
return errConnectionRefused
655+
}
656+
}
657+
return err
658+
}

cli/tui.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,9 +605,13 @@ func (t *TUI) detailView(id DispatchID) string {
605605
}
606606
}
607607
} else if c := rt.response.httpStatus; c != 0 {
608-
add("Error", errorStyle.Render(fmt.Sprintf("%d %s", c, http.StatusText(c))))
608+
style := errorStyle
609+
if !terminalHTTPStatusCode(c) {
610+
style = retryStyle
611+
}
612+
add("Error", style.Render(fmt.Sprintf("%d %s", c, http.StatusText(c))))
609613
} else if rt.response.err != nil {
610-
add("Error", errorStyle.Render(rt.response.err.Error()))
614+
add("Error", retryStyle.Render(rt.response.err.Error()))
611615
}
612616

613617
latency := rt.response.ts.Sub(rt.request.ts)

0 commit comments

Comments
 (0)