More accurate error messages

This commit is contained in:
Sean Lilley 2017-12-21 22:19:52 -05:00
parent 57a95a6623
commit e2db0ac59d
5 changed files with 35 additions and 16 deletions

View File

@ -220,18 +220,23 @@ function loadMaterialTexture(material, name, texturePath, textureOptions, mtlDir
var shallowPath = path.resolve(path.join(mtlDirectory, path.basename(texturePath))); var shallowPath = path.resolve(path.join(mtlDirectory, path.basename(texturePath)));
if (options.secure && outsideDirectory(texturePath, mtlDirectory)) { if (options.secure && outsideDirectory(texturePath, mtlDirectory)) {
// Try looking for the texture in the same directory as the obj // Try looking for the texture in the same directory as the obj
options.logger('Texture file is outside of the mtl directory and the secure flag is true. Attempting to read the texture file from within the obj directory instead.');
texturePromise = loadTexture(shallowPath, textureOptions) texturePromise = loadTexture(shallowPath, textureOptions)
.catch(function() { .catch(function(error) {
options.logger('Could not read texture file at ' + texturePath + ' because it is outside of the mtl directory and the secure flag is true. This texture will be ignored.'); options.logger(error.message);
options.logger('Could not read texture file at ' + shallowPath + '. This texture will be ignored');
}); });
} else { } else {
texturePromise = loadTexture(texturePath, textureOptions) texturePromise = loadTexture(texturePath, textureOptions)
.catch(function() { .catch(function(error) {
// Try looking for the texture in the same directory as the obj // Try looking for the texture in the same directory as the obj
options.logger(error.message);
options.logger('Could not read texture file at ' + texturePath + '. Attempting to read the texture file from within the obj directory instead.');
return loadTexture(shallowPath, textureOptions); return loadTexture(shallowPath, textureOptions);
}) })
.catch(function() { .catch(function(error) {
options.logger('Could not read texture file at ' + texturePath + '. This texture will be ignored.'); options.logger(error.message);
options.logger('Could not read texture file at ' + shallowPath + '. This texture will be ignored.');
}); });
} }
texturePromiseMap[texturePath] = texturePromise; texturePromiseMap[texturePath] = texturePromise;

View File

@ -519,25 +519,30 @@ function loadMtls(mtlPaths, objPath, options) {
var shallowPath = path.resolve(path.join(objDirectory, path.basename(mtlPath))); var shallowPath = path.resolve(path.join(objDirectory, path.basename(mtlPath)));
if (options.secure && outsideDirectory(mtlPath, objDirectory)) { if (options.secure && outsideDirectory(mtlPath, objDirectory)) {
// Try looking for the .mtl in the same directory as the obj // Try looking for the .mtl in the same directory as the obj
options.logger('The material file is outside of the obj directory and the secure flag is true. Attempting to read the material file from within the obj directory instead.');
return loadMtl(shallowPath, options) return loadMtl(shallowPath, options)
.then(function(materialsInMtl) { .then(function(materialsInMtl) {
materials = materials.concat(materialsInMtl); materials = materials.concat(materialsInMtl);
}) })
.catch(function() { .catch(function(error) {
options.logger('Could not read mtl file at ' + mtlPath + ' because it is outside of the obj directory and the secure flag is true. Using default material instead.'); options.logger(error.message);
options.logger('Could not read material file at ' + shallowPath + '. Using default material instead.');
}); });
} }
return loadMtl(mtlPath, options) return loadMtl(mtlPath, options)
.catch(function() { .catch(function(error) {
// Try looking for the .mtl in the same directory as the obj // Try looking for the .mtl in the same directory as the obj
options.logger(error.message);
options.logger('Could not read material file at ' + mtlPath + '. Attempting to read the material file from within the obj directory instead.');
return loadMtl(shallowPath, options); return loadMtl(shallowPath, options);
}) })
.then(function(materialsInMtl) { .then(function(materialsInMtl) {
materials = materials.concat(materialsInMtl); materials = materials.concat(materialsInMtl);
}) })
.catch(function() { .catch(function(error) {
options.logger('Could not read mtl file at ' + mtlPath + '. Using default material instead.'); options.logger(error.message);
options.logger('Could not read material file at ' + shallowPath + '. Using default material instead.');
}); });
}, {concurrency : 10}) }, {concurrency : 10})
.then(function() { .then(function() {

View File

@ -209,7 +209,9 @@ describe('loadMtl', function() {
var material = materials[0]; var material = materials[0];
var baseColorTexture = material.pbrMetallicRoughness.baseColorTexture; var baseColorTexture = material.pbrMetallicRoughness.baseColorTexture;
expect(baseColorTexture).toBeUndefined(); expect(baseColorTexture).toBeUndefined();
expect(spy.calls.argsFor(0)[0].indexOf('Could not read texture file') >= 0).toBe(true); expect(spy.calls.argsFor(0)[0].indexOf('Texture file is outside of the mtl directory and the secure flag is true. Attempting to read the texture file from within the obj directory instead') >= 0).toBe(true);
expect(spy.calls.argsFor(1)[0].indexOf('ENOENT') >= 0).toBe(true);
expect(spy.calls.argsFor(2)[0].indexOf('Could not read texture file') >= 0).toBe(true);
}), done).toResolve(); }), done).toResolve();
}); });

