From 4286919b0635fdbe56b80493a7d6201cefce4f6d Mon Sep 17 00:00:00 2001 From: "Matt.Gooding" <869053+mgooding@users.noreply.github.com> Date: Mon, 21 Mar 2022 11:00:56 -0400 Subject: [PATCH 1/3] Better error when hubAccess is undefined --- core/backend/src/IModelHost.ts | 10 +++++++++- core/backend/src/test/HubMock.ts | 6 +++++- core/backend/src/test/IModelHost.test.ts | 5 +++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/core/backend/src/IModelHost.ts b/core/backend/src/IModelHost.ts index a0e847bbff54..674073694ba2 100644 --- a/core/backend/src/IModelHost.ts +++ b/core/backend/src/IModelHost.ts @@ -322,7 +322,15 @@ export class IModelHost { /** Provides access to the IModelHub for this IModelHost * @beta */ - public static get hubAccess(): BackendHubAccess { return this._hubAccess; } + public static get hubAccess(): BackendHubAccess { + // Strictly speaking, _hubAccess should be marked as possibly undefined since it's not needed for Snapshot iModels. + // However, a decision was made to not provide that type annotation so callers aren't forced to constantly check for + // something that's required in all other workflows. + // This check is here to provide a better error message when hubAccess is inadvertently undefined. + if (this._hubAccess === undefined) + throw new IModelError(IModelStatus.BadRequest, "IModelHost.hubAccess is undefined. Specify an implementation in your IModelHostConfiguration"); + return this._hubAccess; + } private static _isValid = false; /** Returns true if IModelHost is started. */ diff --git a/core/backend/src/test/HubMock.ts b/core/backend/src/test/HubMock.ts index 84704355b9cb..278b2f5da9d8 100644 --- a/core/backend/src/test/HubMock.ts +++ b/core/backend/src/test/HubMock.ts @@ -80,7 +80,11 @@ export class HubMock { this.mockRoot = join(KnownTestLocations.outputDir, "HubMock", mockName); IModelJsFs.recursiveMkDirSync(this.mockRoot); IModelJsFs.purgeDirSync(this.mockRoot); - this._saveHubAccess = IModelHost.hubAccess; + try { + this._saveHubAccess = IModelHost.hubAccess; + } catch (error) { + // See note in IModelHost.hubAccess. hubAccess can in fact be undefined, but that is not annotated in type system. + } IModelHost.setHubAccess(this); HubMock._iTwinId = Guid.createValue(); // all iModels for this test get the same "iTwinId" } diff --git a/core/backend/src/test/IModelHost.test.ts b/core/backend/src/test/IModelHost.test.ts index c0d064fdd6ca..0834b1d80780 100644 --- a/core/backend/src/test/IModelHost.test.ts +++ b/core/backend/src/test/IModelHost.test.ts @@ -192,4 +192,9 @@ describe("IModelHost", () => { }); + it("should throw if hubAccess is undefined and getter is called", async () => { + await IModelHost.startup(); + expect(() => IModelHost.hubAccess).throws("IModelHost.hubAccess is undefined. Specify an implementation in your IModelHostConfiguration"); + }); + }); From ee0a0a3c92d7f83c27c2afb61b0b6610ad9a90b7 Mon Sep 17 00:00:00 2001 From: "Matt.Gooding" <869053+mgooding@users.noreply.github.com> Date: Mon, 21 Mar 2022 11:18:10 -0400 Subject: [PATCH 2/3] rush change --- .../error-on-no-hub-access_2022-03-21-15-17.json | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 common/changes/@itwin/core-backend/error-on-no-hub-access_2022-03-21-15-17.json diff --git a/common/changes/@itwin/core-backend/error-on-no-hub-access_2022-03-21-15-17.json b/common/changes/@itwin/core-backend/error-on-no-hub-access_2022-03-21-15-17.json new file mode 100644 index 000000000000..cdc12470b55d --- /dev/null +++ b/common/changes/@itwin/core-backend/error-on-no-hub-access_2022-03-21-15-17.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@itwin/core-backend", + "comment": "Improved error message for undefined IModelHost.hubAccess", + "type": "none" + } + ], + "packageName": "@itwin/core-backend" +} \ No newline at end of file From ec88a1e49aacec829a98e0a6080944a7abfc23b5 Mon Sep 17 00:00:00 2001 From: "Matt.Gooding" <869053+mgooding@users.noreply.github.com> Date: Mon, 21 Mar 2022 11:42:16 -0400 Subject: [PATCH 3/3] Document IModelHost.hubAccess throwing if backing value is undefined --- core/backend/src/IModelHost.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/core/backend/src/IModelHost.ts b/core/backend/src/IModelHost.ts index 674073694ba2..e2db6152dfa2 100644 --- a/core/backend/src/IModelHost.ts +++ b/core/backend/src/IModelHost.ts @@ -321,6 +321,7 @@ export class IModelHost { /** Provides access to the IModelHub for this IModelHost * @beta + * @note If [[IModelHostConfiguration.hubAccess]] was undefined when initializing this class, accessing this property will throw an error. */ public static get hubAccess(): BackendHubAccess { // Strictly speaking, _hubAccess should be marked as possibly undefined since it's not needed for Snapshot iModels.