From 28220494ff60ba1ebe82505e6f87c4d90c835dea Mon Sep 17 00:00:00 2001 From: develar Date: Tue, 2 Jan 2018 16:47:33 +0100 Subject: [PATCH] feat(electron-updater): add error codes Close #2415 --- packages/builder-util-runtime/src/httpExecutor.ts | 5 +++-- packages/builder-util-runtime/src/index.ts | 6 ++++++ packages/builder-util-runtime/src/uuid.ts | 5 +++-- packages/builder-util-runtime/src/xml.ts | 9 +++++---- packages/electron-updater/src/AppImageUpdater.ts | 6 +++--- packages/electron-updater/src/AppUpdater.ts | 10 +++++----- packages/electron-updater/src/BintrayProvider.ts | 6 +++--- packages/electron-updater/src/GenericProvider.ts | 4 ++-- packages/electron-updater/src/GitHubProvider.ts | 14 ++++++++------ packages/electron-updater/src/MacUpdater.ts | 4 ++-- packages/electron-updater/src/NsisUpdater.ts | 4 ++-- .../electron-updater/src/PrivateGitHubProvider.ts | 10 +++++----- packages/electron-updater/src/Provider.ts | 12 ++++++------ .../src/differentialDownloader/DataSplitter.ts | 13 +++++++------ packages/electron-updater/src/providerFactory.ts | 6 +++--- .../updater/__snapshots__/nsisUpdaterTest.js.snap | 4 ++-- test/src/helpers/packTester.ts | 2 +- 17 files changed, 66 insertions(+), 54 deletions(-) diff --git a/packages/builder-util-runtime/src/httpExecutor.ts b/packages/builder-util-runtime/src/httpExecutor.ts index f930bec348e..05b3977ced2 100644 --- a/packages/builder-util-runtime/src/httpExecutor.ts +++ b/packages/builder-util-runtime/src/httpExecutor.ts @@ -6,6 +6,7 @@ import { Socket } from "net" import { Transform } from "stream" import { parse as parseUrl, URL } from "url" import { CancellationToken } from "./CancellationToken" +import { newError } from "./index" import { ProgressCallbackTransform, ProgressInfo } from "./ProgressCallbackTransform" const debug = _debug("electron-builder") @@ -275,11 +276,11 @@ export class DigestTransform extends Transform { validate() { if (this._actual == null) { - throw new Error("Not finished yet") + throw newError("Not finished yet", "ERR_STREAM_NOT_FINISHED") } if (this._actual !== this.expected) { - throw new Error(`${this.algorithm} checksum mismatch, expected ${this.expected}, got ${this._actual}`) + throw newError(`${this.algorithm} checksum mismatch, expected ${this.expected}, got ${this._actual}`, "ERR_CHECKSUM_MISMATCH") } return null diff --git a/packages/builder-util-runtime/src/index.ts b/packages/builder-util-runtime/src/index.ts index e37965404cb..89883427298 100644 --- a/packages/builder-util-runtime/src/index.ts +++ b/packages/builder-util-runtime/src/index.ts @@ -17,4 +17,10 @@ export function asArray(v: null | undefined | T | Array): Array { else { return [v] } +} + +export function newError(message: string, code: string) { + const error = new Error(message); + (error as any).code = code + return error } \ No newline at end of file diff --git a/packages/builder-util-runtime/src/uuid.ts b/packages/builder-util-runtime/src/uuid.ts index 93aaddcb896..af24f43d3df 100644 --- a/packages/builder-util-runtime/src/uuid.ts +++ b/packages/builder-util-runtime/src/uuid.ts @@ -1,4 +1,5 @@ import { createHash, randomBytes } from "crypto" +import { newError } from "./index" const invalidName = "options.name must be either a string or a Buffer" @@ -123,7 +124,7 @@ export class UUID { } } - throw new Error("Unknown type of uuid") + throw newError("Unknown type of uuid", "ERR_UNKNOWN_UUID_TYPE") } // read stringified uuid into a Buffer @@ -248,7 +249,7 @@ function uuidNamed(name: string | Buffer, hashMethod: string, version: number, n const nameIsNotAString = typeof name !== "string" if (nameIsNotAString && !Buffer.isBuffer(name)) { - throw new Error(invalidName) + throw newError(invalidName, "ERR_INVALID_UUID_NAME") } hash.update(namespace) diff --git a/packages/builder-util-runtime/src/xml.ts b/packages/builder-util-runtime/src/xml.ts index 8b432c6b11e..0ef9a0ac7dd 100644 --- a/packages/builder-util-runtime/src/xml.ts +++ b/packages/builder-util-runtime/src/xml.ts @@ -1,4 +1,5 @@ import * as sax from "sax" +import { newError } from "./index" export class XElement { value = "" @@ -8,17 +9,17 @@ export class XElement { constructor(readonly name: string) { if (!name) { - throw new Error("Element name cannot be empty") + throw newError("Element name cannot be empty", "ERR_XML_ELEMENT_NAME_EMPTY") } if (!isValidName(name)) { - throw new Error(`Invalid element name: ${name}`) + throw newError(`Invalid element name: ${name}`, "ERR_XML_ELEMENT_INVALID_NAME") } } attribute(name: string): string { const result = this.attributes === null ? null : this.attributes[name] if (result == null) { - throw new Error(`No attribute "${name}"`) + throw newError(`No attribute "${name}"`, "ERR_XML_MISSED_ATTRIBUTE") } return result } @@ -32,7 +33,7 @@ export class XElement { element(name: string, ignoreCase = false, errorIfMissed: string | null = null): XElement { const result = this.elementOrNull(name, ignoreCase) if (result === null) { - throw new Error(errorIfMissed || `No element "${name}"`) + throw newError(errorIfMissed || `No element "${name}"`, "ERR_XML_MISSED_ELEMENT") } return result } diff --git a/packages/electron-updater/src/AppImageUpdater.ts b/packages/electron-updater/src/AppImageUpdater.ts index 863ea604e03..05b5e51a438 100644 --- a/packages/electron-updater/src/AppImageUpdater.ts +++ b/packages/electron-updater/src/AppImageUpdater.ts @@ -1,5 +1,5 @@ import BluebirdPromise from "bluebird-lst" -import { AllPublishOptions, CancellationToken, DownloadOptions, UpdateInfo } from "builder-util-runtime" +import { AllPublishOptions, CancellationToken, DownloadOptions, newError, UpdateInfo } from "builder-util-runtime" import { execFileSync, spawn } from "child_process" import isDev from "electron-is-dev" import { chmod, unlinkSync } from "fs-extra-p" @@ -51,7 +51,7 @@ export class AppImageUpdater extends BaseUpdater { const oldFile = process.env.APPIMAGE!! if (oldFile == null) { - throw new Error("APPIMAGE env is not defined") + throw newError("APPIMAGE env is not defined", "ERR_UPDATER_OLD_FILE_NOT_FOUND") } let isDownloadFull = false @@ -88,7 +88,7 @@ export class AppImageUpdater extends BaseUpdater { protected doInstall(installerPath: string, isSilent: boolean, isRunAfter: boolean): boolean { const appImageFile = process.env.APPIMAGE!! if (appImageFile == null) { - throw new Error("APPIMAGE env is not defined") + throw newError("APPIMAGE env is not defined", "ERR_UPDATER_OLD_FILE_NOT_FOUND") } // https://stackoverflow.com/a/1712051/1910191 diff --git a/packages/electron-updater/src/AppUpdater.ts b/packages/electron-updater/src/AppUpdater.ts index 089879beb89..ad7d6c7e75b 100644 --- a/packages/electron-updater/src/AppUpdater.ts +++ b/packages/electron-updater/src/AppUpdater.ts @@ -1,5 +1,5 @@ import BluebirdPromise from "bluebird-lst" -import { AllPublishOptions, asArray, CancellationToken, PublishConfiguration, UpdateInfo, UUID } from "builder-util-runtime" +import { AllPublishOptions, asArray, CancellationToken, newError, PublishConfiguration, UpdateInfo, UUID } from "builder-util-runtime" import { randomBytes } from "crypto" import { Notification } from "electron" import isDev from "electron-is-dev" @@ -63,10 +63,10 @@ export abstract class AppUpdater extends EventEmitter { set channel(value: string | null) { if (this._channel != null) { if (typeof value !== "string") { - throw new Error(`Channel must be a string, but got: ${value}`) + throw newError(`Channel must be a string, but got: ${value}`, "ERR_UPDATER_INVALID_CHANNEL") } else if (value.length === 0) { - throw new Error(`Channel must be not an empty string`) + throw newError(`Channel must be not an empty string`, "ERR_UPDATER_INVALID_CHANNEL") } } @@ -160,7 +160,7 @@ export abstract class AppUpdater extends EventEmitter { const currentVersionString = this.app.getVersion() const currentVersion = parseVersion(currentVersionString) if (currentVersion == null) { - throw new Error(`App version is not a valid semver version: "${currentVersionString}`) + throw newError(`App version is not a valid semver version: "${currentVersionString}`, "ERR_UPDATER_INVALID_VERSION") } this.currentVersion = currentVersion @@ -279,7 +279,7 @@ export abstract class AppUpdater extends EventEmitter { const latestVersion = parseVersion(updateInfo.version) if (latestVersion == null) { - throw new Error(`Latest version (from update server) is not valid semver version: "${latestVersion}`) + throw newError(`Latest version (from update server) is not valid semver version: "${latestVersion}`, "ERR_UPDATER_INVALID_VERSION") } const isStagingMatch = await this.isStagingMatch(updateInfo) diff --git a/packages/electron-updater/src/BintrayProvider.ts b/packages/electron-updater/src/BintrayProvider.ts index b676cbef7c7..0b19c6ed4d5 100644 --- a/packages/electron-updater/src/BintrayProvider.ts +++ b/packages/electron-updater/src/BintrayProvider.ts @@ -1,4 +1,4 @@ -import { BintrayOptions, CancellationToken, HttpExecutor, UpdateInfo } from "builder-util-runtime" +import { BintrayOptions, CancellationToken, HttpExecutor, newError, UpdateInfo } from "builder-util-runtime" import { BintrayClient } from "builder-util-runtime/out/bintray" import { URL } from "url" import { getChannelFilename, getDefaultChannelName, newBaseUrl, Provider, ResolvedUpdateFileInfo } from "./main" @@ -28,7 +28,7 @@ export class BintrayProvider extends Provider { const channelFile = files.find(it => it.name.endsWith(`_${channelFilename}`) || it.name.endsWith(`-${channelFilename}`)) if (channelFile == null) { // noinspection ExceptionCaughtLocallyJS - throw new Error(`Cannot find channel file "${channelFilename}", existing files:\n${files.map(it => JSON.stringify(it, null, 2)).join(",\n")}`) + throw newError(`Cannot find channel file "${channelFilename}", existing files:\n${files.map(it => JSON.stringify(it, null, 2)).join(",\n")}`, "ERR_UPDATER_CHANNEL_FILE_NOT_FOUND") } const channelFileUrl = new URL(`https://dl.bintray.com/${this.client.owner}/${this.client.repo}/${channelFile.name}`) @@ -36,7 +36,7 @@ export class BintrayProvider extends Provider { } catch (e) { if ("statusCode" in e && e.statusCode === 404) { - throw new Error(`No latest version, please ensure that user, package and repository correctly configured. Or at least one version is published. ${e.stack || e.message}`) + throw newError(`No latest version, please ensure that user, package and repository correctly configured. Or at least one version is published. ${e.stack || e.message}`, "ERR_UPDATER_LATEST_VERSION_NOT_FOUND") } throw e } diff --git a/packages/electron-updater/src/GenericProvider.ts b/packages/electron-updater/src/GenericProvider.ts index fcd544e01f1..c2746e27e13 100644 --- a/packages/electron-updater/src/GenericProvider.ts +++ b/packages/electron-updater/src/GenericProvider.ts @@ -1,4 +1,4 @@ -import { GenericServerOptions, HttpError, UpdateInfo } from "builder-util-runtime" +import { GenericServerOptions, HttpError, newError, UpdateInfo } from "builder-util-runtime" import { AppUpdater } from "./AppUpdater" import { getChannelFilename, getCustomChannelName, getDefaultChannelName, isUseOldMacProvider, newBaseUrl, newUrlFromBase, Provider, ResolvedUpdateFileInfo } from "./main" import { parseUpdateInfo, resolveFiles } from "./Provider" @@ -26,7 +26,7 @@ export class GenericProvider extends Provider { } catch (e) { if (e instanceof HttpError && e.statusCode === 404) { - throw new Error(`Cannot find channel "${channelFile}" update info: ${e.stack || e.message}`) + throw newError(`Cannot find channel "${channelFile}" update info: ${e.stack || e.message}`, "ERR_UPDATER_CHANNEL_FILE_NOT_FOUND") } else if (e.code === "ECONNREFUSED") { if (attemptNumber < 3) { diff --git a/packages/electron-updater/src/GitHubProvider.ts b/packages/electron-updater/src/GitHubProvider.ts index fffaa0d7965..61d2854d7d9 100644 --- a/packages/electron-updater/src/GitHubProvider.ts +++ b/packages/electron-updater/src/GitHubProvider.ts @@ -1,4 +1,4 @@ -import { CancellationToken, GithubOptions, githubUrl, HttpError, HttpExecutor, parseXml, ReleaseNoteInfo, UpdateInfo, XElement } from "builder-util-runtime" +import { CancellationToken, GithubOptions, githubUrl, HttpError, HttpExecutor, newError, parseXml, ReleaseNoteInfo, UpdateInfo, XElement } from "builder-util-runtime" import * as semver from "semver" import { URL } from "url" import { AppUpdater } from "./AppUpdater" @@ -9,7 +9,7 @@ export abstract class BaseGitHubProvider extends Provider< // so, we don't need to parse port (because node http doesn't support host as url does) protected readonly baseUrl: URL - constructor(protected readonly options: GithubOptions, defaultHost: string, executor: HttpExecutor) { + protected constructor(protected readonly options: GithubOptions, defaultHost: string, executor: HttpExecutor) { super(executor, false /* because GitHib uses S3 */) this.baseUrl = newBaseUrl(githubUrl(options, defaultHost)) @@ -40,6 +40,7 @@ export class GitHubProvider extends BaseGitHubProvider { let version: string | null try { if (this.updater.allowPrerelease) { + // noinspection TypeScriptValidateJSTypes version = latestRelease.element("link").attribute("href").match(/\/tag\/v?([^\/]+)$/)!![1] } else { @@ -47,11 +48,11 @@ export class GitHubProvider extends BaseGitHubProvider { } } catch (e) { - throw new Error(`Cannot parse releases feed: ${e.stack || e.message},\nXML:\n${feedXml}`) + throw newError(`Cannot parse releases feed: ${e.stack || e.message},\nXML:\n${feedXml}`, "ERR_UPDATER_INVALID_RELEASE_FEED") } if (version == null) { - throw new Error(`No published versions on GitHub`) + throw newError(`No published versions on GitHub`, "ERR_UPDATER_NO_PUBLISHED_VERSIONS") } const channelFile = getChannelFilename(getDefaultChannelName()) @@ -63,7 +64,7 @@ export class GitHubProvider extends BaseGitHubProvider { } catch (e) { if (!this.updater.allowPrerelease && e instanceof HttpError && e.statusCode === 404) { - throw new Error(`Cannot find ${channelFile} in the latest release artifacts (${channelFileUrl}): ${e.stack || e.message}`) + throw newError(`Cannot find ${channelFile} in the latest release artifacts (${channelFileUrl}): ${e.stack || e.message}`, "ERR_UPDATER_CHANNEL_FILE_NOT_FOUND") } throw e } @@ -96,7 +97,7 @@ export class GitHubProvider extends BaseGitHubProvider { return (releaseInfo.tag_name.startsWith("v")) ? releaseInfo.tag_name.substring(1) : releaseInfo.tag_name } catch (e) { - throw new Error(`Unable to find latest version on GitHub (${url}), please ensure a production release exists: ${e.stack || e.message}`) + throw newError(`Unable to find latest version on GitHub (${url}), please ensure a production release exists: ${e.stack || e.message}`, "ERR_UPDATER_LATEST_VERSION_NOT_FOUND") } } @@ -131,6 +132,7 @@ export function computeReleaseNotes(currentVersion: string, isFullChangelog: boo const releaseNotes: Array = [] for (const release of feed.getElements("entry")) { + // noinspection TypeScriptValidateJSTypes const versionRelease = release.element("link").attribute("href").match(/\/tag\/v?([^\/]+)$/)![1] if (semver.lt(currentVersion, versionRelease)) { releaseNotes.push({ diff --git a/packages/electron-updater/src/MacUpdater.ts b/packages/electron-updater/src/MacUpdater.ts index c3d2fe5d557..1a41f0fb92c 100644 --- a/packages/electron-updater/src/MacUpdater.ts +++ b/packages/electron-updater/src/MacUpdater.ts @@ -1,5 +1,5 @@ import BluebirdPromise from "bluebird-lst" -import { AllPublishOptions, CancellationToken, configureRequestOptionsFromUrl, DigestTransform, ProgressCallbackTransform, RequestHeaders, safeGetHeader, safeStringifyJson, UpdateInfo } from "builder-util-runtime" +import { AllPublishOptions, CancellationToken, configureRequestOptionsFromUrl, DigestTransform, newError, ProgressCallbackTransform, RequestHeaders, safeGetHeader, safeStringifyJson, UpdateInfo } from "builder-util-runtime" import { createServer, IncomingMessage, OutgoingHttpHeaders, ServerResponse } from "http" import { AppUpdater } from "./AppUpdater" import { DOWNLOAD_PROGRESS, UPDATE_DOWNLOADED } from "./main" @@ -26,7 +26,7 @@ export class MacUpdater extends AppUpdater { const files = (await this.provider).resolveFiles(updateInfo) const zipFileInfo = findFile(files, "zip", ["pkg", "dmg"]) if (zipFileInfo == null) { - throw new Error(`ZIP file not provided: ${safeStringifyJson(files)}`) + throw newError(`ZIP file not provided: ${safeStringifyJson(files)}`, "ERR_UPDATER_ZIP_FILE_NOT_FOUND") } const server = createServer() diff --git a/packages/electron-updater/src/NsisUpdater.ts b/packages/electron-updater/src/NsisUpdater.ts index ccd71533715..f561ea105c0 100644 --- a/packages/electron-updater/src/NsisUpdater.ts +++ b/packages/electron-updater/src/NsisUpdater.ts @@ -1,4 +1,4 @@ -import { AllPublishOptions, CancellationToken, DownloadOptions, PackageFileInfo, UpdateInfo } from "builder-util-runtime" +import { AllPublishOptions, CancellationToken, DownloadOptions, newError, PackageFileInfo, UpdateInfo } from "builder-util-runtime" import { spawn } from "child_process" import { OutgoingHttpHeaders } from "http" import * as path from "path" @@ -44,7 +44,7 @@ export class NsisUpdater extends BaseUpdater { if (signatureVerificationStatus != null) { await removeTempDirIfAny() // noinspection ThrowInsideFinallyBlockJS - throw new Error(`New version ${this.updateInfo!.version} is not signed by the application owner: ${signatureVerificationStatus}`) + throw newError(`New version ${this.updateInfo!.version} is not signed by the application owner: ${signatureVerificationStatus}`, "ERR_UPDATER_INVALID_SIGNATURE") } const packageInfo = fileInfo.packageInfo diff --git a/packages/electron-updater/src/PrivateGitHubProvider.ts b/packages/electron-updater/src/PrivateGitHubProvider.ts index d7b06db7242..701742f123d 100644 --- a/packages/electron-updater/src/PrivateGitHubProvider.ts +++ b/packages/electron-updater/src/PrivateGitHubProvider.ts @@ -1,4 +1,4 @@ -import { CancellationToken, GithubOptions, HttpError, HttpExecutor, UpdateInfo } from "builder-util-runtime" +import { CancellationToken, GithubOptions, HttpError, HttpExecutor, newError, UpdateInfo } from "builder-util-runtime" import { OutgoingHttpHeaders, RequestOptions } from "http" import { safeLoad } from "js-yaml" import * as path from "path" @@ -31,7 +31,7 @@ export class PrivateGitHubProvider extends BaseGitHubProvider it.name === channelFile) if (asset == null) { // html_url must be always, but just to be sure - throw new Error(`Cannot find ${channelFile} in the release ${releaseInfo.html_url || releaseInfo.name}`) + throw newError(`Cannot find ${channelFile} in the release ${releaseInfo.html_url || releaseInfo.name}`, "ERR_UPDATER_CHANNEL_FILE_NOT_FOUND") } const url = new URL(asset.url) @@ -41,7 +41,7 @@ export class PrivateGitHubProvider extends BaseGitHubProvider it != null && it.name === name) if (asset == null) { - throw new Error(`Cannot find asset "${name}" in: ${JSON.stringify(updateInfo.assets, null, 2)}`) + throw newError(`Cannot find asset "${name}" in: ${JSON.stringify(updateInfo.assets, null, 2)}`, "ERR_UPDATER_ASSET_NOT_FOUND") } return { diff --git a/packages/electron-updater/src/Provider.ts b/packages/electron-updater/src/Provider.ts index 6e39d121116..f740f4c0f51 100644 --- a/packages/electron-updater/src/Provider.ts +++ b/packages/electron-updater/src/Provider.ts @@ -1,4 +1,4 @@ -import { CancellationToken, HttpExecutor, safeStringifyJson, UpdateFileInfo, UpdateInfo, WindowsUpdateInfo } from "builder-util-runtime" +import { CancellationToken, HttpExecutor, newError, safeStringifyJson, UpdateFileInfo, UpdateInfo, WindowsUpdateInfo } from "builder-util-runtime" import { OutgoingHttpHeaders, RequestOptions } from "http" import { safeLoad } from "js-yaml" import { URL } from "url" @@ -49,7 +49,7 @@ export abstract class Provider { export function findFile(files: Array, extension: string, not?: Array): ResolvedUpdateFileInfo | null | undefined { if (files.length === 0) { - throw new Error("No files provided") + throw newError("No files provided", "ERR_UPDATER_NO_FILES_PROVIDED") } const result = files.find(it => it.url.pathname.toLowerCase().endsWith(`.${extension}`)) @@ -66,7 +66,7 @@ export function findFile(files: Array, extension: string export function parseUpdateInfo(rawData: string | null, channelFile: string, channelFileUrl: URL): UpdateInfo { if (rawData == null) { - throw new Error(`Cannot parse update info from ${channelFile} in the latest release artifacts (${channelFileUrl}): rawData: null`) + throw newError(`Cannot parse update info from ${channelFile} in the latest release artifacts (${channelFileUrl}): rawData: null`, "ERR_UPDATER_INVALID_UPDATE_INFO") } let result: UpdateInfo @@ -74,7 +74,7 @@ export function parseUpdateInfo(rawData: string | null, channelFile: string, cha result = safeLoad(rawData) } catch (e) { - throw new Error(`Cannot parse update info from ${channelFile} in the latest release artifacts (${channelFileUrl}): ${e.stack || e.message}, rawData: ${rawData}`) + throw newError(`Cannot parse update info from ${channelFile} in the latest release artifacts (${channelFileUrl}): ${e.stack || e.message}, rawData: ${rawData}`, "ERR_UPDATER_INVALID_UPDATE_INFO") } return result } @@ -94,7 +94,7 @@ export function getFileList(updateInfo: UpdateInfo): Array { ] } else { - throw new Error(`No files provided: ${safeStringifyJson(updateInfo)}`) + throw newError(`No files provided: ${safeStringifyJson(updateInfo)}`, "ERR_UPDATER_NO_FILES_PROVIDED") } } @@ -102,7 +102,7 @@ export function resolveFiles(updateInfo: UpdateInfo, baseUrl: URL, pathTransform const files = getFileList(updateInfo) const result: Array = files.map(fileInfo => { if ((fileInfo as any).sha2 == null && fileInfo.sha512 == null) { - throw new Error(`Update info doesn't contain nor sha256 neither sha512 checksum: ${safeStringifyJson(fileInfo)}`) + throw newError(`Update info doesn't contain nor sha256 neither sha512 checksum: ${safeStringifyJson(fileInfo)}`, "ERR_UPDATER_NO_CHECKSUM") } return { url: newUrlFromBase(pathTransformer(fileInfo.url), baseUrl), diff --git a/packages/electron-updater/src/differentialDownloader/DataSplitter.ts b/packages/electron-updater/src/differentialDownloader/DataSplitter.ts index 942fd9ba6f2..91ea3106c16 100644 --- a/packages/electron-updater/src/differentialDownloader/DataSplitter.ts +++ b/packages/electron-updater/src/differentialDownloader/DataSplitter.ts @@ -1,7 +1,8 @@ +import BluebirdPromise from "bluebird-lst" +import { newError } from "builder-util-runtime" +import { createReadStream } from "fs-extra-p" import { Writable } from "stream" import { Operation, OperationKind } from "./downloadPlanBuilder" -import { createReadStream } from "fs-extra-p" -import BluebirdPromise from "bluebird-lst" const DOUBLE_CRLF = Buffer.from("\r\n\r\n") @@ -69,7 +70,7 @@ export class DataSplitter extends Writable { let start = 0 if (this.ignoreByteCount !== 0 && this.remainingPartDataCount !== 0) { - throw new Error("Internal error") + throw newError("Internal error", "ERR_DATA_SPLITTER_BYTE_COUNT_MISMATCH") } if (this.ignoreByteCount > 0) { @@ -113,7 +114,7 @@ export class DataSplitter extends Writable { taskIndex = this.options.end } else { - throw new Error("taskIndex is null") + throw newError("taskIndex is null", "ERR_DATA_SPLITTER_TASK_INDEX_IS_NULL") } } @@ -122,7 +123,7 @@ export class DataSplitter extends Writable { await this.copyExistingData(prevTaskIndex, taskIndex) } else if (prevTaskIndex > taskIndex) { - throw new Error("prevTaskIndex must be < taskIndex") + throw newError("prevTaskIndex must be < taskIndex", "ERR_DATA_SPLITTER_TASK_INDEX_ASSERT_FAILED") } if (this.isFinished) { @@ -201,7 +202,7 @@ export class DataSplitter extends Writable { private onPartEnd() { const expectedLength = this.partIndexToLength[this.partIndex - 1] if (this.actualPartLength !== expectedLength) { - throw new Error(`Expected length: ${expectedLength} differs from actual: ${this.actualPartLength}`) + throw newError(`Expected length: ${expectedLength} differs from actual: ${this.actualPartLength}`, "ERR_DATA_SPLITTER_LENGTH_MISMATCH") } this.actualPartLength = 0 } diff --git a/packages/electron-updater/src/providerFactory.ts b/packages/electron-updater/src/providerFactory.ts index 995464ba41f..b86099cd94b 100644 --- a/packages/electron-updater/src/providerFactory.ts +++ b/packages/electron-updater/src/providerFactory.ts @@ -1,4 +1,4 @@ -import { AllPublishOptions, BaseS3Options, BintrayOptions, GenericServerOptions, getS3LikeProviderBaseUrl, GithubOptions, PublishConfiguration } from "builder-util-runtime" +import { AllPublishOptions, BaseS3Options, BintrayOptions, GenericServerOptions, getS3LikeProviderBaseUrl, GithubOptions, newError, PublishConfiguration } from "builder-util-runtime" import { AppUpdater } from "./AppUpdater" import { BintrayProvider } from "./BintrayProvider" import { GenericProvider } from "./GenericProvider" @@ -7,7 +7,7 @@ import { PrivateGitHubProvider } from "./PrivateGitHubProvider" export function createClient(data: PublishConfiguration | AllPublishOptions, updater: AppUpdater) { if (typeof data === "string") { - throw new Error("Please pass PublishConfiguration object") + throw newError("Please pass PublishConfiguration object", "ERR_UPDATER_INVALID_PROVIDER_CONFIGURATION") } const httpExecutor = updater.httpExecutor @@ -38,6 +38,6 @@ export function createClient(data: PublishConfiguration | AllPublishOptions, upd return new BintrayProvider(data as BintrayOptions, httpExecutor) default: - throw new Error(`Unsupported provider: ${provider}`) + throw newError(`Unsupported provider: ${provider}`, "ERR_UPDATER_UNSUPPORTED_PROVIDER") } } \ No newline at end of file diff --git a/test/out/updater/__snapshots__/nsisUpdaterTest.js.snap b/test/out/updater/__snapshots__/nsisUpdaterTest.js.snap index be62c18586c..ae1941a27be 100644 --- a/test/out/updater/__snapshots__/nsisUpdaterTest.js.snap +++ b/test/out/updater/__snapshots__/nsisUpdaterTest.js.snap @@ -62,7 +62,7 @@ Array [ exports[`cancel download with progress 1`] = `"Cancelled"`; -exports[`check updates - no versions at all 1`] = `"No latest version, please ensure that user, package and repository correctly configured. Or at least one version is published. HttpError: 404 Not Found"`; +exports[`check updates - no versions at all 1`] = `"ERR_UPDATER_LATEST_VERSION_NOT_FOUND"`; exports[`checkForUpdates several times 1`] = ` Object { @@ -397,7 +397,7 @@ Object { } `; -exports[`sha512 mismatch error event 2`] = `"sha512 checksum mismatch, expected Dj51I0q8aPQ3ioaz9LMqGYujAYRbDNblAQbodDRXAMxmY6hsHqEl3F6SvhfJj5oPhcqdX1ldsgEvfMNXGUXBIZ==, got Dj51I0q8aPQ3ioaz9LMqGYujAYRbDNblAQbodDRXAMxmY6hsHqEl3F6SvhfJj5oPhcqdX1ldsgEvfMNXGUXBIw=="`; +exports[`sha512 mismatch error event 2`] = `"ERR_CHECKSUM_MISMATCH"`; exports[`sha512 mismatch error event 3`] = ` Array [ diff --git a/test/src/helpers/packTester.ts b/test/src/helpers/packTester.ts index c16df8ad99f..81feb3ca361 100644 --- a/test/src/helpers/packTester.ts +++ b/test/src/helpers/packTester.ts @@ -419,7 +419,7 @@ export function platform(platform: Platform): PackagerOptions { export function signed(packagerOptions: PackagerOptions): PackagerOptions { if (process.env.CSC_KEY_PASSWORD == null) { - log.warn({reason: "CSC_KEY_PASSWORD is not defined"}, "macOS code sign is not tested") + log.warn({reason: "CSC_KEY_PASSWORD is not defined"}, "macOS code signing is not tested") } else { packagerOptions.cscLink = CSC_LINK