Skip to content

Commit

Permalink
feat: better errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ido-pluto committed Feb 27, 2024
1 parent 9326aaa commit 050dcdb
Show file tree
Hide file tree
Showing 19 changed files with 88 additions and 93 deletions.
51 changes: 16 additions & 35 deletions src/browser.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,32 @@
import {downloadFileBrowser, DownloadFileBrowserOptions, downloadSequenceBrowser} from "./download/browser-download.js";
import DownloadEngineBrowser, {DownloadEngineOptionsBrowser} from "./download/download-engine/engine/download-engine-browser.js";
import DownloadEngineFile, {DownloadEngineFileOptionsWithDefaults} from "./download/download-engine/download-engine-file.js";
import ProgressStatusFile from "./download/download-engine/progress-status-file.js";
import TransferStatistics, {TransferProgressInfo} from "./download/transfer-visualize/transfer-statistics.js";
import {truncateText} from "./utils/truncate-text.js";
import DownloadEngineWriteStreamBrowser, {
DownloadEngineWriteStreamBrowserWriter
} from "./download/download-engine/streams/download-engine-write-stream/download-engine-write-stream-browser.js";
import DownloadEngineBrowser from "./download/download-engine/engine/download-engine-browser.js";
import EmptyResponseError from "./download/download-engine/streams/download-engine-fetch-stream/errors/empty-response-error.js";
import StatusCodeError from "./download/download-engine/streams/download-engine-fetch-stream/errors/status-code-error.js";
import XhrError from "./download/download-engine/streams/download-engine-fetch-stream/errors/xhr-error.js";
import BaseDownloadEngineFetchStream, {
BaseDownloadEngineFetchStreamOptions
} from "./download/download-engine/streams/download-engine-fetch-stream/base-download-engine-fetch-stream.js";
import DownloadEngineFetchStreamFetch
from "./download/download-engine/streams/download-engine-fetch-stream/download-engine-fetch-stream-fetch.js";
import DownloadEngineFetchStreamXhr
from "./download/download-engine/streams/download-engine-fetch-stream/download-engine-fetch-stream-xhr.js";
import BaseDownloadEngineWriteStream
from "./download/download-engine/streams/download-engine-write-stream/base-download-engine-write-stream.js";
import ProgressStatisticsBuilder, {TransferProgressWithStatus} from "./download/transfer-visualize/progress-statistics-builder.js";
import {TransferProgressWithStatus} from "./download/transfer-visualize/progress-statistics-builder.js";
import {SaveProgressInfo} from "./download/download-engine/types.js";
import InvalidContentLengthError
from "./download/download-engine/streams/download-engine-fetch-stream/errors/invalid-content-length-error.js";
import FetchStreamError from "./download/download-engine/streams/download-engine-fetch-stream/errors/fetch-stream-error.js";
import IpullError from "./errors/ipull-error.js";
import EngineError from "./download/download-engine/engine/error/engine-error.js";

export {
DownloadEngineBrowser,
DownloadEngineFile,
TransferStatistics,
ProgressStatisticsBuilder,
ProgressStatusFile,
downloadFileBrowser,
downloadSequenceBrowser,
truncateText,
EmptyResponseError,
StatusCodeError,
XhrError,
BaseDownloadEngineFetchStream,
DownloadEngineFetchStreamFetch,
DownloadEngineFetchStreamXhr,
BaseDownloadEngineWriteStream,
DownloadEngineWriteStreamBrowser
InvalidContentLengthError,
FetchStreamError,
IpullError,
EngineError
};


