Skip to content

Commit

Permalink
Added better error handling which fixed #91
Browse files Browse the repository at this point in the history
  • Loading branch information
lindell committed Aug 6, 2016
1 parent d3c232a commit d777d8d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 21 deletions.
39 changes: 20 additions & 19 deletions src/JsBarcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import linearizeEncodings from './help/linearizeEncodings.js';
import fixOptions from './help/fixOptions.js';
import getRenderProperties from './help/getRenderProperties.js';

// Exceptions
import ErrorHandler from './exceptions/ErrorHandler.js';
import {InvalidInputException} from './exceptions/exceptions.js';

// Default values
import defaults from './defaults/defaults.js';

Expand All @@ -27,6 +31,7 @@ let JsBarcode = function(element, text, options){
api._renderProperties = getRenderProperties(element);
api._encodings = [];
api._options = defaults;
api._errorHandler = new ErrorHandler(api);

// If text is set, use the simple syntax (render the barcode directly)
if(typeof text !== "undefined"){
Expand All @@ -36,9 +41,7 @@ let JsBarcode = function(element, text, options){
options.format = autoSelectBarcode();
}

api.options(options);
api[options.format](text, options);
api.render();
api.options(options)[options.format](text, options).render();
}

return api;
Expand All @@ -59,14 +62,17 @@ function registerBarcode(barcodes, name){
API.prototype[name] =
API.prototype[name.toUpperCase()] =
API.prototype[name.toLowerCase()] =
function(text, options){
var newOptions = merge(this._options, options);
var Encoder = barcodes[name];
var encoded = encode(text, Encoder, newOptions);
this._encodings.push(encoded);

return this;
};
function(text, options){
var api = this;
return api._errorHandler.wrapBarcodeCall(function(){
var newOptions = merge(api._options, options);
var Encoder = barcodes[name];
var encoded = encode(text, Encoder, newOptions);
api._encodings.push(encoded);

return api;
});
};
}

// encode() handles the Encoder call and builds the binary string to be rendered
Expand All @@ -79,12 +85,7 @@ function encode(text, Encoder, options){
// If the input is not valid for the encoder, throw error.
// If the valid callback option is set, call it instead of throwing error
if(!encoder.valid()){
if(options.valid === defaults.valid){
throw new Error('"' + text + '" is not a valid input.');
}
else{
options.valid(false);
}
throw new InvalidInputException(encoder.constructor.name, text);
}

// Make a request for the binary data (and other infromation) that should be rendered
Expand Down Expand Up @@ -164,11 +165,11 @@ API.prototype.render = function(){
render(this._renderProperties, this._encodings, this._options);
}

this._options.valid(true);

return this;
};

API.prototype._defaults = defaults;

// Prepares the encodings and calls the renderer
function render(renderProperties, encodings, options){
encodings = linearizeEncodings(encodings);
Expand Down
39 changes: 39 additions & 0 deletions src/exceptions/ErrorHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*eslint no-console: 0 */

class ErrorHandler{
constructor(api){
this.api = api;
}

handleCatch(e){
// If babel supported extending of Error in a correct way instanceof would be used here
if(e.name === "InvalidInputException"){
if(this.api._options.valid !== this.api._defaults.valid){
this.api._options.valid(false);
}
else{
throw e.message;
}
}
else{
throw e;
}

this.api.render = function(){};
}

wrapBarcodeCall(func){
try{
var result = func(...arguments);
this.api._options.valid(true);
return result;
}
catch(e){
this.handleCatch(e);

return this.api;
}
}
}

export default ErrorHandler;
29 changes: 29 additions & 0 deletions src/exceptions/exceptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class InvalidInputException extends Error{
constructor(symbology, input) {
super();
this.name = "InvalidInputException";

this.symbology = symbology;
this.input = input;

this.message = '"' + this.input + '" is not a valid input for ' + this.symbology;
}
}

class InvalidElementException extends Error{
constructor() {
super();
this.name = "InvalidElementException";
this.message = "Not supported type to render on";
}
}

class NoElementException extends Error{
constructor() {
super();
this.name = "NoElementException";
this.message = "No element to render on.";
}
}

export {InvalidInputException, InvalidElementException, NoElementException};
6 changes: 4 additions & 2 deletions src/help/getRenderProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import getOptionsFromElement from "./getOptionsFromElement.js";
import {getRendererClass} from "../renderers";

import {InvalidElementException, NoElementException} from "../exceptions/exceptions.js";

// Takes an element and returns an object with information about how
// it should be rendered
// This could also return an array with these objects
Expand Down Expand Up @@ -57,15 +59,15 @@ function getRenderProperties(element){
};
}
else{
throw new Error("Not supported type to render on.");
throw new InvalidElementException();
}
}


function querySelectedRenderProperties(string){
var selector = document.querySelectorAll(string);
if(selector.length === 0){
throw new Error("No element found");
throw new NoElementException();
}
else{
let returnArray = [];
Expand Down

0 comments on commit d777d8d

Please sign in to comment.