Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide an error message when IModelHost.hubAccess is undefined #3391

Merged
merged 3 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@itwin/core-backend",
"comment": "Improved error message for undefined IModelHost.hubAccess",
"type": "none"
}
],
"packageName": "@itwin/core-backend"
}
10 changes: 9 additions & 1 deletion core/backend/src/IModelHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
pmconne marked this conversation as resolved.
Show resolved Hide resolved
// 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. */
Expand Down
6 changes: 5 additions & 1 deletion core/backend/src/test/HubMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
5 changes: 5 additions & 0 deletions core/backend/src/test/IModelHost.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});

});