Skip to content

Commit 33487fb

Browse files
authored
TGALoader: Bubble parsing errors. (mrdoob#26497)
* TGALoader: Bubble parsing errors. * TGALoader: Clean up.
1 parent 78acb52 commit 33487fb

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

examples/jsm/loaders/TGALoader.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class TGALoader extends DataTextureLoader {
2525
case TGA_TYPE_RLE_INDEXED:
2626
if ( header.colormap_length > 256 || header.colormap_size !== 24 || header.colormap_type !== 1 ) {
2727

28-
console.error( 'THREE.TGALoader: Invalid type colormap data for indexed type.' );
28+
throw new Error( 'THREE.TGALoader: Invalid type colormap data for indexed type.' );
2929

3030
}
3131

@@ -39,7 +39,7 @@ class TGALoader extends DataTextureLoader {
3939
case TGA_TYPE_RLE_GREY:
4040
if ( header.colormap_type ) {
4141

42-
console.error( 'THREE.TGALoader: Invalid type colormap data for colormap type.' );
42+
throw new Error( 'THREE.TGALoader: Invalid type colormap data for colormap type.' );
4343

4444
}
4545

@@ -48,20 +48,20 @@ class TGALoader extends DataTextureLoader {
4848
// What the need of a file without data ?
4949

5050
case TGA_TYPE_NO_DATA:
51-
console.error( 'THREE.TGALoader: No data.' );
51+
throw new Error( 'THREE.TGALoader: No data.' );
5252

5353
// Invalid type ?
5454

5555
default:
56-
console.error( 'THREE.TGALoader: Invalid type "%s".', header.image_type );
56+
throw new Error( 'THREE.TGALoader: Invalid type ' + header.image_type );
5757

5858
}
5959

6060
// check image width and height
6161

6262
if ( header.width <= 0 || header.height <= 0 ) {
6363

64-
console.error( 'THREE.TGALoader: Invalid image size.' );
64+
throw new Error( 'THREE.TGALoader: Invalid image size.' );
6565

6666
}
6767

@@ -70,7 +70,7 @@ class TGALoader extends DataTextureLoader {
7070
if ( header.pixel_size !== 8 && header.pixel_size !== 16 &&
7171
header.pixel_size !== 24 && header.pixel_size !== 32 ) {
7272

73-
console.error( 'THREE.TGALoader: Invalid pixel size "%s".', header.pixel_size );
73+
throw new Error( 'THREE.TGALoader: Invalid pixel size ' + header.pixel_size );
7474

7575
}
7676

@@ -365,7 +365,7 @@ class TGALoader extends DataTextureLoader {
365365
break;
366366

367367
default:
368-
console.error( 'THREE.TGALoader: Format not supported.' );
368+
throw new Error( 'THREE.TGALoader: Format not supported.' );
369369
break;
370370

371371
}
@@ -391,7 +391,7 @@ class TGALoader extends DataTextureLoader {
391391
break;
392392

393393
default:
394-
console.error( 'THREE.TGALoader: Format not supported.' );
394+
throw new Error( 'THREE.TGALoader: Format not supported.' );
395395
break;
396396

397397
}
@@ -422,7 +422,7 @@ class TGALoader extends DataTextureLoader {
422422
TGA_ORIGIN_UL = 0x02,
423423
TGA_ORIGIN_UR = 0x03;
424424

425-
if ( buffer.length < 19 ) console.error( 'THREE.TGALoader: Not enough data to contain header.' );
425+
if ( buffer.length < 19 ) throw new Error( 'THREE.TGALoader: Not enough data to contain header.' );
426426

427427
let offset = 0;
428428

@@ -450,7 +450,7 @@ class TGALoader extends DataTextureLoader {
450450

451451
if ( header.id_length + offset > buffer.length ) {
452452

453-
console.error( 'THREE.TGALoader: No data.' );
453+
throw new Error( 'THREE.TGALoader: No data.' );
454454

455455
}
456456

src/loaders/DataTextureLoader.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,28 @@ class DataTextureLoader extends Loader {
3030
loader.setWithCredentials( scope.withCredentials );
3131
loader.load( url, function ( buffer ) {
3232

33-
const texData = scope.parse( buffer ); // TODO: Use try/catch here and throw errors in derived loaders, see #26412
33+
let texData;
3434

35-
if ( ! texData ) return onError();
35+
try {
36+
37+
texData = scope.parse( buffer );
38+
39+
} catch ( error ) {
40+
41+
if ( onError !== undefined ) {
42+
43+
onError( error );
44+
45+
} else {
46+
47+
console.error( error );
48+
return;
49+
50+
}
51+
52+
}
53+
54+
if ( ! texData ) return onError(); // TODO: Remove this when all loaders properly throw errors
3655

3756
if ( texData.image !== undefined ) {
3857

0 commit comments

Comments
 (0)