History

refactor(controller): share download operation errors

seroxdesign committed 3 months ago

Commit 9c735a31 parent f7ad3903 files · +17 −25Browse files

Showing the changes to controller/src/modules (3 of 3 files). Show all

Unified · Split

3 files
  1. controller/src/modules/engines/downloads/download-manager.ts +1 −12
  2. controller/src/modules/engines/downloads/download-operation.ts +14 −0
  3. controller/src/modules/engines/downloads/download-store.ts +2 −13
controller/src/modules/engines/downloads/download-manager.ts+1−12
@@ -18,6 +18,7 @@ import { DOWNLOAD_DEFAULT_IGNORE_FILENAMES, DOWNLOAD_PROGRESS_THROTTLE_MS } from · 17 unmodified lines
1818 import { EngineOperationError } from "../engine-spec";
1919 import type { DownloadFileInfo, DownloadStatus, ModelDownload } from "../types";
2020 import type { DownloadStore } from "./download-store";
21+import { attempt, operationError } from "./download-operation";
2122 import {
2223 buildHuggingFaceFileList,
2324 fetchEffect,
@@ -108,18 +109,6 @@ type ActiveDownload = { · 84 unmodified lines
108109
109110 const toTimestamp = (): string => new Date().toISOString();
110111
111−const operationError = (operation: string, cause: unknown): EngineOperationError =>
112− new EngineOperationError({
113− operation,
114− message: cause instanceof Error ? cause.message : String(cause),
115− });
116−
117−const attempt = <A>(operation: string, evaluate: () => A): Effect.Effect<A, EngineOperationError> =>
118− Effect.try({
119− try: evaluate,
120− catch: (cause) => operationError(operation, cause),
121− });
122−
123112 const closeWriter = (
124113 writer: ReturnType<typeof createWriteStream>,
125114 ): Effect.Effect<void, EngineOperationError> =>
controller/src/modules/engines/downloads/download-operation.ts+14−0
@@ -0,0 +1,14 @@
1+import { Effect } from "effect";
2+import { EngineOperationError } from "../engine-spec";
3+
4+export const operationError = (operation: string, cause: unknown): EngineOperationError =>
5+ new EngineOperationError({
6+ operation,
7+ message: cause instanceof Error ? cause.message : String(cause),
8+ });
9+
10+export const attempt = <A>(operation: string, evaluate: () => A): Effect.Effect<A, EngineOperationError> =>
11+ Effect.try({
12+ try: evaluate,
13+ catch: (cause) => operationError(operation, cause),
14+ });
controller/src/modules/engines/downloads/download-store.ts+2−13
@@ -1,7 +1,8 @@
11 import { Effect, Option, Schema } from "effect";
22 import { openSqliteDatabase } from "../../../stores/sqlite";
3−import { EngineOperationError } from "../engine-spec";
3+import type { EngineOperationError } from "../engine-spec";
44 import type { ModelDownload } from "../types";
5+import { attempt, operationError } from "./download-operation";
56
67 const DownloadFileSchema = Schema.Struct({
78 path: Schema.String,
@@ -27,18 +28,6 @@ const ModelDownloadSchema = Schema.Struct({ · 19 unmodified lines
2728 error: Schema.NullOr(Schema.String),
2829 });
2930
30−const operationError = (operation: string, cause: unknown): EngineOperationError =>
31− new EngineOperationError({
32− operation,
33− message: cause instanceof Error ? cause.message : String(cause),
34− });
35−
36−const attempt = <A>(operation: string, evaluate: () => A): Effect.Effect<A, EngineOperationError> =>
37− Effect.try({
38− try: evaluate,
39− catch: (cause) => operationError(operation, cause),
40− });
41−
4231 const decodeDownload = (value: unknown): Effect.Effect<ModelDownload, EngineOperationError> =>
4332 attempt("parse-download-record", () =>
4433 typeof value === "string" ? JSON.parse(value) : value,