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.
This commit is contained in:
@ -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<HttpResponse> {
|
||||
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 ? `<div class="error">${error.message}</div>` : "")
|
||||
.replace(
|
||||
/{{UPDATE_STATUS}}/,
|
||||
errorOrUpdate && !(errorOrUpdate instanceof Error)
|
||||
? `Updated to ${errorOrUpdate.version}`
|
||||
: await this.getUpdateHtml(),
|
||||
)
|
||||
.replace(/{{ERROR}}/, errorOrUpdate instanceof Error ? `<div class="error">${errorOrUpdate.message}</div>` : "")
|
||||
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)
|
||||
|
@ -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)),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user