on main help sign in
chore: add custom error and message for hyper unauthorized (#2577)
1b7a457Andrey Nering
internal/agent/agent.go+2
@@ -529,6 +529,8 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
529 currentAssistant.AddFinish(message.FinishReasonCanceled, "User canceled request", "")
529 currentAssistant.AddFinish(message.FinishReasonCanceled, "User canceled request", "")
530 } else if isPermissionErr {
530 } else if isPermissionErr {
531 currentAssistant.AddFinish(message.FinishReasonPermissionDenied, "User denied permission", "")
531 currentAssistant.AddFinish(message.FinishReasonPermissionDenied, "User denied permission", "")
532+ } else if errors.Is(err, hyper.ErrUnauthorized) {
533+ currentAssistant.AddFinish(message.FinishReasonError, "Unauthorized", `Authentication with Hyper failed. Please run "crush auth" to re-authenticate.`)
532 } else if errors.Is(err, hyper.ErrNoCredits) {
534 } else if errors.Is(err, hyper.ErrNoCredits) {
533 url := hyper.BaseURL()
535 url := hyper.BaseURL()
534 link := linkStyle.Hyperlink(url, "id=hyper").Render(url)
536 link := linkStyle.Hyperlink(url, "id=hyper").Render(url)
internal/agent/hyper/provider.go+5−2
@@ -69,7 +69,10 @@ var BaseURL = sync.OnceValue(func() string {
69 return cmp.Or(os.Getenv("HYPER_URL"), defaultBaseURL)
69 return cmp.Or(os.Getenv("HYPER_URL"), defaultBaseURL)
70})
70})
71
71
72+var (
72−var ErrNoCredits = errors.New("you're out of credits")
73+ ErrNoCredits = errors.New("you're out of credits")
74+ ErrUnauthorized = errors.New("unauthorized")
75+)
73
76
74type options struct {
77type options struct {
75 baseURL string
78 baseURL string
@@ -183,7 +186,7 @@ func (m *languageModel) Stream(ctx context.Context, call fantasy.Call) (fantasy.
183 return nil, toProviderError(resp, retryAfter(resp))
186 return nil, toProviderError(resp, retryAfter(resp))
184 case http.StatusUnauthorized:
187 case http.StatusUnauthorized:
185 _ = resp.Body.Close()
188 _ = resp.Body.Close()
186− return nil, toProviderError(resp, "")
189+ return nil, ErrUnauthorized
187 case http.StatusPaymentRequired:
190 case http.StatusPaymentRequired:
188 _ = resp.Body.Close()
191 _ = resp.Body.Close()
189 return nil, ErrNoCredits
192 return nil, ErrNoCredits
internal/ui/model/ui.go+1−1
@@ -2964,7 +2964,7 @@ func (m *UI) sendMessage(content string, attachments ...message.Attachment) tea.
2964 }
2964 }
2965 return util.InfoMsg{
2965 return util.InfoMsg{
2966 Type: util.InfoTypeError,
2966 Type: util.InfoTypeError,
2967− Msg: fmt.Sprintf("Failed to run agent: %v", err),
2967+ Msg: fmt.Sprintf("%v", err),
2968 }
2968 }
2969 }
2969 }
2970 return nil
2970 return nil