export type {
TransferProgressInfo,
DownloadFileBrowserOptions,
DownloadEngineOptionsBrowser,
DownloadEngineWriteStreamBrowserWriter,
DownloadEngineFileOptionsWithDefaults as DownloadEngineFileOptions,
BaseDownloadEngineFetchStreamOptions,
TransferProgressWithStatus
DownloadEngineBrowser,
TransferProgressWithStatus,
SaveProgressInfo
};
6 changes: 3 additions & 3 deletions src/download/download-engine/download-engine-file.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {PromisePool, Stoppable} from "@supercharge/promise-pool";
import ProgressStatusFile from "./progress-status-file.js";
import {ChunkStatus, DownloadFile, DownloadProgressInfo} from "./types.js";
import {ChunkStatus, DownloadFile, SaveProgressInfo} from "./types.js";
import BaseDownloadEngineFetchStream from "./streams/download-engine-fetch-stream/base-download-engine-fetch-stream.js";
import BaseDownloadEngineWriteStream from "./streams/download-engine-write-stream/base-download-engine-write-stream.js";
import retry from "async-retry";
Expand All @@ -27,7 +27,7 @@ export type DownloadEngineFileEvents = {
paused: () => void
resumed: () => void
progress: (progress: ProgressStatusFile) => void
save: (progress: DownloadProgressInfo) => void
save: (progress: SaveProgressInfo) => void
finished: () => void
closed: () => void
[key: string]: any
Expand All @@ -42,7 +42,7 @@ export default class DownloadEngineFile extends EventEmitter<DownloadEngineFileE
public readonly file: DownloadFile;
public options: DownloadEngineFileOptionsWithDefaults;

protected _progress: DownloadProgressInfo = {
protected _progress: SaveProgressInfo = {
part: 0,
chunks: [],
chunkSize: 0
Expand Down
4 changes: 2 additions & 2 deletions src/download/download-engine/engine/base-download-engine.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {DownloadFile, DownloadProgressInfo} from "../types.js";
import {DownloadFile, SaveProgressInfo} from "../types.js";
import DownloadEngineFile, {DownloadEngineFileOptions} from "../download-engine-file.js";
import BaseDownloadEngineFetchStream, {
BaseDownloadEngineFetchStreamOptions
Expand All @@ -23,7 +23,7 @@ export type BaseDownloadEngineEvents = {
paused: () => void
resumed: () => void
progress: (progress: TransferProgressWithStatus) => void
save: (progress: DownloadProgressInfo) => void
save: (progress: SaveProgressInfo) => void
finished: () => void
closed: () => void
[key: string]: any
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {DownloadProgressInfo} from "../types.js";
import {SaveProgressInfo} from "../types.js";
import DownloadEngineFile from "../download-engine-file.js";
import DownloadEngineFetchStreamFetch from "../streams/download-engine-fetch-stream/download-engine-fetch-stream-fetch.js";
import DownloadEngineFetchStreamXhr from "../streams/download-engine-fetch-stream/download-engine-fetch-stream-xhr.js";
Expand All @@ -11,7 +11,7 @@ import BaseDownloadEngineFetchStream from "../streams/download-engine-fetch-stre

export type DownloadEngineOptionsBrowser = BaseDownloadEngineOptions & {
onWrite?: DownloadEngineWriteStreamBrowserWriter,
progress?: DownloadProgressInfo,
progress?: SaveProgressInfo,
fetchStrategy?: "xhr" | "fetch",
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default class DownloadAlreadyStartedError extends Error {
import EngineError from "./engine-error.js";

export default class DownloadAlreadyStartedError extends EngineError {
public constructor() {
super("Download already started");
}
Expand Down
3 changes: 3 additions & 0 deletions src/download/download-engine/engine/error/engine-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import IpullError from "../../../../errors/ipull-error.js";

export default class EngineError extends IpullError {}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export default class MissingEventError extends Error {}
import EngineError from "./engine-error.js";

export default class MissingEventError extends EngineError {}
4 changes: 3 additions & 1 deletion src/download/download-engine/engine/error/save-path-error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default class SavePathError extends Error {
import EngineError from "./engine-error.js";

export default class SavePathError extends EngineError {
constructor(message: string) {
super(message);
}
Expand Down
4 changes: 3 additions & 1 deletion src/download/download-engine/engine/error/url-input-error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default class UrlInputError extends Error {
import EngineError from "./engine-error.js";

export default class UrlInputError extends EngineError {
constructor(message: string) {
super(message);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default class EmptyResponseError extends Error {
import FetchStreamError from "./fetch-stream-error.js";

export default class EmptyResponseError extends FetchStreamError {
constructor(url: string, public readonly headers: { [key: string]: string | string[] }) {
super("Empty response error: " + url);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import IpullError from "../../../../../errors/ipull-error.js";

export default class FetchStreamError extends IpullError {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default class InvalidContentLengthError extends Error {
import FetchStreamError from "./fetch-stream-error.js";

export default class InvalidContentLengthError extends FetchStreamError {
constructor(expectedLength: number, gotLength: number) {
super(`Expected ${expectedLength} bytes, but got ${gotLength} bytes. If you on browser try to set "ignoreIfRangeWithQueryParams" to true, this will add a "_ignore" query parameter to the URL to avoid chrome "if-range" header.`);

Check warning on line 5 in src/download/download-engine/streams/download-engine-fetch-stream/errors/invalid-content-length-error.ts

View workflow job for this annotation

GitHub Actions / test

This line has a length of 236. Maximum allowed is 140
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default class PathNotAFileError extends Error {
import FetchStreamError from "./fetch-stream-error.js";

export default class PathNotAFileError extends FetchStreamError {
constructor(path: string) {
super(`Path is not a file: ${path}`);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default class StatusCodeError extends Error {
import FetchStreamError from "./fetch-stream-error.js";

export default class StatusCodeError extends FetchStreamError {
constructor(
public readonly url: string,
public readonly statusCode: number,
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export default class XhrError extends Error {}
import FetchStreamError from "./fetch-stream-error.js";

export default class XhrError extends FetchStreamError {}
4 changes: 2 additions & 2 deletions src/download/download-engine/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export enum ChunkStatus {
COMPLETE
}

export type DownloadProgressInfo = {
export type SaveProgressInfo = {
part: number,
chunks: ChunkStatus[],
chunkSize: number,
Expand All @@ -20,5 +20,5 @@ export type DownloadFile = {
totalSize: number
localFileName: string
parts: DownloadFilePart[]
downloadProgress?: DownloadProgressInfo
downloadProgress?: SaveProgressInfo
};
4 changes: 2 additions & 2 deletions src/download/node-download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type CliProgressDownloadEngineOptions = {
cliAction?: string;
};

export type DownloadEngineOptionsCLI = DownloadEngineOptionsNodejs & CliProgressDownloadEngineOptions;
export type DownloadFileOptions = DownloadEngineOptionsNodejs & CliProgressDownloadEngineOptions;

function createCliProgressForDownloadEngine(options: CliProgressDownloadEngineOptions) {
const cliOptions: TransferCliOptions = {...options};
Expand All @@ -35,7 +35,7 @@ function createCliProgressForDownloadEngine(options: CliProgressDownloadEngineOp
/**
* Download one file with CLI progress
*/
export async function downloadFile(options: DownloadEngineOptionsCLI) {
export async function downloadFile(options: DownloadFileOptions) {
const downloader = await DownloadEngineNodejs.createFromOptions(options);

if (options.cliProgress) {
Expand Down
1 change: 1 addition & 0 deletions src/errors/ipull-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default class IpullError extends Error {}
65 changes: 27 additions & 38 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,36 @@
import {truncateText} from "./utils/truncate-text.js";
import TransferStatistics, {TransferProgressInfo} from "./download/transfer-visualize/transfer-statistics.js";
import TransferCli, {TransferCliOptions, TransferCliStatus} from "./download/transfer-visualize/transfer-cli/transfer-cli.js";
import DownloadEngineNodejs, {DownloadEngineOptionsNodejs} from "./download/download-engine/engine/download-engine-nodejs.js";
import DownloadEngineFile, {DownloadEngineFileOptionsWithDefaults} from "./download/download-engine/download-engine-file.js";
import {downloadFile, downloadSequence} from "./download/node-download.js";
import BaseDownloadEngine from "./download/download-engine/engine/base-download-engine.js";
import BaseDownloadEngineFetchStream, {
BaseDownloadEngineFetchStreamOptions
} from "./download/download-engine/streams/download-engine-fetch-stream/base-download-engine-fetch-stream.js";
import DownloadEngineFetchStreamFetch
from "./download/download-engine/streams/download-engine-fetch-stream/download-engine-fetch-stream-fetch.js";
import DownloadEngineFetchStreamLocalFile
from "./download/download-engine/streams/download-engine-fetch-stream/download-engine-fetch-stream-local-file.js";
import BaseDownloadEngineWriteStream
from "./download/download-engine/streams/download-engine-write-stream/base-download-engine-write-stream.js";
import DownloadEngineWriteStreamNodejs
from "./download/download-engine/streams/download-engine-write-stream/download-engine-write-stream-nodejs.js";
import ProgressStatisticsBuilder, {TransferProgressWithStatus} from "./download/transfer-visualize/progress-statistics-builder.js";
import DownloadEngineNodejs from "./download/download-engine/engine/download-engine-nodejs.js";
import {downloadFile, DownloadFileOptions, downloadSequence, DownloadSequenceOptions} from "./download/node-download.js";
import {TransferProgressWithStatus} from "./download/transfer-visualize/progress-statistics-builder.js";
import {SaveProgressInfo} from "./download/download-engine/types.js";
import PathNotAFileError from "./download/download-engine/streams/download-engine-fetch-stream/errors/path-not-a-file-error.js";
import EmptyResponseError from "./download/download-engine/streams/download-engine-fetch-stream/errors/empty-response-error.js";
import StatusCodeError from "./download/download-engine/streams/download-engine-fetch-stream/errors/status-code-error.js";
import XhrError from "./download/download-engine/streams/download-engine-fetch-stream/errors/xhr-error.js";
import InvalidContentLengthError
from "./download/download-engine/streams/download-engine-fetch-stream/errors/invalid-content-length-error.js";
import FetchStreamError from "./download/download-engine/streams/download-engine-fetch-stream/errors/fetch-stream-error.js";
import IpullError from "./errors/ipull-error.js";
import EngineError from "./download/download-engine/engine/error/engine-error.js";

export {
TransferStatistics,
TransferCli,
ProgressStatisticsBuilder,
DownloadEngineNodejs,
BaseDownloadEngine,
DownloadEngineFile,
truncateText,
downloadFile,
downloadSequence,
BaseDownloadEngineFetchStream,
DownloadEngineFetchStreamFetch,
DownloadEngineFetchStreamLocalFile,
BaseDownloadEngineWriteStream,
DownloadEngineWriteStreamNodejs
PathNotAFileError,
EmptyResponseError,
StatusCodeError,
XhrError,
InvalidContentLengthError,
FetchStreamError,
IpullError,
EngineError
};

export type {
TransferProgressInfo,
TransferCliOptions,
TransferCliStatus,
DownloadEngineOptionsNodejs,
DownloadEngineFileOptionsWithDefaults as DownloadEngineFileOptions,
BaseDownloadEngineFetchStreamOptions,
TransferProgressWithStatus
DownloadFileOptions,
DownloadSequenceOptions,
DownloadEngineNodejs,
TransferProgressWithStatus,
SaveProgressInfo
};


0 comments on commit 050dcdb

Please sign in to comment.