diff --git a/src/modules/cards/application/useCases/queries/GetUrlStatusForMyLibraryUseCase.ts b/src/modules/cards/application/useCases/queries/GetUrlStatusForMyLibraryUseCase.ts index b15663f5..13bcdf1b 100644 --- a/src/modules/cards/application/useCases/queries/GetUrlStatusForMyLibraryUseCase.ts +++ b/src/modules/cards/application/useCases/queries/GetUrlStatusForMyLibraryUseCase.ts @@ -12,6 +12,7 @@ import { CuratorId } from '../../../domain/value-objects/CuratorId'; import { URL } from '../../../domain/value-objects/URL'; import { CollectionId } from '../../../domain/value-objects/CollectionId'; import { CollectionDTO, UrlCard } from '@semble/types'; +import { AuthenticationError } from '../../../../../shared/core/AuthenticationError'; export interface GetUrlStatusForMyLibraryQuery { url: string; @@ -33,7 +34,7 @@ export class GetUrlStatusForMyLibraryUseCase extends BaseUseCase< GetUrlStatusForMyLibraryQuery, Result< GetUrlStatusForMyLibraryResult, - ValidationError | AppError.UnexpectedError + ValidationError | AuthenticationError | AppError.UnexpectedError > > { constructor( @@ -52,7 +53,7 @@ export class GetUrlStatusForMyLibraryUseCase extends BaseUseCase< ): Promise< Result< GetUrlStatusForMyLibraryResult, - ValidationError | AppError.UnexpectedError + ValidationError | AuthenticationError | AppError.UnexpectedError > > { try { @@ -101,6 +102,10 @@ export class GetUrlStatusForMyLibraryUseCase extends BaseUseCase< ); if (authorProfileResult.isErr()) { + // Propagate authentication errors + if (authorProfileResult.error instanceof AuthenticationError) { + return err(authorProfileResult.error); + } return err( AppError.UnexpectedError.create(authorProfileResult.error), ); @@ -161,6 +166,10 @@ export class GetUrlStatusForMyLibraryUseCase extends BaseUseCase< fullCollection.authorId.value, ); if (authorProfileResult.isErr()) { + // Propagate authentication errors + if (authorProfileResult.error instanceof AuthenticationError) { + throw authorProfileResult.error; + } throw new Error( `Failed to fetch author profile: ${authorProfileResult.error.message}`, ); @@ -186,6 +195,10 @@ export class GetUrlStatusForMyLibraryUseCase extends BaseUseCase< }), ); } catch (error) { + // Propagate authentication errors + if (error instanceof AuthenticationError) { + return err(error); + } return err(AppError.UnexpectedError.create(error)); } } diff --git a/src/modules/cards/infrastructure/http/controllers/GetUrlStatusForMyLibraryController.ts b/src/modules/cards/infrastructure/http/controllers/GetUrlStatusForMyLibraryController.ts index c008b445..34918813 100644 --- a/src/modules/cards/infrastructure/http/controllers/GetUrlStatusForMyLibraryController.ts +++ b/src/modules/cards/infrastructure/http/controllers/GetUrlStatusForMyLibraryController.ts @@ -2,6 +2,7 @@ import { Controller } from '../../../../../shared/infrastructure/http/Controller import { Response } from 'express'; import { GetUrlStatusForMyLibraryUseCase } from '../../../application/useCases/queries/GetUrlStatusForMyLibraryUseCase'; import { AuthenticatedRequest } from '../../../../../shared/infrastructure/http/middleware/AuthMiddleware'; +import { AuthenticationError } from '../../../../../shared/core/AuthenticationError'; export class GetUrlStatusForMyLibraryController extends Controller { constructor( @@ -29,12 +30,16 @@ export class GetUrlStatusForMyLibraryController extends Controller { }); if (result.isErr()) { + // Check if the error is an authentication error + if (result.error instanceof AuthenticationError) { + return this.unauthorized(res, result.error.message); + } return this.fail(res, result.error); } return this.ok(res, result.value); } catch (error: any) { - return this.fail(res, error); + return this.handleError(res, error); } } }