View File

@ -298,7 +298,10 @@ describe('loadObj', function() {
expect(loadObj(objMissingMtllibPath, options) expect(loadObj(objMissingMtllibPath, options)
.then(function(data) { .then(function(data) {
expect(data.materials.length).toBe(0); expect(data.materials.length).toBe(0);
expect(spy.calls.argsFor(0)[0].indexOf('Could not read mtl file') >= 0).toBe(true); expect(spy.calls.argsFor(0)[0].indexOf('ENOENT') >= 0).toBe(true);
expect(spy.calls.argsFor(1)[0].indexOf('Attempting to read the material file from within the obj directory instead.') >= 0).toBe(true);
expect(spy.calls.argsFor(2)[0].indexOf('ENOENT') >= 0).toBe(true);
expect(spy.calls.argsFor(3)[0].indexOf('Could not read material file') >= 0).toBe(true);
}), done).toResolve(); }), done).toResolve();
}); });
@ -324,8 +327,9 @@ describe('loadObj', function() {
expect(loadObj(objExternalResourcesPath, options) expect(loadObj(objExternalResourcesPath, options)
.then(function(data) { .then(function(data) {
expect(data.materials.length).toBe(1); // obj references 2 materials, one of which is outside the input directory expect(data.materials.length).toBe(1); // obj references 2 materials, one of which is outside the input directory
expect(spy.calls.argsFor(0)[0].indexOf('Could not read mtl file') >= 0).toBe(true); expect(spy.calls.argsFor(0)[0].indexOf('The material file is outside of the obj directory and the secure flag is true. Attempting to read the material file from within the obj directory instead.') >= 0).toBe(true);
expect(spy.calls.argsFor(1)[0].indexOf('Could not read texture file') >= 0).toBe(true); expect(spy.calls.argsFor(1)[0].indexOf('ENOENT') >= 0).toBe(true);
expect(spy.calls.argsFor(2)[0].indexOf('Could not read material file') >= 0).toBe(true);
}), done).toResolve(); }), done).toResolve();
}); });
@ -371,7 +375,10 @@ describe('loadObj', function() {
.then(function(data) { .then(function(data) {
var baseColorTexture = data.materials[0].pbrMetallicRoughness.baseColorTexture; var baseColorTexture = data.materials[0].pbrMetallicRoughness.baseColorTexture;
expect(baseColorTexture).toBeUndefined(); expect(baseColorTexture).toBeUndefined();
expect(spy.calls.argsFor(0)[0].indexOf('Could not read texture file') >= 0).toBe(true); expect(spy.calls.argsFor(0)[0].indexOf('ENOENT') >= 0).toBe(true);
expect(spy.calls.argsFor(1)[0].indexOf('Attempting to read the texture file from within the obj directory instead.') >= 0).toBe(true);
expect(spy.calls.argsFor(2)[0].indexOf('ENOENT') >= 0).toBe(true);
expect(spy.calls.argsFor(3)[0].indexOf('Could not read texture file') >= 0).toBe(true);
}), done).toResolve(); }), done).toResolve();
}); });

View File

@ -124,7 +124,7 @@ describe('obj2gltf', function() {
}; };
expect(obj2gltf(missingMtllibObjPath, options) expect(obj2gltf(missingMtllibObjPath, options)
.then(function() { .then(function() {
expect(lastMessage.indexOf('Could not read mtl file') >= 0).toBe(true); expect(lastMessage.indexOf('Could not read material file') >= 0).toBe(true);
}), done).toResolve(); }), done).toResolve();
}); });