Skip to content

Commit 1bb8151

Browse files
committed
add better error catching
1 parent 99a0a0c commit 1bb8151

File tree

2 files changed

+62
-30
lines changed

2 files changed

+62
-30
lines changed

src/api/routes/linkry.ts

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -105,51 +105,87 @@ const linkryRoutes: FastifyPluginAsync = async (fastify, _options) => {
105105
},
106106
},
107107
async (request, reply) => {
108+
const username = request.username!;
109+
const tableName = genericConfig.LinkryDynamoTableName;
110+
111+
// First try-catch: Fetch owner records
112+
let ownerRecords;
108113
try {
109-
const username = request.username!;
110-
const tableName = genericConfig.LinkryDynamoTableName;
111-
const ownerRecords = await fetchOwnerRecords(
114+
ownerRecords = await fetchOwnerRecords(
112115
username,
113116
tableName,
114117
fastify.dynamoClient,
115118
);
116-
const ownedUniqueSlugs = extractUniqueSlugs(ownerRecords);
117-
const ownedLinksWithGroups = await getGroupsForSlugs(
119+
} catch (error) {
120+
request.log.error(
121+
`Failed to fetch owner records: ${error instanceof Error ? error.toString() : "Unknown error"}`,
122+
);
123+
throw new DatabaseFetchError({
124+
message: "Failed to fetch owner records from Dynamo table.",
125+
});
126+
}
127+
128+
const ownedUniqueSlugs = extractUniqueSlugs(ownerRecords);
129+
130+
// Second try-catch: Get groups for slugs
131+
let ownedLinksWithGroups;
132+
try {
133+
ownedLinksWithGroups = await getGroupsForSlugs(
118134
ownedUniqueSlugs,
119135
ownerRecords,
120136
tableName,
121137
fastify.dynamoClient,
122138
);
123-
let delegatedLinks;
124-
let userGroups: string[];
125-
if (request.userRoles!.has(AppRoles.LINKS_ADMIN)) {
139+
} catch (error) {
140+
request.log.error(
141+
`Failed to get groups for slugs: ${error instanceof Error ? error.toString() : "Unknown error"}`,
142+
);
143+
throw new DatabaseFetchError({
144+
message: "Failed to get groups for links from Dynamo table.",
145+
});
146+
}
147+
148+
// Third try-catch paths: Get delegated links based on user role
149+
let delegatedLinks;
150+
if (request.userRoles!.has(AppRoles.LINKS_ADMIN)) {
151+
// Admin path
152+
try {
126153
delegatedLinks = (
127154
await getAllLinks(tableName, fastify.dynamoClient)
128155
).filter((x) => x.owner !== username);
129-
} else {
130-
userGroups = getFilteredUserGroups(request);
156+
} catch (error) {
157+
request.log.error(
158+
`Failed to get all links for admin: ${error instanceof Error ? error.toString() : "Unknown error"}`,
159+
);
160+
throw new DatabaseFetchError({
161+
message: "Failed to get all links for admin from Dynamo table.",
162+
});
163+
}
164+
} else {
165+
// Regular user path
166+
const userGroups = getFilteredUserGroups(request);
167+
try {
131168
delegatedLinks = await getDelegatedLinks(
132169
userGroups,
133170
ownedUniqueSlugs,
134171
tableName,
135172
fastify.dynamoClient,
136173
);
174+
} catch (error) {
175+
request.log.error(
176+
`Failed to get delegated links: ${error instanceof Error ? error.toString() : "Unknown error"}`,
177+
);
178+
throw new DatabaseFetchError({
179+
message: "Failed to get delegated links from Dynamo table.",
180+
});
137181
}
138-
reply.code(200).send({
139-
ownedLinks: ownedLinksWithGroups,
140-
delegatedLinks: delegatedLinks,
141-
});
142-
} catch (error) {
143-
if (error instanceof BaseError) {
144-
throw error;
145-
}
146-
request.log.error(
147-
`Failed to get from DynamoDB: ${error instanceof Error ? error.toString() : "Unknown error"}`,
148-
);
149-
throw new DatabaseFetchError({
150-
message: "Failed to get Links from Dynamo table.",
151-
});
152182
}
183+
184+
// Send the response
185+
reply.code(200).send({
186+
ownedLinks: ownedLinksWithGroups,
187+
delegatedLinks: delegatedLinks,
188+
});
153189
},
154190
);
155191

tests/unit/linkry.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
GetItemCommand,
66
ScanCommand,
77
QueryCommand,
8+
TransactWriteItemsCommand,
89
} from "@aws-sdk/client-dynamodb";
910
import { mockClient } from "aws-sdk-client-mock";
1011
import init from "../../src/api/index.js";
@@ -129,7 +130,7 @@ test("Happy path: Create a new linkry redirect", async () => {
129130
Items: [], // Simulate no existing records for the slug
130131
});
131132

132-
ddbMock.on(PutItemCommand).resolves({}); // Simulate successful insertion
133+
ddbMock.on(TransactWriteItemsCommand).resolves({}); // Simulate successful insertion
133134

134135
// Define the request payload
135136
const payload = {
@@ -148,11 +149,6 @@ test("Happy path: Create a new linkry redirect", async () => {
148149

149150
// Assert the response status code
150151
expect(response.statusCode).toBe(201);
151-
152-
// Assert the response body
153-
expect(response.body).toStrictEqual({
154-
id: "acm-test-slug",
155-
});
156152
});
157153

158154
// const testAdminJwt = createJwt(undefined, "LINKS_ADMIN");

0 commit comments

Comments
 (0)