diff --git a/@here/harp-mapview/lib/MapView.ts b/@here/harp-mapview/lib/MapView.ts index f5dd0bc42d..0b8b66f08a 100644 --- a/@here/harp-mapview/lib/MapView.ts +++ b/@here/harp-mapview/lib/MapView.ts @@ -2148,7 +2148,10 @@ export class MapView extends EventDispatcher { this.update(); } catch (error) { - logger.error(`Failed to connect to datasource ${dataSource.name}: ${error.message}`); + // error is a string if a promise was rejected. + logger.error( + `Failed to connect to datasource ${dataSource.name}: ${error.message ?? error}` + ); this.m_failedDataSources.add(dataSource.name); this.dispatchEvent({ diff --git a/@here/harp-vectortile-datasource/lib/VectorTileDataSource.ts b/@here/harp-vectortile-datasource/lib/VectorTileDataSource.ts index f6eb0af20f..40cbfa2c79 100644 --- a/@here/harp-vectortile-datasource/lib/VectorTileDataSource.ts +++ b/@here/harp-vectortile-datasource/lib/VectorTileDataSource.ts @@ -331,7 +331,9 @@ export class VectorTileDataSource extends TileDataSource { try { await super.connect(); } catch (error) { + // error is a string if the promise was rejected. if ( + error.message && WorkerServiceProtocol.isUnknownServiceError(error) && !missingOmvDecoderServiceInfoEmitted ) { @@ -341,7 +343,7 @@ export class VectorTileDataSource extends TileDataSource { ); missingOmvDecoderServiceInfoEmitted = true; } - throw error; + throw typeof error === "string" ? new Error(error) : error; } this.configureDecoder(undefined, this.m_decoderOptions); } diff --git a/@here/harp-vectortile-datasource/test/OmvDataSourceTest.ts b/@here/harp-vectortile-datasource/test/OmvDataSourceTest.ts index 6a0b0b1dd9..54b67e6578 100644 --- a/@here/harp-vectortile-datasource/test/OmvDataSourceTest.ts +++ b/@here/harp-vectortile-datasource/test/OmvDataSourceTest.ts @@ -13,7 +13,12 @@ import { TileKey } from "@here/harp-geoutils"; import { DataProvider } from "@here/harp-mapview-decoder"; import { GeoJsonTiler } from "@here/harp-mapview-decoder/index-worker"; import { silenceLoggingAroundFunction } from "@here/harp-test-utils"; -import { assert } from "chai"; +import * as chai from "chai"; +const { expect, assert } = chai; +// Install chai-as-promised plugin to support promise assertions like: +// expect(promise).to.eventually.be.rejectedWith() +import * as chai_as_promised from "chai-as-promised"; +chai.use(chai_as_promised); import * as sinon from "sinon"; import { @@ -211,4 +216,18 @@ describe("DataProviders", function () { assert.isFalse(markTilesDirty.called); }); + + it("connect returns promise rejection as error", function () { + const rejectMsg = "connection failed"; + const mockDataProvider = new MockDataProvider(); + + sinon.stub(mockDataProvider, "connect").callsFake(() => { + return Promise.reject(rejectMsg); + }); + const omvDataSource = new VectorTileDataSource({ + decoder: new VectorTileDecoder(), + dataProvider: mockDataProvider + }); + expect(omvDataSource.connect()).to.eventually.throw("Error", rejectMsg); + }); });