From f64860d73cbbf1e926c4a81c671a734d5aaaf648 Mon Sep 17 00:00:00 2001 From: Reeju Bhattacharya Date: Sun, 27 Mar 2022 19:06:45 +0530 Subject: [PATCH 1/2] initial commit --- src/core/friendly_errors/file_errors.js | 7 ++++ src/io/files.js | 52 ++++++++++++++++++++++++- translations/en/translation.json | 1 + translations/es/translation.json | 1 + 4 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/core/friendly_errors/file_errors.js b/src/core/friendly_errors/file_errors.js index 4bc3bd85cb..565085ad49 100644 --- a/src/core/friendly_errors/file_errors.js +++ b/src/core/friendly_errors/file_errors.js @@ -74,6 +74,13 @@ if (typeof IS_MINIFIED !== 'undefined') { message: translator('fes.fileLoadError.gif'), method: 'loadImage' }; + case 9: + return { + message: translator('fes.fileLoadError.get', { + suggestion + }), + method: 'httpGet' + }; } }; /** diff --git a/src/io/files.js b/src/io/files.js index 49ae034753..30b1731d34 100644 --- a/src/io/files.js +++ b/src/io/files.js @@ -822,8 +822,56 @@ p5.prototype.httpGet = function() { p5._validateParameters('httpGet', arguments); const args = Array.prototype.slice.call(arguments); - args.splice(1, 0, 'GET'); - return p5.prototype.httpDo.apply(this, args); + const path = args[0]; + let datatype; // String + let data; // Object|Boolean + let callback; // Function + let errorCallback; // Function + + for (let i = 1; i < args.length; i++) { + const arg = args[i]; + if (typeof arg === 'string') { + datatype = arg; + } else if (typeof arg === 'object') { + data = arg; + } else if (typeof arg === 'function') { + if (!callback) { + callback = arg; + } else { + errorCallback = arg; + } + } + } + + // args.splice(1, 0, 'GET'); + // const argsArray = [path, 'GET', datatype]; + // if (data) argsArray.push(data); + // if (callback) argsArray.push(callback); + // if (errorCallback) argsArray.push(err); + + return this.httpDo( + path, + 'GET', + datatype, + data, + resp => { + if (callback) { + callback(resp); + } else { + return resp; + } + }, + err => { + p5._friendlyFileLoadError(9, path); + if (errorCallback) { + errorCallback(err); + } else { + throw err; + } + } + ); + + // return p5.prototype.httpDo.apply(this, argsArray); }; /** diff --git a/translations/en/translation.json b/translations/en/translation.json index f0afe49617..6fa87add31 100644 --- a/translations/en/translation.json +++ b/translations/en/translation.json @@ -5,6 +5,7 @@ "fileLoadError": { "bytes": "It looks like there was a problem loading your file. {{suggestion}}", "font": "It looks like there was a problem loading your font. {{suggestion}}", + "get": "It looks like there was a problem completing your GET request. {{suggestion}}", "gif": "There was some trouble loading your GIF. Make sure that your GIF is using 87a or 89a encoding.", "image": "It looks like there was a problem loading your image. {{suggestion}}", "json": "It looks like there was a problem loading your JSON file. {{suggestion}}", diff --git a/translations/es/translation.json b/translations/es/translation.json index 7ab126b1d9..8106f119e3 100644 --- a/translations/es/translation.json +++ b/translations/es/translation.json @@ -5,6 +5,7 @@ "fileLoadError": { "bytes": "", "font": "", + "get": "", "gif": "", "image": "", "json": "", From 741e180627001a295cdbaa0768255dab4ecce72c Mon Sep 17 00:00:00 2001 From: Reeju Bhattacharya Date: Sun, 27 Mar 2022 19:24:03 +0530 Subject: [PATCH 2/2] remove unnecessary comments --- src/io/files.js | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/io/files.js b/src/io/files.js index 30b1731d34..eea5dffdb9 100644 --- a/src/io/files.js +++ b/src/io/files.js @@ -823,10 +823,10 @@ p5.prototype.httpGet = function() { const args = Array.prototype.slice.call(arguments); const path = args[0]; - let datatype; // String - let data; // Object|Boolean - let callback; // Function - let errorCallback; // Function + let datatype; + let data; + let callback; + let errorCallback; for (let i = 1; i < args.length; i++) { const arg = args[i]; @@ -843,12 +843,6 @@ p5.prototype.httpGet = function() { } } - // args.splice(1, 0, 'GET'); - // const argsArray = [path, 'GET', datatype]; - // if (data) argsArray.push(data); - // if (callback) argsArray.push(callback); - // if (errorCallback) argsArray.push(err); - return this.httpDo( path, 'GET', @@ -870,8 +864,6 @@ p5.prototype.httpGet = function() { } } ); - - // return p5.prototype.httpDo.apply(this, argsArray); }; /**