Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/poor-cameras-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lightmill/log-server': patch
---

fix crashes when trying to create a run that already exists
8 changes: 3 additions & 5 deletions packages/log-server/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* eslint-disable @typescript-eslint/no-namespace */
import express from 'express';
import { zodiosContext } from '@zodios/express';
import { LogFilter, Store } from './store.js';
import { LogFilter, Store, StoreError } from './store.js';
import session from 'cookie-session';
import { SqliteError } from 'better-sqlite3';
import { NextFunction, Request, RequestHandler, Response } from 'express';
import { api } from './api.js';
import { csvExportStream, jsonExportStream } from './export.js';
Expand Down Expand Up @@ -149,11 +148,10 @@ export function createLogServer({
},
});
} catch (e) {
if (e instanceof SqliteError && e.code === 'SQLITE_CONSTRAINT') {
if (e instanceof StoreError && e.code === 'RUN_EXISTS') {
res.status(400).json({
status: 'error',
message:
'Could not add run, probably because a run with that ID already exists for this experiment',
message: e.message,
});
return;
}
Expand Down
49 changes: 37 additions & 12 deletions packages/log-server/src/store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import * as url from 'node:url';
import SQliteDB from 'better-sqlite3';
import SQliteDB, { SqliteError } from 'better-sqlite3';
import {
Kysely,
FileMigrationProvider,
Expand Down Expand Up @@ -78,17 +78,30 @@ export class Store {
experimentId: string;
createdAt: Date;
}) {
let result = await this.#db
.insertInto('run')
.values({
runId,
experimentId,
createdAt: createdAt.toISOString(),
status: 'running',
})
.returning(['runId', 'experimentId'])
.executeTakeFirstOrThrow();
return { runId: result.runId, experimentId: result.experimentId };
try {
let result = await this.#db
.insertInto('run')
.values({
runId,
experimentId,
createdAt: createdAt.toISOString(),
status: 'running',
})
.returning(['runId', 'experimentId'])
.executeTakeFirstOrThrow();
return { runId: result.runId, experimentId: result.experimentId };
} catch (e) {
if (
e instanceof SqliteError &&
e.code === 'SQLITE_CONSTRAINT_PRIMARYKEY'
) {
throw new StoreError(
`run "${runId}" already exists for experiment "${experimentId}".`,
'RUN_EXISTS'
);
}
throw e;
}
}

async getRun(experimentId: string, runId: string) {
Expand Down Expand Up @@ -295,3 +308,15 @@ export type Log = {
clientDate?: Date;
values: JsonObject;
};

type StoreErrorCode = 'RUN_EXISTS';
export class StoreError extends Error {
code: StoreErrorCode;
cause?: Error;
constructor(message: string, code: StoreErrorCode, cause?: Error) {
super(message);
this.name = 'StoreError';
this.code = code;
this.cause = cause;
}
}