Skip to content

Commit

Permalink
TGALoader: Bubble parsing errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mugen87 committed Jul 26, 2023
1 parent 78acb52 commit a6adadc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
20 changes: 10 additions & 10 deletions examples/jsm/loaders/TGALoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TGALoader extends DataTextureLoader {
case TGA_TYPE_RLE_INDEXED:
if ( header.colormap_length > 256 || header.colormap_size !== 24 || header.colormap_type !== 1 ) {

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

}

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

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

}

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

case TGA_TYPE_NO_DATA:
console.error( 'THREE.TGALoader: No data.' );
throw new Error.error( 'THREE.TGALoader: No data.' );

// Invalid type ?

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

}

// check image width and height

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

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

}

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

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

}

Expand Down Expand Up @@ -365,7 +365,7 @@ class TGALoader extends DataTextureLoader {
break;

default:
console.error( 'THREE.TGALoader: Format not supported.' );
throw new Error( 'THREE.TGALoader: Format not supported.' );
break;

}
Expand All @@ -391,7 +391,7 @@ class TGALoader extends DataTextureLoader {
break;

default:
console.error( 'THREE.TGALoader: Format not supported.' );
throw new Error( 'THREE.TGALoader: Format not supported.' );
break;

}
Expand Down Expand Up @@ -422,7 +422,7 @@ class TGALoader extends DataTextureLoader {
TGA_ORIGIN_UL = 0x02,
TGA_ORIGIN_UR = 0x03;

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

let offset = 0;

Expand Down Expand Up @@ -450,7 +450,7 @@ class TGALoader extends DataTextureLoader {

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

console.error( 'THREE.TGALoader: No data.' );
throw new Error( 'THREE.TGALoader: No data.' );

}

Expand Down
23 changes: 21 additions & 2 deletions src/loaders/DataTextureLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,28 @@ class DataTextureLoader extends Loader {
loader.setWithCredentials( scope.withCredentials );
loader.load( url, function ( buffer ) {

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

if ( ! texData ) return onError();
try {

texData = scope.parse( buffer );

} catch ( error ) {

if ( onError !== undefined ) {

onError( error );

} else {

console.error( error );
return;

}

}

if ( ! texData ) return onError(); // TODO: Remove this when all loaders properly throw errors

if ( texData.image !== undefined ) {

Expand Down

0 comments on commit a6adadc

Please sign in to comment.