Handle file path errors and better promise handling

This commit is contained in:
Sean Lilley 2017-04-05 10:44:28 -04:00
parent 72c20eb6ee
commit 965402c535
3 changed files with 105 additions and 74 deletions

View File

@ -130,4 +130,7 @@ console.time('Total');
convert(objPath, gltfPath, options)
.then(function() {
console.timeEnd('Total');
})
.catch(function(error) {
console.log(error.message);
});

View File

@ -48,6 +48,7 @@ var defaultLogger = function(message) {
* @param {Logger} [options.logger] A callback function for handling logged messages. Defaults to console.log.
*/
function convert(objPath, gltfPath, options) {
return new Promise(function(resolve, reject) {
options = defaultValue(options, {});
var binary = defaultValue(options.binary, false);
var separate = defaultValue(options.separate, false);
@ -73,13 +74,22 @@ function convert(objPath, gltfPath, options) {
throw new DeveloperError('gltfPath is required');
}
var objExtension = path.extname(objPath).toLowerCase();
if (objExtension !== '.obj') {
throw new DeveloperError('Invalid obj path "' + objPath + '"');
}
var extension = path.extname(gltfPath).toLowerCase();
if (extension !== '.gltf' && extension !== '.glb') {
throw new DeveloperError('Invalid gltf path "' + gltfPath + '"');
}
var basePath = path.dirname(gltfPath);
var modelName = path.basename(gltfPath, path.extname(gltfPath));
var extension = path.extname(gltfPath);
if (extension === '.glb') {
binary = true;
if (bypassPipeline) {
options.logger('--bypassPipeline does not convert to binary glTF, saving as .gltf');
logger('--bypassPipeline does not convert to binary glTF, saving as .gltf');
extension = '.gltf';
}
}
@ -120,6 +130,9 @@ function convert(objPath, gltfPath, options) {
} else {
return GltfPipeline.processJSONToDisk(gltf, gltfPath, pipelineOptions);
}
})
.then(resolve)
.catch(reject);
});
}

View File

@ -1,12 +1,19 @@
'use strict';
var Cesium = require('cesium');
var GltfPipeline = require('gltf-pipeline').Pipeline;
var path = require('path');
var convert = require('../../lib/convert');
var writeUris = require('../../lib/writeUris');
var DeveloperError = Cesium.DeveloperError;
var objPath = 'specs/data/box-textured/box-textured.obj';
var gltfPath = 'specs/data/box-textured/box-textured.gltf';
var glbPath = 'specs/data/box-textured/box-textured.glb';
var objPathInvalid = 'invalid/';
var gltfPathInvalid = 'invalid/model.invalid';
var objPathNonExistent = 'specs/data/non-existent.obj';
var gltfPathNonExistent = 'specs/data/non-existent.gltf';
var objExternalResourcesPath = 'specs/data/box-external-resources/box-external-resources.obj';
@ -116,7 +123,7 @@ describe('convert', function() {
}), done).toResolve();
});
it('Uses a custom logger', function(done) {
it('uses a custom logger', function(done) {
var spy = spyOn(GltfPipeline, 'processJSONToDisk');
var logCount = 0;
var options = {
@ -131,15 +138,23 @@ describe('convert', function() {
}), done).toResolve();
});
it('throws if objPath is undefined', function() {
expect(function() {
convert(undefined, gltfPath);
}).toThrowDeveloperError();
it('rejects if objPath is undefined', function(done) {
expect(convert(undefined, gltfPath), done).toRejectWith(DeveloperError);
});
it('throws if gltfPath is undefined', function() {
expect(function() {
convert(objPath, undefined);
}).toThrowDeveloperError();
it('rejects if gltfPath is undefined', function(done) {
expect(convert(objPath, undefined), done).toRejectWith(DeveloperError);
});
it('rejects if obj path is invalid', function(done) {
expect(convert(objPathInvalid, gltfPath), done).toRejectWith(DeveloperError);
});
it('rejects if gltf path is invalid', function(done) {
expect(convert(objPath, gltfPathInvalid), done).toRejectWith(DeveloperError);
});
it('rejects if obj path does not exist', function(done) {
expect(convert(objPathNonExistent, gltfPath), done).toRejectWith(Error);
});
});