From 27ba64c7e4c27ea08c3856b543be11a38c4879ca Mon Sep 17 00:00:00 2001 From: Asher Date: Fri, 17 Apr 2020 14:58:14 -0500 Subject: [PATCH] Improve request error handling See #1532 for more context. - Errored JSON requests will get back the error in JSON instead of using the status text. This seems better to me because it seems more correct to utilize the response body over hijacking the status text. The caller is expecting JSON anyway. Worst of all I never actually set the status text like I thought I did so it wasn't working to begin with. - Allow the update error to propagate for JSON update requests. It was caught to show the error inline instead of an error page when using the update page but for JSON requests it meant there was no error and no error code so it looked like it succeeded. - Make errors for failed requests to GitHub less incomprehensible. Previously they would just be the code which is no context at all. --- ci/vscode.patch | 18 ++++++++---------- package.json | 2 +- src/node/app/update.ts | 23 ++++++++++++++++------- src/node/http.ts | 18 +++++++++++++----- 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/ci/vscode.patch b/ci/vscode.patch index 9393be35..85868212 100644 --- a/ci/vscode.patch +++ b/ci/vscode.patch @@ -541,10 +541,10 @@ index eab8591492..26668701f7 100644 options.logService.error(`${logPrefix} socketFactory.connect() failed. Error:`); diff --git a/src/vs/server/browser/client.ts b/src/vs/server/browser/client.ts new file mode 100644 -index 0000000000..4f8543d975 +index 0000000000..649cf32f0a --- /dev/null +++ b/src/vs/server/browser/client.ts -@@ -0,0 +1,266 @@ +@@ -0,0 +1,264 @@ +import { Emitter } from 'vs/base/common/event'; +import { URI } from 'vs/base/common/uri'; +import { localize } from 'vs/nls'; @@ -720,11 +720,10 @@ index 0000000000..4f8543d975 + const response = await fetch(normalize(`${options.base}/update/apply`), { + headers: { "content-type": "application/json" }, + }); -+ if (response.status !== 200) { -+ throw new Error(response.statusText); -+ } -+ + const json = await response.json(); ++ if (response.status !== 200 || json.error) { ++ throw new Error(json.error || response.statusText); ++ } + (services.get(INotificationService) as INotificationService).info(`Updated to ${json.version}`); + }; + @@ -734,11 +733,10 @@ index 0000000000..4f8543d975 + const response = await fetch(normalize(`${options.base}/update`), { + headers: { "content-type": "application/json" }, + }); -+ if (response.status !== 200) { -+ throw new Error("unexpected response"); -+ } -+ + const json = await response.json(); ++ if (response.status !== 200 || json.error) { ++ throw new Error(json.error || response.statusText); ++ } + if (json.isLatest) { + return; + } diff --git a/package.json b/package.json index ffd7d67f..0ead146d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "code-server", "license": "MIT", - "version": "3.1.1", + "version": "3.1.2", "scripts": { "clean": "ci/clean.sh", "vscode": "ci/vscode.sh", diff --git a/src/node/app/update.ts b/src/node/app/update.ts index 6acbc571..588e5201 100644 --- a/src/node/app/update.ts +++ b/src/node/app/update.ts @@ -87,8 +87,7 @@ export class UpdateHttpProvider extends HttpProvider { public async getRoot( route: Route, request: http.IncomingMessage, - appliedUpdate?: string, - error?: Error, + errorOrUpdate?: Update | Error, ): Promise { if (request.headers["content-type"] === "application/json") { if (!this.enabled) { @@ -108,8 +107,13 @@ export class UpdateHttpProvider extends HttpProvider { } const response = await this.getUtf8Resource(this.rootPath, "src/browser/pages/update.html") response.content = response.content - .replace(/{{UPDATE_STATUS}}/, appliedUpdate ? `Updated to ${appliedUpdate}` : await this.getUpdateHtml()) - .replace(/{{ERROR}}/, error ? `
${error.message}
` : "") + .replace( + /{{UPDATE_STATUS}}/, + errorOrUpdate && !(errorOrUpdate instanceof Error) + ? `Updated to ${errorOrUpdate.version}` + : await this.getUpdateHtml(), + ) + .replace(/{{ERROR}}/, errorOrUpdate instanceof Error ? `
${errorOrUpdate.message}
` : "") return this.replaceTemplates(route, response) } @@ -186,11 +190,16 @@ export class UpdateHttpProvider extends HttpProvider { const update = await this.getUpdate() if (!this.isLatestVersion(update)) { await this.downloadAndApplyUpdate(update) - return this.getRoot(route, request, update.version) + return this.getRoot(route, request, update) } return this.getRoot(route, request) } catch (error) { - return this.getRoot(route, request, undefined, error) + // For JSON requests propagate the error. Otherwise catch it so we can + // show the error inline with the update button instead of an error page. + if (request.headers["content-type"] === "application/json") { + throw error + } + return this.getRoot(route, error) } } @@ -362,7 +371,7 @@ export class UpdateHttpProvider extends HttpProvider { } if (!response.statusCode || response.statusCode < 200 || response.statusCode >= 400) { - return reject(new Error(`${response.statusCode || "500"}`)) + return reject(new Error(`${uri}: ${response.statusCode || "500"}`)) } resolve(response) diff --git a/src/node/http.ts b/src/node/http.ts index dd5ff581..5c065374 100644 --- a/src/node/http.ts +++ b/src/node/http.ts @@ -646,11 +646,19 @@ export class HttpServer { if (code >= HttpCode.ServerError) { logger.error(error.stack) } - const payload = await route.provider.getErrorRoot(route, code, code, e.message) - write({ - code, - ...payload, - }) + if (request.headers["content-type"] === "application/json") { + write({ + code, + content: { + error: e.message, + }, + }) + } else { + write({ + code, + ...(await route.provider.getErrorRoot(route, code, code, e.message)), + }) + } } }