From 66e3441e0eccc2bea2d608fd02f1c4795f1c02f6 Mon Sep 17 00:00:00 2001 From: Kalervo Kujala Date: Tue, 24 Jan 2012 22:04:59 +0200 Subject: [PATCH 1/2] Change throws to errors. --- src/canvas.js | 4 ++-- src/core.js | 10 +++++----- src/jpx.js | 16 ++++++++-------- src/obj.js | 4 ++-- src/stream.js | 22 +++++++++++++--------- src/util.js | 16 ++++++++-------- src/worker.js | 8 ++++---- 7 files changed, 42 insertions(+), 38 deletions(-) diff --git a/src/canvas.js b/src/canvas.js index 5ef900861..d0b0064f6 100644 --- a/src/canvas.js +++ b/src/canvas.js @@ -548,7 +548,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { var fontObj = this.objs.get(fontRefName).fontObj; if (!fontObj) { - throw 'Can\'t find font for ' + fontRefName; + error('Can\'t find font for ' + fontRefName); } var name = fontObj.loadedName || 'sans-serif'; @@ -866,7 +866,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { } else if (IR[0] == 'RadialAxial' || IR[0] == 'Dummy') { var pattern = Pattern.shadingFromIR(this.ctx, IR); } else { - throw 'Unkown IR type'; + error('Unkown IR type ' + IR[0]); } return pattern; }, diff --git a/src/core.js b/src/core.js index 7a9f3ee03..df38c086f 100644 --- a/src/core.js +++ b/src/core.js @@ -410,14 +410,14 @@ var Page = (function PageClosure() { if (callback) callback(e); else - throw e; + error(e); } }.bind(this), function pageDisplayReadPromiseError(reason) { if (callback) callback(reason); else - throw reason; + error(reason); } ); } @@ -620,7 +620,7 @@ var PDFDoc = (function PDFDocClosure() { if (!globalScope.PDFJS.disableWorker && typeof Worker !== 'undefined') { var workerSrc = PDFJS.workerSrc; if (typeof workerSrc === 'undefined') { - throw 'No PDFJS.workerSrc specified'; + error('No PDFJS.workerSrc specified'); } try { @@ -716,7 +716,7 @@ var PDFDoc = (function PDFDocClosure() { }); break; default: - throw 'Got unkown object type ' + type; + error('Got unkown object type ' + type); } }, this); @@ -737,7 +737,7 @@ var PDFDoc = (function PDFDocClosure() { if (page.displayReadyPromise) page.displayReadyPromise.reject(data.error); else - throw data.error; + error(data.error); }, this); messageHandler.on('jpeg_decode', function(data, promise) { diff --git a/src/jpx.js b/src/jpx.js index 61a8f4487..c212c6fd0 100644 --- a/src/jpx.js +++ b/src/jpx.js @@ -1052,7 +1052,7 @@ var JpxImage = (function JpxImageClosure() { } r = 0; } - throw 'Out of packets'; + error('Out of packets'); }; } function ResolutionLayerComponentPositionIterator(context) { @@ -1091,7 +1091,7 @@ var JpxImage = (function JpxImageClosure() { } l = 0; } - throw 'Out of packets'; + error('Out of packets'); }; } function buildPackets(context) { @@ -1187,7 +1187,7 @@ var JpxImage = (function JpxImageClosure() { new ResolutionLayerComponentPositionIterator(context); break; default: - throw 'Unsupported progression order'; + error('Unsupported progression order ' + progressionOrder); } } function parseTilePackets(context, data, offset, dataLength) { @@ -1589,7 +1589,7 @@ var JpxImage = (function JpxImageClosure() { if (lbox == 0) lbox = length - position + headerSize; if (lbox < headerSize) - throw 'Invalid box field size'; + error('Invalid box field size'); var dataLength = lbox - headerSize; var jumpDataLength = true; switch (tbox) { @@ -1675,7 +1675,7 @@ var JpxImage = (function JpxImageClosure() { scalarExpounded = true; break; default: - throw 'Invalid SQcd value'; + error('Invalid SQcd value ' + sqcd); } qcd.noQuantization = spqcdSize == 8; qcd.scalarExpounded = scalarExpounded; @@ -1728,7 +1728,7 @@ var JpxImage = (function JpxImageClosure() { scalarExpounded = true; break; default: - throw 'Invalid SQcd value'; + error('Invalid SQcd value ' + sqcd); } qcc.noQuantization = spqcdSize == 8; qcc.scalarExpounded = scalarExpounded; @@ -1795,7 +1795,7 @@ var JpxImage = (function JpxImageClosure() { cod.terminationOnEachCodingPass || cod.verticalyStripe || cod.predictableTermination || cod.segmentationSymbolUsed) - throw 'Unsupported COD options: ' + uneval(cod); + error('Unsupported COD options: ' + uneval(cod)); if (context.mainHeader) context.COD = cod; @@ -1840,7 +1840,7 @@ var JpxImage = (function JpxImageClosure() { // skipping content break; default: - throw 'Unknown codestream code: ' + code.toString(16); + error('Unknown codestream code: ' + code.toString(16)); } position += length; } diff --git a/src/obj.js b/src/obj.js index ef7932546..5b87fec13 100644 --- a/src/obj.js +++ b/src/obj.js @@ -574,7 +574,7 @@ var XRef = (function XRefClosure() { var stream, parser; if (e.uncompressed) { if (e.gen != gen) - throw ('inconsistent generation in XRef'); + error('inconsistent generation in XRef'); stream = this.stream.makeSubStream(e.offset); parser = new Parser(new Lexer(stream), true, this); var obj1 = parser.getObj(); @@ -703,7 +703,7 @@ var PDFObjects = (function PDFObjectsClosure() { // If there isn't an object yet or the object isn't resolved, then the // data isn't ready yet! if (!obj || !obj.isResolved) { - throw 'Requesting object that isn\'t resolved yet ' + objId; + error('Requesting object that isn\'t resolved yet ' + objId); return null; } else { return obj.data; diff --git a/src/stream.js b/src/stream.js index 610a54d38..c7b7c83e6 100644 --- a/src/stream.js +++ b/src/stream.js @@ -821,15 +821,19 @@ var JpegStream = (function JpegStreamClosure() { JpegStream.prototype.ensureBuffer = function jpegStreamEnsureBuffer(req) { if (this.bufferLength) return; - var jpegImage = new JpegImage(); - if (this.colorTransform != -1) - jpegImage.colorTransform = this.colorTransform; - jpegImage.parse(this.bytes); - var width = jpegImage.width; - var height = jpegImage.height; - var data = jpegImage.getData(width, height); - this.buffer = data; - this.bufferLength = data.length; + try { + var jpegImage = new JpegImage(); + if (this.colorTransform != -1) + jpegImage.colorTransform = this.colorTransform; + jpegImage.parse(this.bytes); + var width = jpegImage.width; + var height = jpegImage.height; + var data = jpegImage.getData(width, height); + this.buffer = data; + this.bufferLength = data.length; + } catch (e) { + error(e); + } }; JpegStream.prototype.getIR = function jpegStreamGetIR() { return bytesToString(this.bytes); diff --git a/src/util.js b/src/util.js index 99b422296..759908e9e 100644 --- a/src/util.js +++ b/src/util.js @@ -255,8 +255,8 @@ var Promise = (function PromiseClosure() { return; } if (this._data !== EMPTY_PROMISE) { - throw 'Promise ' + this.name + - ': Cannot set the data of a promise twice'; + error('Promise ' + this.name + + ': Cannot set the data of a promise twice'); } this._data = value; this.hasData = true; @@ -268,7 +268,7 @@ var Promise = (function PromiseClosure() { get data() { if (this._data === EMPTY_PROMISE) { - throw 'Promise ' + this.name + ': Cannot get data that isn\'t set'; + error('Promise ' + this.name + ': Cannot get data that isn\'t set'); } return this._data; }, @@ -283,10 +283,10 @@ var Promise = (function PromiseClosure() { resolve: function promiseResolve(data) { if (this.isResolved) { - throw 'A Promise can be resolved only once ' + this.name; + error('A Promise can be resolved only once ' + this.name); } if (this.isRejected) { - throw 'The Promise was already rejected ' + this.name; + error('The Promise was already rejected ' + this.name); } this.isResolved = true; @@ -300,10 +300,10 @@ var Promise = (function PromiseClosure() { reject: function proimseReject(reason) { if (this.isRejected) { - throw 'A Promise can be rejected only once ' + this.name; + error('A Promise can be rejected only once ' + this.name); } if (this.isResolved) { - throw 'The Promise was already resolved ' + this.name; + error('The Promise was already resolved ' + this.name); } this.isRejected = true; @@ -317,7 +317,7 @@ var Promise = (function PromiseClosure() { then: function promiseThen(callback, errback) { if (!callback) { - throw 'Requiring callback' + this.name; + error('Requiring callback' + this.name); } // If the promise is already resolved, call the callback directly. diff --git a/src/worker.js b/src/worker.js index 4d9dd1bb6..f9777d7df 100644 --- a/src/worker.js +++ b/src/worker.js @@ -26,7 +26,7 @@ function MessageHandler(name, comObj) { delete callbacks[callbackId]; callback(data.data); } else { - throw 'Cannot resolve callback ' + callbackId; + error('Cannot resolve callback ' + callbackId); } } else if (data.action in ah) { var action = ah[data.action]; @@ -44,7 +44,7 @@ function MessageHandler(name, comObj) { action[0].call(action[1], data.data); } } else { - throw 'Unkown action from worker: ' + data.action; + error('Unkown action from worker: ' + data.action); } }; } @@ -53,7 +53,7 @@ MessageHandler.prototype = { on: function messageHandlerOn(actionName, handler, scope) { var ah = this.actionHandler; if (ah[actionName]) { - throw 'There is already an actionName called "' + actionName + '"'; + error('There is already an actionName called "' + actionName + '"'); } ah[actionName] = [handler, scope]; }, @@ -217,7 +217,7 @@ var workerConsole = { timeEnd: function timeEnd(name) { var time = consoleTimer[name]; if (time == null) { - throw 'Unkown timer name ' + name; + error('Unkown timer name ' + name); } this.log('Timer:', name, Date.now() - time); } From e3a3ec6f2e3b57bd310ed8cb0d663946dae9b2d2 Mon Sep 17 00:00:00 2001 From: Kalervo Kujala Date: Sun, 29 Jan 2012 22:25:06 +0200 Subject: [PATCH 2/2] Use JPX and JPEG in error messages. Also throw in workerConsole. --- src/jpx.js | 16 ++++++++-------- src/stream.js | 2 +- src/worker.js | 1 + 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/jpx.js b/src/jpx.js index c212c6fd0..a15c3db54 100644 --- a/src/jpx.js +++ b/src/jpx.js @@ -1052,7 +1052,7 @@ var JpxImage = (function JpxImageClosure() { } r = 0; } - error('Out of packets'); + error('JPX error: Out of packets'); }; } function ResolutionLayerComponentPositionIterator(context) { @@ -1091,7 +1091,7 @@ var JpxImage = (function JpxImageClosure() { } l = 0; } - error('Out of packets'); + error('JPX error: Out of packets'); }; } function buildPackets(context) { @@ -1187,7 +1187,7 @@ var JpxImage = (function JpxImageClosure() { new ResolutionLayerComponentPositionIterator(context); break; default: - error('Unsupported progression order ' + progressionOrder); + error('JPX error: Unsupported progression order ' + progressionOrder); } } function parseTilePackets(context, data, offset, dataLength) { @@ -1589,7 +1589,7 @@ var JpxImage = (function JpxImageClosure() { if (lbox == 0) lbox = length - position + headerSize; if (lbox < headerSize) - error('Invalid box field size'); + error('JPX error: Invalid box field size'); var dataLength = lbox - headerSize; var jumpDataLength = true; switch (tbox) { @@ -1675,7 +1675,7 @@ var JpxImage = (function JpxImageClosure() { scalarExpounded = true; break; default: - error('Invalid SQcd value ' + sqcd); + error('JPX error: Invalid SQcd value ' + sqcd); } qcd.noQuantization = spqcdSize == 8; qcd.scalarExpounded = scalarExpounded; @@ -1728,7 +1728,7 @@ var JpxImage = (function JpxImageClosure() { scalarExpounded = true; break; default: - error('Invalid SQcd value ' + sqcd); + error('JPX error: Invalid SQcd value ' + sqcd); } qcc.noQuantization = spqcdSize == 8; qcc.scalarExpounded = scalarExpounded; @@ -1795,7 +1795,7 @@ var JpxImage = (function JpxImageClosure() { cod.terminationOnEachCodingPass || cod.verticalyStripe || cod.predictableTermination || cod.segmentationSymbolUsed) - error('Unsupported COD options: ' + uneval(cod)); + error('JPX error: Unsupported COD options: ' + uneval(cod)); if (context.mainHeader) context.COD = cod; @@ -1840,7 +1840,7 @@ var JpxImage = (function JpxImageClosure() { // skipping content break; default: - error('Unknown codestream code: ' + code.toString(16)); + error('JPX error: Unknown codestream code: ' + code.toString(16)); } position += length; } diff --git a/src/stream.js b/src/stream.js index c7b7c83e6..fc163171f 100644 --- a/src/stream.js +++ b/src/stream.js @@ -832,7 +832,7 @@ var JpegStream = (function JpegStreamClosure() { this.buffer = data; this.bufferLength = data.length; } catch (e) { - error(e); + error('JPEG error: ' + e); } }; JpegStream.prototype.getIR = function jpegStreamGetIR() { diff --git a/src/worker.js b/src/worker.js index f9777d7df..b81ff0540 100644 --- a/src/worker.js +++ b/src/worker.js @@ -208,6 +208,7 @@ var workerConsole = { action: 'console_error', data: args }); + throw 'pdf.js execution error'; }, time: function time(name) {