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
18import { EngineOperationError } from "../engine-spec";18import { EngineOperationError } from "../engine-spec";
19import type { DownloadFileInfo, DownloadStatus, ModelDownload } from "../types";19import type { DownloadFileInfo, DownloadStatus, ModelDownload } from "../types";
20import type { DownloadStore } from "./download-store";20import type { DownloadStore } from "./download-store";
21import { attempt, operationError } from "./download-operation";
21import {22import {
22 buildHuggingFaceFileList,23 buildHuggingFaceFileList,
23 fetchEffect,24 fetchEffect,
@@ -108,18 +109,6 @@ type ActiveDownload = { · 84 unmodified lines
108 109
109const toTimestamp = (): string => new Date().toISOString();110const toTimestamp = (): string => new Date().toISOString();
110 111
111const operationError = (operation: string, cause: unknown): EngineOperationError =>
112 new EngineOperationError({
113 operation,
114 message: cause instanceof Error ? cause.message : String(cause),
115 });
116
117const attempt = <A>(operation: string, evaluate: () => A): Effect.Effect<A, EngineOperationError> =>
118 Effect.try({
119 try: evaluate,
120 catch: (cause) => operationError(operation, cause),
121 });
122
123const closeWriter = (112const closeWriter = (
124 writer: ReturnType<typeof createWriteStream>,113 writer: ReturnType<typeof createWriteStream>,
125): Effect.Effect<void, EngineOperationError> =>114): Effect.Effect<void, EngineOperationError> =>
controller/src/modules/engines/downloads/download-operation.ts+14−0
@@ -0,0 +1,14 @@
1import { Effect } from "effect";
2import { EngineOperationError } from "../engine-spec";
3
4export const operationError = (operation: string, cause: unknown): EngineOperationError =>
5 new EngineOperationError({
6 operation,
7 message: cause instanceof Error ? cause.message : String(cause),
8 });
9
10export 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 @@
1import { Effect, Option, Schema } from "effect";1import { Effect, Option, Schema } from "effect";
2import { openSqliteDatabase } from "../../../stores/sqlite";2import { openSqliteDatabase } from "../../../stores/sqlite";
3import { EngineOperationError } from "../engine-spec";3import type { EngineOperationError } from "../engine-spec";
4import type { ModelDownload } from "../types";4import type { ModelDownload } from "../types";
5import { attempt, operationError } from "./download-operation";
5 6
6const DownloadFileSchema = Schema.Struct({7const DownloadFileSchema = Schema.Struct({
7 path: Schema.String,8 path: Schema.String,
@@ -27,18 +28,6 @@ const ModelDownloadSchema = Schema.Struct({ · 19 unmodified lines
27 error: Schema.NullOr(Schema.String),28 error: Schema.NullOr(Schema.String),
28});29});
29 30
30const operationError = (operation: string, cause: unknown): EngineOperationError =>
31 new EngineOperationError({
32 operation,
33 message: cause instanceof Error ? cause.message : String(cause),
34 });
35
36const attempt = <A>(operation: string, evaluate: () => A): Effect.Effect<A, EngineOperationError> =>
37 Effect.try({
38 try: evaluate,
39 catch: (cause) => operationError(operation, cause),
40 });
41
42const decodeDownload = (value: unknown): Effect.Effect<ModelDownload, EngineOperationError> =>31const decodeDownload = (value: unknown): Effect.Effect<ModelDownload, EngineOperationError> =>
43 attempt("parse-download-record", () =>32 attempt("parse-download-record", () =>
44 typeof value === "string" ? JSON.parse(value) : value,33 typeof value === "string" ? JSON.parse(value) : value,