Skip to content

Commit 7128c7b

Browse files
committed
2 parents 88ecf8d + 3d85bd4 commit 7128c7b

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

backend/src/api/db.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ const setAPIMetadata = async (
3232
editor: Editor,
3333
machine_name: string,
3434
os: string
35-
): Promise<void | ApiMetadataAlreadySet> => {
35+
): Promise<void | ApiMetadataAlreadySet | ApiNotFound> => {
3636
const keyRes = await client.query({
3737
text: "SELECT user_id, metadata_set FROM api_key WHERE value = $1 FOR UPDATE",
3838
values: [api_key],
3939
});
4040

4141
if (keyRes.rowCount === 0) {
42-
throw new ApiNotFound();
42+
return new ApiNotFound();
4343
}
4444

4545
const { user_id, metadata_set } = keyRes.rows[0];
4646

47-
if (metadata_set) throw new ApiMetadataAlreadySet();
47+
if (metadata_set) return new ApiMetadataAlreadySet();
4848

4949
// Create the machine
5050
const machineQ = await client.query({
@@ -78,7 +78,7 @@ const getUserByApi = async (
7878
values: [user_id],
7979
});
8080

81-
if (userRes.rowCount === 0) throw new ApiNotFound();
81+
if (userRes.rowCount === 0) return new ApiNotFound();
8282
const { name, email } = userRes.rows[0];
8383

8484
const user: User = {

backend/src/api/service.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ import * as db from "./db";
22
import crypto from "node:crypto";
33
import { ApiKey, Editor } from "./validation";
44
import { UserNotFound } from "@/auth/errors";
5-
import { ApiGenerationAfterAttempts } from "./errors";
5+
import {
6+
ApiGenerationAfterAttempts,
7+
ApiMetadataAlreadySet,
8+
ApiNotFound,
9+
} from "./errors";
610
import { DBError } from "@/pool";
711
import pool from "@/pool";
12+
import { AppError } from "@/utils/error";
813

914
const ATTEMPTS = 3;
1015

@@ -37,11 +42,21 @@ const setApiMetadata = async (
3742
editor: Editor,
3843
machine_name: string,
3944
os: string
40-
): Promise<void | UserNotFound> => {
45+
): Promise<void | UserNotFound | ApiMetadataAlreadySet | ApiNotFound> => {
4146
const client = await pool.connect();
4247
try {
4348
await client.query("begin transaction");
44-
await db.setAPIMetadata(client, api_key, editor, machine_name, os);
49+
const res = await db.setAPIMetadata(
50+
client,
51+
api_key,
52+
editor,
53+
machine_name,
54+
os
55+
);
56+
if (res instanceof AppError) {
57+
await client.query("rollback");
58+
return res;
59+
}
4560
await client.query("commit");
4661
} catch (err) {
4762
await client.query("rollback");

0 commit comments

Comments
 (0)