From d4bb1095c0738c84ec410937870004ef36196828 Mon Sep 17 00:00:00 2001 From: Joachim Van Herwegen Date: Thu, 15 Jul 2021 11:43:05 +0200 Subject: [PATCH] fix: Let AclInitializer throw internal error in case of problems --- src/init/AclInitializer.ts | 10 +++++++++- test/unit/init/AclInitializer.test.ts | 7 +++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/init/AclInitializer.ts b/src/init/AclInitializer.ts index 439072008..44e86afb4 100644 --- a/src/init/AclInitializer.ts +++ b/src/init/AclInitializer.ts @@ -5,6 +5,8 @@ import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifie import { getLoggerFor } from '../logging/LogUtil'; import type { ResourceStore } from '../storage/ResourceStore'; import { TEXT_TURTLE } from '../util/ContentTypes'; +import { createErrorMessage } from '../util/errors/ErrorUtil'; +import { InternalServerError } from '../util/errors/InternalServerError'; import { ensureTrailingSlash, joinFilePath } from '../util/PathUtil'; import { Initializer } from './Initializer'; @@ -43,7 +45,13 @@ export class AclInitializer extends Initializer { } else { this.logger.debug(`Installing root ACL document at ${rootAcl.path}`); const aclDocument = createReadStream(this.aclPath, 'utf8'); - await this.store.setRepresentation(rootAcl, new BasicRepresentation(aclDocument, rootAcl, TEXT_TURTLE)); + try { + await this.store.setRepresentation(rootAcl, new BasicRepresentation(aclDocument, rootAcl, TEXT_TURTLE)); + } catch (error: unknown) { + const msg = `There was an issue initializing the root .acl resource: ${createErrorMessage(error)}`; + this.logger.error(msg); + throw new InternalServerError(msg, { cause: error }); + } } } } diff --git a/test/unit/init/AclInitializer.test.ts b/test/unit/init/AclInitializer.test.ts index f7cb5d8d8..bf08f09c2 100644 --- a/test/unit/init/AclInitializer.test.ts +++ b/test/unit/init/AclInitializer.test.ts @@ -3,6 +3,7 @@ import { AclInitializer } from '../../../src/init/AclInitializer'; import type { AuxiliaryIdentifierStrategy } from '../../../src/ldp/auxiliary/AuxiliaryIdentifierStrategy'; import { BasicRepresentation } from '../../../src/ldp/representation/BasicRepresentation'; import type { ResourceStore } from '../../../src/storage/ResourceStore'; +import { InternalServerError } from '../../../src/util/errors/InternalServerError'; import { joinFilePath } from '../../../src/util/PathUtil'; const createReadStream = jest.spyOn(fs, 'createReadStream').mockReturnValue('file contents' as any); @@ -72,9 +73,11 @@ describe('AclInitializer', (): void => { }); it('errors when the root ACL check errors.', async(): Promise => { - store.resourceExists.mockRejectedValueOnce(new Error('Fatal')); + store.setRepresentation.mockRejectedValueOnce(new Error('Fatal')); const initializer = new AclInitializer({ baseUrl, store, aclStrategy }); - await expect(initializer.handle()).rejects.toThrow('Fatal'); + const prom = initializer.handle(); + await expect(prom).rejects.toThrow('There was an issue initializing the root .acl resource: Fatal'); + await expect(prom).rejects.toThrow(InternalServerError); }); });