Skip to content

Commit e41ff78

Browse files
feat:show api error status code (RooCodeInc#1635)
* Show status code / message when API request error occurs. * Moved logic to a helper method. * Different error message format. * Removed old comment. --------- Co-authored-by: Michael Overhorst <[email protected]>
1 parent 6508549 commit e41ff78

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/core/Cline.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,14 @@ export class Cline {
12021202
return false
12031203
}
12041204

1205+
private formatErrorWithStatusCode(error: any): string {
1206+
const statusCode = error.status || error.statusCode || (error.response && error.response.status)
1207+
const message = error.message ?? JSON.stringify(serializeError(error), null, 2)
1208+
1209+
// Only prepend the statusCode if it's not already part of the message
1210+
return statusCode && !message.includes(statusCode.toString()) ? `${statusCode} - ${message}` : message
1211+
}
1212+
12051213
async *attemptApiRequest(previousApiReqIndex: number): ApiStream {
12061214
// Wait for MCP servers to be connected before generating system prompt
12071215
await pWaitFor(() => this.providerRef.deref()?.mcpHub?.isConnecting !== true, { timeout: 10_000 }).catch(() => {
@@ -1309,10 +1317,9 @@ export class Cline {
13091317
} else {
13101318
// request failed after retrying automatically once, ask user if they want to retry again
13111319
// note that this api_req_failed ask is unique in that we only present this option if the api hasn't streamed any content yet (ie it fails on the first chunk due), as it would allow them to hit a retry button. However if the api failed mid-stream, it could be in any arbitrary state where some tools may have executed, so that error is handled differently and requires cancelling the task entirely.
1312-
const { response } = await this.ask(
1313-
"api_req_failed",
1314-
error.message ?? JSON.stringify(serializeError(error), null, 2),
1315-
)
1320+
const errorMessage = this.formatErrorWithStatusCode(error)
1321+
1322+
const { response } = await this.ask("api_req_failed", errorMessage)
13161323
if (response !== "yesButtonClicked") {
13171324
// this will never happen since if noButtonClicked, we will clear current task, aborting this instance
13181325
throw new Error("API request failed")
@@ -3060,7 +3067,9 @@ export class Cline {
30603067
// abandoned happens when extension is no longer waiting for the cline instance to finish aborting (error is thrown here when any function in the for loop throws due to this.abort)
30613068
if (!this.abandoned) {
30623069
this.abortTask() // if the stream failed, there's various states the task could be in (i.e. could have streamed some tools the user may have executed), so we just resort to replicating a cancel task
3063-
await abortStream("streaming_failed", error.message ?? JSON.stringify(serializeError(error), null, 2))
3070+
const errorMessage = this.formatErrorWithStatusCode(error)
3071+
3072+
await abortStream("streaming_failed", errorMessage)
30643073
const history = await this.providerRef.deref()?.getTaskWithId(this.taskId)
30653074
if (history) {
30663075
await this.providerRef.deref()?.initClineWithHistoryItem(history.historyItem)

0 commit comments

Comments
 (0)