|
1 | 1 | import fs from 'node:fs/promises';
|
2 | 2 | import path from 'node:path';
|
3 | 3 | import * as url from 'node:url';
|
4 |
| -import SQliteDB from 'better-sqlite3'; |
| 4 | +import SQliteDB, { SqliteError } from 'better-sqlite3'; |
5 | 5 | import {
|
6 | 6 | Kysely,
|
7 | 7 | FileMigrationProvider,
|
@@ -78,17 +78,30 @@ export class Store {
|
78 | 78 | experimentId: string;
|
79 | 79 | createdAt: Date;
|
80 | 80 | }) {
|
81 |
| - let result = await this.#db |
82 |
| - .insertInto('run') |
83 |
| - .values({ |
84 |
| - runId, |
85 |
| - experimentId, |
86 |
| - createdAt: createdAt.toISOString(), |
87 |
| - status: 'running', |
88 |
| - }) |
89 |
| - .returning(['runId', 'experimentId']) |
90 |
| - .executeTakeFirstOrThrow(); |
91 |
| - return { runId: result.runId, experimentId: result.experimentId }; |
| 81 | + try { |
| 82 | + let result = await this.#db |
| 83 | + .insertInto('run') |
| 84 | + .values({ |
| 85 | + runId, |
| 86 | + experimentId, |
| 87 | + createdAt: createdAt.toISOString(), |
| 88 | + status: 'running', |
| 89 | + }) |
| 90 | + .returning(['runId', 'experimentId']) |
| 91 | + .executeTakeFirstOrThrow(); |
| 92 | + return { runId: result.runId, experimentId: result.experimentId }; |
| 93 | + } catch (e) { |
| 94 | + if ( |
| 95 | + e instanceof SqliteError && |
| 96 | + e.code === 'SQLITE_CONSTRAINT_PRIMARYKEY' |
| 97 | + ) { |
| 98 | + throw new StoreError( |
| 99 | + `run "${runId}" already exists for experiment "${experimentId}".`, |
| 100 | + 'RUN_EXISTS' |
| 101 | + ); |
| 102 | + } |
| 103 | + throw e; |
| 104 | + } |
92 | 105 | }
|
93 | 106 |
|
94 | 107 | async getRun(experimentId: string, runId: string) {
|
@@ -295,3 +308,15 @@ export type Log = {
|
295 | 308 | clientDate?: Date;
|
296 | 309 | values: JsonObject;
|
297 | 310 | };
|
| 311 | + |
| 312 | +type StoreErrorCode = 'RUN_EXISTS'; |
| 313 | +export class StoreError extends Error { |
| 314 | + code: StoreErrorCode; |
| 315 | + cause?: Error; |
| 316 | + constructor(message: string, code: StoreErrorCode, cause?: Error) { |
| 317 | + super(message); |
| 318 | + this.name = 'StoreError'; |
| 319 | + this.code = code; |
| 320 | + this.cause = cause; |
| 321 | + } |
| 322 | +} |
0 commit comments