@@ -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
0 commit comments