fix: Let AclInitializer throw internal error in case of problems

This commit is contained in:
Joachim Van Herwegen
2021-07-15 11:43:05 +02:00
parent 13c8317d31
commit d4bb1095c0
2 changed files with 14 additions and 3 deletions

View File

@@ -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 });
}
}
}
}

View File

@@ -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<void> => {
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);
});
});