Skip to content

Commit

Permalink
fix: correctly set the tile state on error
Browse files Browse the repository at this point in the history
  • Loading branch information
dnlkoch committed Sep 19, 2024
1 parent 13892d9 commit 019ae24
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/parser/SHOGunApplicationUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import OlSourceXYZ, {
import OlTile from 'ol/Tile';
import OlTileGrid from 'ol/tilegrid/TileGrid';
import OlTileGridWMTS from 'ol/tilegrid/WMTS';
import OlTileState from 'ol/TileState';
import OlView, { ViewOptions as OlViewOptions } from 'ol/View';

import {
Expand All @@ -54,8 +55,6 @@ class SHOGunApplicationUtil<

private readonly client: SHOGunAPIClient | undefined;

private readonly objectUrls: Record<string, string> = {};

constructor(opts?: SHOGunApplicationUtilOpts) {
// TODO Default client?
this.client = opts?.client;
Expand Down Expand Up @@ -733,9 +732,6 @@ class SHOGunApplicationUtil<

private async bearerTokenLoadFunction(imageTile: OlTile | OlImage, src: string, useBearerToken: boolean = false) {
try {
if (this.objectUrls[src]) {
URL.revokeObjectURL(this.objectUrls[src]);
}
const response = await fetch(src, {
headers: useBearerToken ? {
...getBearerTokenHeader(this.client?.getKeycloak())
Expand All @@ -745,14 +741,23 @@ class SHOGunApplicationUtil<
const imageElement = (imageTile as OlImageTile).getImage() as HTMLImageElement;

if (!response.ok) {
imageElement.src = src;
return;
throw new Error(`No successful response: ${response.status}`);
}

const blob = await response.blob();

if (!/image\/*/.test(blob.type)) {
throw new Error(`Unexpected format ${blob.type} returned`);
}

this.objectUrls[src] = URL.createObjectURL(await response.blob());
imageElement.src = this.objectUrls[src];
imageElement.src = URL.createObjectURL(blob);

imageElement.onload = () => {
URL.revokeObjectURL(src);
};
} catch (error) {
Logger.error('Error while loading WMS: ', error);
Logger.error('Error while loading an image tile: ', error);
(imageTile as OlImageTile).setState(OlTileState.ERROR);
}
}
}
Expand Down

0 comments on commit 019ae24

Please sign in to comment.