Skip to content

Commit

Permalink
add errorHandler module
Browse files Browse the repository at this point in the history
  • Loading branch information
therewasaguy committed Nov 24, 2015
1 parent 04288f2 commit d6c2102
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 63 deletions.
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ module.exports = function(grunt) {
'sndcore': 'src/sndcore',
'master': 'src/master',
'helpers': 'src/helpers',
'errorHandler': 'src/errorHandler',
'soundfile': 'src/soundfile',
'amplitude': 'src/amplitude',
'fft': 'src/fft',
Expand Down
14 changes: 10 additions & 4 deletions examples/loadSound_404ErrorHandling/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

function setup() {
createCanvas(800,200);
soundFile = loadSound('http://files/beatbx.mp3', soundReady, soundError);
soundFile2 = loadSound('../files/beatbx.mp3', soundReady, soundError);
loadSound('http://badURL.mp3', soundReady, soundError);
loadSound('../badPath.mp3', soundReady, soundError);

loadSound('http://badURL.mp3', soundReady);
loadSound('../badPath.mp3', soundReady);
}

function soundReady(){
function soundReady(soundFile){
soundFile.play();
}

function soundError(e) {
console.log(e);
console.log('New error:');
console.log('- name: ' + e.name);
console.log('- message: ' + e.message);
console.log('- stack: ' + e.stack);
console.log('- failed path: ' + e.failedPath);
}
78 changes: 49 additions & 29 deletions lib/p5.sound.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,44 @@ helpers = function () {
return o;
};
}(master);
var errorHandler;
errorHandler = function () {
'use strict';
/**
* Helper function to generate an error
* with a custom stack trace that points to the sketch
* and removes other parts of the stack trace.
*
* @private
*
* @param {String} name custom error name
* @param {String} errorTrace custom error trace
* @param {String} failedPath path to the file that failed to load
* @property {String} name custom error name
* @property {String} message custom error message
* @property {String} stack trace the error back to a line in the user's sketch.
* Note: this edits out stack trace within p5.js and p5.sound.
* @property {String} originalStack unedited, original stack trace
* @property {String} failedPath path to the file that failed to load
* @return {Error} returns a custom Error object
*/
var CustomError = function (name, errorTrace, failedPath) {
var err = new Error();
var tempStack, splitStack;
err.name = name;
err.originalStack = err.stack + errorTrace;
tempStack = err.stack + errorTrace;
err.failedPath = failedPath;
// only print the part of the stack trace that refers to the user code:
var splitStack = tempStack.split('\n');
splitStack = splitStack.filter(function (ln) {
return !ln.match(/(p5.|native code|globalInit)/g);
});
err.stack = splitStack.join('\n');
return err;
};
return CustomError;
}();
var panner;
panner = function () {
'use strict';
Expand Down Expand Up @@ -694,6 +732,7 @@ panner = function () {
var soundfile;
soundfile = function () {
'use strict';
var CustomError = errorHandler;
var p5sound = master;
var ac = p5sound.audiocontext;
/**
Expand Down Expand Up @@ -885,7 +924,7 @@ soundfile = function () {
}
}, // error decoding buffer. "e" is undefined in Chrome 11/22/2015
function (e) {
var err = generateError(errorTrace);
var err = new CustomError('decodeAudioData', errorTrace, self.url);
var msg = 'AudioContext error at decodeAudioData for ' + self.url;
if (errorCallback) {
err.msg = msg;
Expand All @@ -895,8 +934,8 @@ soundfile = function () {
}
});
} else {
var err = generateError(errorTrace);
var msg = 'Unable to load ' + self.url + ' ' + request.status + ' (' + request.statusText + ')';
var err = new CustomError('loadSound', errorTrace, self.url);
var msg = 'Unable to load ' + self.url + '. The request status was: ' + request.status + ' (' + request.statusText + ')';
if (errorCallback) {
err.message = msg;
errorCallback(err);
Expand All @@ -907,8 +946,8 @@ soundfile = function () {
};
// if there is another error, aside from 404...
request.onerror = function (e) {
var err = generateError(errorTrace);
var msg = 'loadSound error: There was no response from the server at ' + self.url + '. Check the url and internet connectivity.';
var err = new CustomError('loadSound', errorTrace, self.url);
var msg = 'There was no response from the server at ' + self.url + '. Check the url and internet connectivity.';
if (errorCallback) {
err.message = msg;
errorCallback(err);
Expand Down Expand Up @@ -2110,26 +2149,7 @@ soundfile = function () {
this.id = id;
this.val = val;
};
/**
* Helper function to generate an error
* with a custom stack trace that points to the sketch
* and removes other junk.
*
* @param {String} errorTrace custom error trace
* @return {Error} returns an Error object.
*/
function generateError(errorTrace) {
var err = new Error();
err.stack = err.stack + errorTrace;
// only print the part of the stack trace that refers to the user code:
var splitStack = err.stack.split('\n');
splitStack = splitStack.filter(function (ln) {
return !ln.match(/(p5.|native code|globalInit)/g);
});
err.stack = splitStack.join('\n');
return err;
}
}(sndcore, master);
}(sndcore, errorHandler, master);
var amplitude;
amplitude = function () {
'use strict';
Expand Down Expand Up @@ -3484,8 +3504,8 @@ signal = function () {
* @param {Number} number to multiply
* @param {Number} inMin input range minumum
* @param {Number} inMax input range maximum
* @param {Number} outMin output range minumum
* @param {Number} outMax output range maximum
* @param {Number} outMin input range minumum
* @param {Number} outMax input range maximum
* @return {p5.SignalScale} object
*/
Signal.prototype.scale = function (inMin, inMax, outMin, outMax) {
Expand Down Expand Up @@ -7328,5 +7348,5 @@ src_app = function () {
'use strict';
var p5SOUND = sndcore;
return p5SOUND;
}(sndcore, master, helpers, panner, soundfile, amplitude, fft, signal, oscillator, env, pulse, noise, audioin, filter, delay, reverb, metro, looper, soundRecorder, peakdetect, gain);
}));
}(sndcore, master, helpers, errorHandler, panner, soundfile, amplitude, fft, signal, oscillator, env, pulse, noise, audioin, filter, delay, reverb, metro, looper, soundRecorder, peakdetect, gain);
}));
6 changes: 3 additions & 3 deletions lib/p5.sound.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ define(function (require) {
var p5SOUND = require('sndcore');
require('master');
require('helpers');
require('errorHandler');
require('panner');
require('soundfile');
require('amplitude');
Expand Down
42 changes: 42 additions & 0 deletions src/errorHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
define(function (require) {
'use strict';

/**
* Helper function to generate an error
* with a custom stack trace that points to the sketch
* and removes other parts of the stack trace.
*
* @private
*
* @param {String} name custom error name
* @param {String} errorTrace custom error trace
* @param {String} failedPath path to the file that failed to load
* @property {String} name custom error name
* @property {String} message custom error message
* @property {String} stack trace the error back to a line in the user's sketch.
* Note: this edits out stack trace within p5.js and p5.sound.
* @property {String} originalStack unedited, original stack trace
* @property {String} failedPath path to the file that failed to load
* @return {Error} returns a custom Error object
*/
var CustomError = function(name, errorTrace, failedPath) {
var err = new Error();
var tempStack, splitStack;

err.name = name;
err.originalStack = err.stack + errorTrace;
tempStack = err.stack + errorTrace;
err.failedPath = failedPath;

// only print the part of the stack trace that refers to the user code:
var splitStack = tempStack.split('\n');
splitStack = splitStack.filter(function(ln) {
return !ln.match(/(p5.|native code|globalInit)/g);
});
err.stack = splitStack.join('\n');

return err;
};

return CustomError
});
33 changes: 6 additions & 27 deletions src/soundfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ define(function (require) {
'use strict';

require('sndcore');
var CustomError = require('errorHandler');
var p5sound = require('master');
var ac = p5sound.audiocontext;

Expand Down Expand Up @@ -221,7 +222,7 @@ define(function (require) {
},
// error decoding buffer. "e" is undefined in Chrome 11/22/2015
function(e) {
var err = generateError(errorTrace);
var err = new CustomError('decodeAudioData', errorTrace, self.url);
var msg = 'AudioContext error at decodeAudioData for ' + self.url;
if (errorCallback) {
err.msg = msg;
Expand All @@ -234,8 +235,8 @@ define(function (require) {
}
// if request status != 200, it failed
else {
var err = generateError(errorTrace);
var msg = 'Unable to load ' + self.url + ' ' + request.status + ' (' + request.statusText + ')';
var err = new CustomError('loadSound', errorTrace, self.url);
var msg = 'Unable to load ' + self.url + '. The request status was: ' + request.status + ' (' + request.statusText + ')';

if (errorCallback) {
err.message = msg;
Expand All @@ -248,8 +249,8 @@ define(function (require) {

// if there is another error, aside from 404...
request.onerror = function(e) {
var err = generateError(errorTrace);
var msg = 'loadSound error: There was no response from the server at ' + self.url + '. Check the url and internet connectivity.';
var err = new CustomError('loadSound', errorTrace, self.url);
var msg = 'There was no response from the server at ' + self.url + '. Check the url and internet connectivity.';

if (errorCallback) {
err.message = msg;
Expand Down Expand Up @@ -1629,27 +1630,5 @@ define(function (require) {
this.val = val;
};

/**
* Helper function to generate an error
* with a custom stack trace that points to the sketch
* and removes other junk.
*
* @param {String} errorTrace custom error trace
* @return {Error} returns an Error object.
*/
function generateError(errorTrace) {
var err = new Error();
err.stack = err.stack + errorTrace;

// only print the part of the stack trace that refers to the user code:
var splitStack = err.stack.split('\n');
splitStack = splitStack.filter(function(ln) {
return !ln.match(/(p5.|native code|globalInit)/g);
});
err.stack = splitStack.join('\n');

return err;
};


});

0 comments on commit d6c2102

Please sign in to comment.